import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_sticky_header/flutter_sticky_header.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:get/get.dart';
import 'package:nomo/app/widgets/click_opacity.dart';
import 'package:nomo/config/theme/theme_extensions/theme_extension.dart';

import '../../../constants/assets.dart';
import '../../../data/models/launch/groups.dart';
import '../controllers/node_controller.dart';

class NodeList extends StatefulWidget {
  final int tabIndex;

  const NodeList({super.key, required this.tabIndex});

  @override
  State<NodeList> createState() {
    return _NodeListState();
  }
}

class _NodeListState extends State<NodeList>
    with AutomaticKeepAliveClientMixin {
  @override
  bool get wantKeepAlive => true;

  // 获取 Controller
  NodeController get controller => Get.find<NodeController>();

  /// 获取国家的展开状态
  bool _getExpandedState(String countryCode) {
    return controller.getExpandedState(widget.tabIndex, countryCode);
  }

  /// 设置国家的展开状态
  void _setExpandedState(String countryCode, bool expanded) {
    setState(() {
      controller.setExpandedState(widget.tabIndex, countryCode, expanded);
    });
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);

    // 获取当前 tab 的数据
    final data = controller.getDataByTabIndex(widget.tabIndex);

    if (data == null || data.tags == null || data.list == null) {
      return Center(
        child: Text(
          '暂无数据',
          style: TextStyle(fontSize: 16.sp, color: Get.reactiveTheme.hintColor),
        ),
      );
    }

    // 按 tag 分组数据
    final groupedData = <int, List<dynamic>>{};
    for (var location in data.list!) {
      if (location.tag != null) {
        groupedData.putIfAbsent(location.tag!, () => []).add(location);
      }
    }

    // 创建 tag 名称映射
    final tagNameMap = <int, String>{};
    for (var tag in data.tags!) {
      if (tag.id != null && tag.name != null) {
        tagNameMap[tag.id!] = tag.name!;
      }
    }

    // 按 sort 排序 tags
    final sortedTags = data.tags!.toList()
      ..sort((a, b) => (a.sort ?? 0).compareTo(b.sort ?? 0));

    return CustomScrollView(
      slivers: [
        for (var tag in sortedTags)
          if (groupedData.containsKey(tag.id) &&
              groupedData[tag.id]!.isNotEmpty)
            SliverStickyHeader(
              header: Container(
                color: Get.reactiveTheme.scaffoldBackgroundColor,
                padding: const EdgeInsets.all(16),
                child: Text(
                  tag.name ?? '',
                  style: TextStyle(
                    color: Get.reactiveTheme.hintColor,
                    fontSize: 16,
                    fontWeight: FontWeight.bold,
                  ),
                ),
              ),
              sliver: SliverList(
                delegate: SliverChildBuilderDelegate((context, i) {
                  final locationList = groupedData[tag.id]![i];
                  final countryCode = locationList.icon ?? '';

                  return _CountrySection(
                    locationList: locationList,
                    // 传递展开状态
                    expanded: _getExpandedState(countryCode),
                    // 展开状态变化回调
                    onExpandedChanged: (expanded) {
                      _setExpandedState(countryCode, expanded);
                    },
                  );
                }, childCount: groupedData[tag.id]!.length),
              ),
            ),
      ],
    );
  }
}

class _CountrySection extends StatelessWidget {
  final LocationList locationList;
  final bool expanded;
  final ValueChanged<bool> onExpandedChanged;

  const _CountrySection({
    required this.locationList,
    required this.expanded,
    required this.onExpandedChanged,
  });

  @override
  Widget build(BuildContext context) {
    final countryIcon = locationList.icon ?? '';
    final countryName = locationList.name ?? '';
    final locations = locationList.locations ?? [];

    return Container(
      margin: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
      decoration: BoxDecoration(
        color: Get.reactiveTheme.highlightColor,
        borderRadius: BorderRadius.circular(12),
      ),
      child: Column(
        children: [
          ClickOpacity(
            onTap: () => onExpandedChanged(!expanded),
            child: Padding(
              padding: EdgeInsets.all(14.w),
              child: Row(
                children: [
                  // 国旗图标
                  ClipRRect(
                    borderRadius: BorderRadius.circular(4.r),
                    child: SvgPicture.asset(
                      Assets.getCountryFlagImage(countryIcon),
                      width: 32.w,
                      height: 24.w,
                      fit: BoxFit.cover,
                    ),
                  ),
                  10.horizontalSpace,
                  Text(
                    countryName,
                    style: TextStyle(
                      fontSize: 16.sp,
                      height: 1.5,
                      fontWeight: FontWeight.w500,
                      color: Get.reactiveTheme.textTheme.bodyLarge!.color,
                    ),
                  ),
                  const Spacer(),
                  // 箭头图标
                  AnimatedRotation(
                    turns: expanded ? 0.25 : 0.0,
                    duration: const Duration(milliseconds: 300),
                    child: Icon(
                      Icons.keyboard_arrow_right,
                      size: 20.w,
                      color: Get.reactiveTheme.hintColor,
                    ),
                  ),
                ],
              ),
            ),
          ),
          ClipRect(
            child: AnimatedAlign(
              duration: const Duration(milliseconds: 300),
              curve: Curves.easeInOut,
              heightFactor: expanded ? 1.0 : 0.0,
              alignment: Alignment.topLeft,
              child: AnimatedOpacity(
                opacity: expanded ? 1.0 : 0.0,
                duration: const Duration(milliseconds: 300),
                child: Column(
                  children: locations.map((location) {
                    final locationName = location.name ?? '';
                    final locationIcon = location.icon ?? countryIcon;
                    final latency = location.latency;

                    return ClickOpacity(
                      onTap: () {
                        // TODO: 处理节点选择
                      },
                      child: Column(
                        children: [
                          Divider(
                            height: 1,
                            color: Get.reactiveTheme.dividerColor,
                          ),
                          Container(
                            alignment: Alignment.centerLeft,
                            margin: EdgeInsets.only(
                              top: 14.w,
                              bottom: 14.w,
                              left: 24.w,
                              right: 14.w,
                            ),
                            child: Row(
                              children: [
                                ClipRRect(
                                  borderRadius: BorderRadius.circular(4.r),
                                  child: SvgPicture.asset(
                                    Assets.getCountryFlagImage(locationIcon),
                                    width: 32.w,
                                    height: 24.w,
                                    fit: BoxFit.cover,
                                  ),
                                ),
                                10.horizontalSpace,
                                Expanded(
                                  child: Text(
                                    locationName,
                                    style: TextStyle(
                                      fontSize: 14.sp,
                                      fontWeight: FontWeight.w500,
                                      color: Get.reactiveTheme.hintColor,
                                    ),
                                  ),
                                ),
                                // 显示延迟
                                if (latency != null && latency > 0)
                                  Text(
                                    '${latency}ms',
                                    style: TextStyle(
                                      fontSize: 12.sp,
                                      color: Get.reactiveTheme.hintColor,
                                    ),
                                  ),
                              ],
                            ),
                          ),
                        ],
                      ),
                    );
                  }).toList(),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
