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/resource/string.gen.dart';
import 'package:electronic_assistant/utils/expand.dart';
import 'package:electronic_assistant/utils/toast_util.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.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 PopScope(
      canPop: false,
      onPopInvokedWithResult: (bool didPop, dynamic result) async {
        if (controller.scaffoldKey.currentState?.isDrawerOpen == true) {
          controller.closeDrawer();
          return;
        }
        if ((controller.lastPressedAt == null ||
            DateTime.now().difference(controller.lastPressedAt!) >
                    const Duration(seconds: 2))) {
          controller.setLastPressedAt(DateTime.now());
          ToastUtil.showToast(StringName.exitAppTip.tr);
        } else {
          controller.exit();
        }
      },
      child: Scaffold(
        key: controller.scaffoldKey,
        body: Obx(() {
          return pages[controller.currentIndex];
        }),
        resizeToAvoidBottomInset: false,
        floatingActionButton: buildAIChatBtn(),
        floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
        bottomNavigationBar: buildBottomAppBar(),
        drawerEdgeDragWidth: 0,
        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: Align(
          alignment: Alignment.bottomCenter,
          child: SizedBox(
            width: 62.w,
            child: AspectRatio(
                aspectRatio: 195 / 223,
                child: Assets.images.mainTabSecretary.image()),
          ),
        ));
  }

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

  Widget bottomAppBarItem(int index) {
    return Obx(() {
      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;
      }
      return GestureDetector(
        behavior: HitTestBehavior.opaque,
        child: SizedBox(
          height: 56.h,
          child: Center(
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                Image.asset(imagePath, width: 24.w, height: 24.w),
                Text(
                  tabBean.title.tr,
                  style: style,
                )
              ],
            ),
          ),
        ),
        onTap: () {
          if (controller.currentIndex != index) {
            controller.updateIndex(index);
          }
        },
      );
    });
  }
}
