import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:flashlink/config/theme/theme_extensions/theme_extension.dart';
import 'package:flashlink/utils/misc.dart';

import '../../../../config/translations/strings_enum.dart';
import '../../../base/base_view.dart';
import '../../../widgets/click_opacity.dart';
import '../../../widgets/ix_app_bar.dart';
import '../controllers/node_controller.dart';
import '../widgets/node_list.dart';

class NodeView extends BaseView<NodeController> {
  const NodeView({super.key});

  @override
  PreferredSizeWidget? get appBar => IXAppBar(
    title: Strings.selectServer.tr,
    actions: [
      if (isDesktop)
        ClickOpacity(
          child: Padding(
            padding: EdgeInsets.symmetric(horizontal: 16.w),
            child: Icon(
              Icons.refresh,
              size: 20.w,
              color: Get.reactiveTheme.hintColor,
            ),
          ),
          onTap: () => controller.refreshLocations(),
        ),
    ],
  );

  @override
  Widget buildContent(BuildContext context) {
    final tabCount = controller.tabTextList.length;
    if (tabCount == 0) {
      return Center(child: Text(Strings.noData.tr));
    }
    return DefaultTabController(
      length: tabCount,
      initialIndex: controller.currentTabIndex, // 恢复上次的 Tab 位置
      child: Column(
        children: [
          if (isDesktop) _buildRefreshingIndicator(),
          _buildTabs(),
          Expanded(child: _buildTabBarView()),
        ],
      ),
    );
  }

  Widget _buildRefreshingIndicator() {
    return Obx(
      () => controller.isRefreshing
          ? LinearProgressIndicator(
              backgroundColor: Get.reactiveTheme.scaffoldBackgroundColor,
              minHeight: 2.w,
              valueColor: AlwaysStoppedAnimation<Color>(
                Get.reactiveTheme.primaryColor,
              ),
            )
          : const SizedBox(height: 2),
    );
  }

  Widget _buildTabs() {
    return TabBar(
      splashFactory: NoSplash.splashFactory,
      overlayColor: WidgetStateProperty.all(Colors.transparent),
      isScrollable: true,
      tabAlignment: TabAlignment.start,
      dividerColor: Get.reactiveTheme.dividerColor,
      indicatorSize: TabBarIndicatorSize.label,
      indicatorColor: Colors.transparent,
      // indicator: BoxDecoration(
      //   color: Get.reactiveTheme.primaryColor,
      //   borderRadius: BorderRadius.circular(8.r),
      // ),
      // indicatorWeight: 2,
      labelColor: Colors.white,
      unselectedLabelColor: Get.reactiveTheme.hintColor,
      labelStyle: TextStyle(
        fontSize: 14.sp,
        fontWeight: FontWeight.w400,
        fontFamily: 'FiraSans',
      ),
      unselectedLabelStyle: TextStyle(
        fontSize: 14.sp,
        fontWeight: FontWeight.w400,
        fontFamily: 'FiraSans',
      ),
      padding: EdgeInsets.symmetric(horizontal: 7.w, vertical: 10.w),
      labelPadding: EdgeInsets.symmetric(horizontal: 7.w, vertical: 0.w),
      // 监听 Tab 切换，保存当前索引
      onTap: (index) {
        controller.setTabSelected(index);
      },
      tabs: controller.tabTextList.map((e) {
        return Container(
          padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 8.w),
          decoration: BoxDecoration(
            color:
                controller.currentTabIndex == controller.tabTextList.indexOf(e)
                ? Get.reactiveTheme.primaryColor
                : Get.reactiveTheme.cardColor,
            borderRadius: BorderRadius.circular(8.r),
          ),
          child: Row(
            children: [
              Icon(
                controller.getTabIcon(controller.tabTextList.indexOf(e)),
                size: 14.w,
              ),
              4.horizontalSpace,
              Text(e),
            ],
          ),
        );
      }).toList(),
    );
  }

  Widget _buildTabBarView() {
    return TabBarView(
      physics: const NeverScrollableScrollPhysics(), // 禁用左右滑动
      children: List.generate(
        controller.tabTextList.length,
        (index) => NodeList(tabIndex: index),
      ),
    );
  }
}
