import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import '../../../widgets/kr_simple_loading.dart';
import 'package:kaer_with_panels/app/widgets/kr_country_flag.dart';
import 'package:kaer_with_panels/app/widgets/kr_app_text_style.dart';
import 'package:kaer_with_panels/app/localization/app_translations.dart';
import 'package:kaer_with_panels/app/services/singbox_imp/kr_sing_box_imp.dart';
import '../controllers/kr_home_controller.dart';
import '../models/kr_home_views_status.dart';

class KRHomeConnectionInfoView extends GetView<KRHomeController> {
  const KRHomeConnectionInfoView({super.key});

  @override

  Widget build(BuildContext context) {
    return _buildConnectCard(context);
  }

  /// 当前连接
  Widget _buildConnectCard(BuildContext context) {
    return Obx(() {
      return Container(
        margin: EdgeInsets.symmetric(horizontal: 16.w),
        width: double.infinity,
        height: 116.w,
        decoration: ShapeDecoration(
          color: Theme.of(context).cardColor,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(16.w),
          ),
        ),
        child: Padding(
          padding: EdgeInsets.all(14.w),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text(
                    AppTranslations.kr_home.currentConnectionTitle,
                    style: KrAppTextStyle(
                      fontSize: 12,
                      color: Theme.of(context).textTheme.bodySmall?.color,
                    ),
                  ),
                  // 切换节点按钮
                  GestureDetector(
                    onTap: () {
                      controller.kr_switchListStatus(KRHomeViewsListStatus.kr_subscribeList);
                    },
                    child: Row(
                      children: [
                        Text(
                          AppTranslations.kr_home.switchNode,
                          style: KrAppTextStyle(
                            color: Theme.of(context).textTheme.bodySmall?.color,
                            fontSize: 12,
                            fontWeight: FontWeight.w400,
                          ),
                        ),
                        Icon(
                          Icons.arrow_forward_ios,
                          size: 12.w,
                          color: Theme.of(context).textTheme.bodySmall?.color,
                        ),
                      ],
                    ),
                  ),
                ],
              ),
              SizedBox(height: 10.w),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Row(
                    children: [
                      KRCountryFlag(
                        countryCode: controller.kr_getCurrentNodeCountry(),
                      ),
                      SizedBox(width: 10.w),
                      Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text(
                            controller.kr_currentNodeName.value,
                            style: KrAppTextStyle(
                              color: Theme.of(context).textTheme.bodyMedium?.color,
                              fontSize: 16,
                              fontWeight: FontWeight.w500,
                            ),
                          ),
                          SizedBox(height: 6.w),
                          Row(
                            children: [
                              Obx(() {
                                final delay = controller.kr_currentNodeLatency.value;

                                // 获取延迟颜色
                                Color getLatencyColor(int delay) {
                                  if (delay == -2) {
                                    return Colors.green;
                                  } else if (delay == -1) {
                                    return Theme.of(context).primaryColor;
                                  } else if (delay < 500) {
                                    return Colors.green;
                                  } else if (delay < 3000) {
                                    return Color(0xFFFFB700);
                                  } else {
                                    return Colors.red;
                                  }
                                }

                                // 获取延迟文本
                                String getLatencyText(int delay) {
                                  if (delay == -2) {
                                    return '0ms';
                                  } else if (delay == -1) {
                                    return AppTranslations.kr_home.connecting;
                                  } else if (delay >= 3000) {
                                    return AppTranslations.kr_home.timeout;
                                  } else {
                                    return '${delay}ms';
                                  }
                                }

                                if (delay == -1) {
                                  return Row(
                                    children: [
                                      KRSimpleLoading(
                                        color: Colors.green,
                                        size: 12.w,
                                        duration: const Duration(milliseconds: 800),
                                      ),
                                      SizedBox(width: 2.w),
                                      Text(
                                        'connecting',
                                        style: TextStyle(
                                          color: Colors.green,
                                          fontSize: 11,
                                          fontWeight: FontWeight.w400,
                                        ),
                                      ),
                                    ],
                                  );
                                }

                                return Row(
                                  children: [
                                    Icon(Icons.signal_cellular_alt,
                                        size: 12.w,
                                        color: getLatencyColor(delay)),
                                    SizedBox(width: 2),
                                    Text(
                                      getLatencyText(delay),
                                      style: KrAppTextStyle(
                                        color: getLatencyColor(delay),
                                        fontSize: 11,
                                        fontWeight: FontWeight.w400,
                                      ),
                                    ),
                                  ],
                                );
                              }),
                              // 只在非连接中状态显示上下行
                              Obx(() {
                                final delay = controller.kr_currentNodeLatency.value;
                                if (delay == -1) {
                                  return const SizedBox.shrink();
                                }
                                return Row(
                                  children: [
                                    SizedBox(width: 10.w),
                                    Icon(Icons.arrow_upward,
                                        size: 12.w,
                                        color: Theme.of(context).textTheme.bodySmall?.color),
                                    Text(
                                      controller.kr_formatBytes(KRSingBoxImp.instance.kr_stats.value.uplink),
                                      style: KrAppTextStyle(
                                        color: Theme.of(context).textTheme.bodySmall?.color,
                                        fontSize: 11,
                                        fontWeight: FontWeight.w400,
                                      ),
                                    ),
                                    SizedBox(width: 10.w),
                                    Icon(Icons.arrow_downward,
                                        size: 12.w,
                                        color: Theme.of(context).textTheme.bodySmall?.color),
                                    Text(
                                      controller.kr_formatBytes(KRSingBoxImp.instance.kr_stats.value.downlink),
                                      style: KrAppTextStyle(
                                        color: Theme.of(context).textTheme.bodySmall?.color,
                                        fontSize: 11,
                                        fontWeight: FontWeight.w400,
                                      ),
                                    ),
                                  ],
                                );
                              }),
                            ],
                          ),
                        ],
                      ),
                    ],
                  ),
                  CupertinoSwitch(
                    value: controller.kr_isConnected.value,
                    onChanged: (bool value) {
                      controller.kr_toggleSwitch(value);
                    },
                    activeTrackColor: Colors.blue,
                  ),
                ],
              ),
            ],
          ),
        ),
      );
    });
  }
} 