import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:nomo/app/base/base_view.dart';
import 'package:nomo/app/widgets/click_opacity.dart';
import 'package:nomo/app/widgets/ix_app_bar.dart';
import 'package:nomo/app/widgets/submit_btn.dart';
import 'package:nomo/config/theme/theme_extensions/theme_extension.dart';

import '../../../../config/theme/dark_theme_colors.dart';
import '../../../../config/translations/strings_enum.dart';
import '../../../constants/assets.dart';
import '../../../constants/iconfont/iconfont.dart';
import '../../../widgets/ix_image.dart';
import '../controllers/account_controller.dart';

class AccountView extends BaseView<AccountController> {
  const AccountView({super.key});

  @override
  PreferredSizeWidget? get appBar => IXAppBar(title: Strings.account.tr);

  @override
  Widget buildContent(BuildContext context) {
    return Obx(() {
      final isPremium = controller.isPremium.value;
      return SingleChildScrollView(
        padding: EdgeInsets.symmetric(horizontal: 14.w, vertical: 10.w),
        child: Column(
          children: [
            // Account 信息卡片
            _buildAccountCard(isPremium),

            20.verticalSpaceFromWidth,

            // 底部按钮
            _buildBottomButtons(isPremium),

            20.verticalSpaceFromWidth,
          ],
        ),
      );
    });
  }

  /// 构建账户信息卡片
  Widget _buildAccountCard(bool isPremium) {
    return Container(
      decoration: BoxDecoration(
        color: Get.reactiveTheme.highlightColor,
        borderRadius: BorderRadius.circular(12.r),
      ),
      child: Column(
        children: [
          // Account 条目
          _buildAccountItem(isPremium),
          _buildDivider(),

          // UID 条目
          _buildUIDItem(),
          _buildDivider(),

          // Time/Term 条目
          if (isPremium) _buildValidTermItem() else _buildFreeTimeItem(),
          _buildDivider(),
          // Premium 功能列表
          _buildPremiumFeatures(isPremium),
        ],
      ),
    );
  }

  /// Account 条目
  Widget _buildAccountItem(bool isPremium) {
    return Container(
      height: 56.w,
      padding: EdgeInsets.symmetric(horizontal: 16.w),
      child: Row(
        children: [
          // 图标
          Container(
            width: 30.w,
            height: 30.w,
            decoration: BoxDecoration(
              color: Get.reactiveTheme.shadowColor,
              borderRadius: BorderRadius.circular(8.r),
            ),
            child: Icon(IconFont.icon29, size: 20.w, color: Colors.white),
          ),
          10.horizontalSpace,
          // 标题
          Expanded(
            child: Text(
              'Account',
              style: TextStyle(
                fontSize: 14.sp,
                color: Get.reactiveTheme.textTheme.bodyLarge!.color,
                fontWeight: FontWeight.w400,
              ),
            ),
          ),
          // 徽章
          IXImage(
            source: isPremium ? Assets.premium : Assets.free,
            width: isPremium ? 92.w : 64.w,
            height: 28.w,
            sourceType: ImageSourceType.asset,
          ),
        ],
      ),
    );
  }

  /// UID 条目
  Widget _buildUIDItem() {
    return ClickOpacity(
      onTap: () {
        Clipboard.setData(ClipboardData(text: controller.uid));
        Get.snackbar(Strings.copied.tr, 'UID ${Strings.copied.tr}');
      },
      child: Container(
        height: 56.w,
        padding: EdgeInsets.symmetric(horizontal: 16.w),
        child: Row(
          children: [
            // 图标
            Container(
              width: 30.w,
              height: 30.w,
              decoration: BoxDecoration(
                color: Get.reactiveTheme.shadowColor,
                borderRadius: BorderRadius.circular(8.r),
              ),
              child: Icon(IconFont.icon14, size: 20.w, color: Colors.white),
            ),
            10.horizontalSpace,
            // UID
            Expanded(
              child: Text(
                controller.uid,
                style: TextStyle(
                  fontSize: 14.sp,
                  color: Get.reactiveTheme.textTheme.bodyLarge!.color,
                  fontWeight: FontWeight.w400,
                ),
              ),
            ),
            // 复制图标
            Icon(
              IconFont.icon57,
              size: 20.w,
              color: Get.reactiveTheme.hintColor,
            ),
          ],
        ),
      ),
    );
  }

  /// Free Time 条目
  Widget _buildFreeTimeItem() {
    return Container(
      height: 56.w,
      padding: EdgeInsets.symmetric(horizontal: 16.w),
      child: Row(
        children: [
          // 图标
          Container(
            width: 30.w,
            height: 30.w,
            decoration: BoxDecoration(
              color: Get.reactiveTheme.shadowColor,
              borderRadius: BorderRadius.circular(8.r),
            ),
            child: Icon(IconFont.icon30, size: 20.w, color: Colors.white),
          ),
          10.horizontalSpace,
          // 标题
          Expanded(
            child: Text(
              'Free Time',
              style: TextStyle(
                fontSize: 14.sp,
                color: Get.reactiveTheme.textTheme.bodyLarge!.color,
                fontWeight: FontWeight.w400,
              ),
            ),
          ),
          // 时间
          Text(
            controller.freeTime,
            style: TextStyle(
              fontSize: 13.sp,
              color: DarkThemeColors.validTermColor,
              fontWeight: FontWeight.w400,
            ),
          ),
        ],
      ),
    );
  }

