import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:keyboard/base/base_view.dart';
import 'package:keyboard/module/intimacy_analyse/screenshot_reply/scan_image_reply/scan_image_reply_view.dart';
import 'package:nested_scroll_views/material.dart';

import '../../../resource/colors.gen.dart';
import 'conversation_analysis/conversation_analysis_view.dart';
import 'intimacy_analyse_screenshot_reply_controller.dart';

/// 亲密度分析-截图回复Tab页
class IntimacyAnalyseScreenshotReplyView
    extends BaseView<IntimacyAnalyseScreenshotReplyController> {
  const IntimacyAnalyseScreenshotReplyView({super.key});

  @override
  Color backgroundColor() {
    return Colors.transparent;
  }

  @override
  Widget buildBody(BuildContext context) {
    return Column(
      children: [
        SizedBox(height: 13.h),
        // TabBar
        _buildTabBar(),
        SizedBox(height: 10.h),
        // 内容
        _buildContent(),
      ],
    );
  }

  /// TabBar
  Widget _buildTabBar() {
    return Stack(
      children: [
        Container(
          width: 258.w,
          height: 40.h,
          decoration: BoxDecoration(
            color: ColorName.white50,
            borderRadius: BorderRadius.all(Radius.circular(18.r)),
          ),
        ),
        Positioned.fill(
          child: Container(
            padding: EdgeInsets.all(2.w),
            child: TabBar(
              // 是否可以滚动
              isScrollable: false,
              // Tab居中
              tabAlignment: TabAlignment.center,
              // 去除底部的黑线
              dividerHeight: 0,
              indicatorPadding: EdgeInsets.zero,
              // 让Tab严格贴近背景，默认左右会有padding
              indicatorSize: TabBarIndicatorSize.tab,
              // 去除Tab默认的padding，否则Tab之间会有默认的间距
              labelPadding: EdgeInsets.zero,
              // 渐变指示器
              indicator: BoxDecoration(
                // 渐变背景
                gradient: LinearGradient(
                  colors: [ColorName.bgPurpleTab1, ColorName.bgPurpleTab2],
                  begin: Alignment.centerLeft,
                  end: Alignment.centerRight,
                ),
                shape: BoxShape.rectangle,
                borderRadius: BorderRadius.all(Radius.circular(47.r)),
              ),
              // 选中时的颜色
              labelStyle: TextStyle(
                fontSize: 12.sp,
                fontWeight: FontWeight.w700,
                color: ColorName.white,
              ),
              // 未选中时的颜色
              unselectedLabelStyle: TextStyle(
                fontSize: 12.sp,
                color: ColorName.black60,
                fontWeight: FontWeight.w400,
              ),
              // 配置Tab数据
              tabs:
                  controller.tabBarList.mapIndexed((int index, tabName) {
                    return Tab(
                      child: SizedBox(
                        width: 127.w,
                        child: Center(child: Text(tabName)),
                      ),
                    );
                  }).toList(),
              controller: controller.tabController,
              onTap: (index) {
                controller.handleTabChange(index);
              },
            ),
          ),
        ),
      ],
    );
  }

  /// PageView
  Widget _buildContent() {
    return Expanded(
      child: NestedPageView(
        controller: controller.pageController,
        onPageChanged: (index) {
          controller.handlePageChange(index);
        },
        children: [
          // 对话分析Tab
          ConversationAnalysisView(),
          // 识图回复Tab
          ScanImageReplyView(),
        ],
      ),
    );
  }
}
