import 'package:electronic_assistant/base/base_page.dart';
import 'package:electronic_assistant/module/main/controller.dart';
import 'package:electronic_assistant/module/main/drawer/view.dart';
import 'package:electronic_assistant/resource/assets.gen.dart';
import 'package:electronic_assistant/resource/colors.gen.dart';
import 'package:electronic_assistant/utils/expand.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import '../../router/app_pages.dart';
import '../files/view.dart';
import '../home/view.dart';

class MainTabPage extends BasePage<MainController> {
  MainTabPage({super.key});

  final pages = [
    const HomePage(),
    const FilesPage(),
  ];

  @override
  Widget buildBody(BuildContext context) {
    return Scaffold(
      key: controller.scaffoldKey,
      body: Obx(() {
        return pages[controller.currentIndex];
      }),
      floatingActionButton: buildAIChatBtn(),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
      bottomNavigationBar: buildBottomAppBar(),
      drawer: Drawer(
        shape: const RoundedRectangleBorder(
          borderRadius: BorderRadius.zero,
        ),
        backgroundColor: "#F6F5F8".toColor(),
        child: const MainDrawerView(),
      ),
    );
  }

  @override
  bool immersive() {
    return true;
  }

  Widget buildAIChatBtn() {
    return GestureDetector(
      onTap: () {
        controller.onChatClick();
      },
      child: Container(
        width: 68.w,
        height: 68.w,
        decoration: const BoxDecoration(
          color: ColorName.white,
          shape: BoxShape.circle,
        ),
        margin: EdgeInsets.only(top: 36.w),
        padding: EdgeInsets.all(6.w),
        child: Assets.images.mainTabSecretary.image(),
      ),
    );
  }

  BottomAppBar buildBottomAppBar() {
    return BottomAppBar(
      color: Colors.white,
      height: 68.h,
      child: Obx(() {
        return Flex(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          direction: Axis.horizontal,
          children: <Widget>[
            Expanded(
              flex: 1,
              child: bottomAppBarItem(0),
            ),
            const Expanded(
              flex: 1,
              child: SizedBox(),
            ),
            const SizedBox(),
            Expanded(
              flex: 1,
              child: bottomAppBarItem(1),
            ),
          ],
        );
      }),
    );
  }

  Widget bottomAppBarItem(int index) {
    //设置默认未选中的状态
    TextStyle style;
    TabBean tabBean = controller.tabBeans[index];
    String imagePath;
    if (controller.currentIndex == index) {
      //选中的话
      style = TextStyle(fontSize: 10.sp, color: tabBean.txtSelectedColor);
      imagePath = tabBean.selectedIcon;
    } else {
      style = TextStyle(fontSize: 10.sp, color: tabBean.txtNormalColor);
      imagePath = tabBean.normalIcon;
    }
    //构造返回的Widget
    Widget item = GestureDetector(
      behavior: HitTestBehavior.opaque,
      child: Center(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            Expanded(child: Image.asset(imagePath)),
            Text(
              tabBean.title.tr,
              style: style,
            )
          ],
        ),
      ),
      onTap: () {
        if (controller.currentIndex != index) {
          controller.updateIndex(index);
        }
      },
    );
    return item;
  }
}