  /// Valid Term 条目
  Widget _buildValidTermItem() {
    return Container(
      height: 56.w,
      padding: EdgeInsets.symmetric(horizontal: 16.w),
      child: Row(
        children: [
          // 图标
          Container(
            width: 30.w,
            height: 30.w,
            decoration: BoxDecoration(
              color: Get.reactiveTheme.shadowColor,
              borderRadius: BorderRadius.circular(8.r),
            ),
            child: Icon(IconFont.icon30, size: 20.w, color: Colors.white),
          ),
          10.horizontalSpace,
          // 标题
          Expanded(
            child: Text(
              'Valid Term',
              style: TextStyle(
                fontSize: 14.sp,
                color: Get.reactiveTheme.textTheme.bodyLarge!.color,
                fontWeight: FontWeight.w400,
              ),
            ),
          ),
          // 有效期
          Text(
            controller.validTerm,
            style: TextStyle(
              fontSize: 13.sp,
              color: Get.reactiveTheme.primaryColor,
              fontWeight: FontWeight.w400,
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildPremiumFeatures(bool isPremium) {
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: 14.w),
      child: Column(
        children: [
          _buildFeatureItem(IconFont.icon60, 'Unlock all free locations'),
          _buildFeatureItem(IconFont.icon61, 'Unlock smart mode'),
          _buildFeatureItem(IconFont.icon62, 'Unlock Multi-hop mode'),
          _buildFeatureItem(IconFont.icon63, 'Premium can share X devices'),
          _buildFeatureItem(IconFont.icon64, 'Own your own private server'),
          _buildFeatureItem(IconFont.icon65, 'Close ads'),
        ],
      ),
    );
  }

  Widget _buildFeatureItem(IconData icon, String title) {
    return SizedBox(
      height: 44.w,
      child: Row(
        children: [
          Icon(icon, color: DarkThemeColors.subscriptionColor, size: 24.w),
          12.horizontalSpace,
          Expanded(
            child: Text(
              title,
              style: TextStyle(
                fontSize: 13.sp,
                color: Get.reactiveTheme.hintColor,
              ),
            ),
          ),
          Container(
            width: 20.w,
            height: 20.w,
            decoration: BoxDecoration(
              shape: BoxShape.circle,
              color: DarkThemeColors.subscriptionSelectColor,
            ),
            child: Icon(Icons.check, color: Colors.white, size: 12.w),
          ),
        ],
      ),
    );
  }

  /// 底部按钮
  Widget _buildBottomButtons(bool isPremium) {
    if (isPremium) {
      return Column(
        children: [
          SubmitButton(
            text: 'Change Subscription',
            bgColor: Get.reactiveTheme.highlightColor,
            textColor: DarkThemeColors.subscriptionColor,
            onPressed: () {
              // TODO: 修改订阅
            },
          ),
          20.verticalSpaceFromWidth,
          // Device Authorization 按钮
          _buildSecondaryButton(
            text:
                'Device Authorization (${controller.deviceCount}/${controller.maxDeviceCount})',
            icon: IconFont.icon11,
            onTap: () {
              // TODO: 设备授权
            },
          ),
          10.verticalSpaceFromWidth,
          // 提示文字
          Text(
            'You can authorize other devices as Premium users\n(${controller.deviceCount}/${controller.maxDeviceCount})',
            textAlign: TextAlign.center,
            style: TextStyle(
              fontSize: 12.sp,
              color: Get.reactiveTheme.hintColor,
              height: 1.5,
            ),
          ),
        ],
      );
    } else {
      return Column(
        children: [
          // Upgrade to Premium 按钮
          SubmitButton(
            text: Strings.upgradeToPremium.tr,
            bgColor: Get.reactiveTheme.highlightColor,
            textColor: DarkThemeColors.subscriptionColor,
            onPressed: () {
              // TODO: 修改订阅
            },
          ),
          20.verticalSpaceFromWidth,
          // Activate Pre Code 按钮
          _buildSecondaryButton(
            text: Strings.activatePreCode.tr,
            icon: IconFont.icon23,
            onTap: () {
              // TODO: 激活兑换码
            },
          ),
          10.verticalSpaceFromWidth,
          // 提示文字
          Text(
            Strings.preCodeHint.tr,
            textAlign: TextAlign.center,
            style: TextStyle(
              fontSize: 12.sp,
              color: Get.reactiveTheme.hintColor,
            ),
          ),
        ],
      );
    }
  }

  /// 次要按钮（黑色边框）
  Widget _buildSecondaryButton({
    required String text,
    required IconData icon,
    required VoidCallback onTap,
  }) {
    return ClickOpacity(
      onTap: onTap,
      child: Container(
        height: 52.w,
        decoration: BoxDecoration(
          border: Border.all(color: Get.reactiveTheme.dividerColor, width: 1.w),
          borderRadius: BorderRadius.circular(12.r),
        ),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              text,
              style: TextStyle(
                fontSize: 16.sp,
                color: DarkThemeColors.subscriptionColor,
                fontWeight: FontWeight.w400,
              ),
            ),
            SizedBox(width: 8.w),
            Icon(icon, size: 20.w, color: DarkThemeColors.subscriptionColor),
          ],
        ),
      ),
    );
  }

  /// 构建分割线
  Widget _buildDivider() {
    return Divider(height: 1.w, color: Get.reactiveTheme.dividerColor);
  }
}
