import 'dart:async';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:vitalapp/architecture/defines.dart';
import 'package:vitalapp/helper/goto_helper.dart';
import 'package:vitalapp/managers/interfaces/account.dart';
import 'package:vitalapp/pages/controllers/home_nav_mixin.dart';
import 'package:vitalapp/pages/home/models/menu.dart';
import 'package:vitalapp/store/store.dart';

import 'state.dart';

class HomeController extends FControllerBase with HomeNavMixin {
  static Widget _buildImgIcon(String assetName) {
    return ClipRect(
      child: SizedBox(
        height: 34,
        child: Image.asset(
          "assets/images/home/$assetName",
          width: 46,
          height: 46,
          fit: BoxFit.fitWidth,
        ),
      ),
    );
  }

  final state = HomeState();

  /// 当前选中路由
  int currentIndex = -1;
  List<HomeMenuItem> homeMenuItems = [
    HomeMenuItem(
      key: 'ZY',
      title: "主页",
      routeName: "/dashboard",
      iconData: Icons.home_outlined,
    ),
    HomeMenuItem(
      key: 'JKDA',
      title: "健康档案",
      routeName: "/patient/detail",
      iconWidget: _buildImgIcon('healthRecord.png'),
    ),
    HomeMenuItem(
      key: 'JMLB',
      title: "居民列表",
      routeName: "/patient/list",
      iconWidget: _buildImgIcon('healthRecord.png'),
    ),
    HomeMenuItem(
      key: 'YSQY',
      title: "医生签约",
      routeName: "/contract/package_list",
      iconWidget: _buildImgIcon('doctorSigning.png'),
    ),
    HomeMenuItem(
      key: 'JKTJ',
      title: "健康体检",
      routeName: "/check/form",
      iconWidget: _buildImgIcon('healthCheckup.png'),
    ),
    HomeMenuItem(
      key: 'RQSF',
      title: "人群随访",
      routeName: "/check/follow_up",
      iconWidget: _buildImgIcon('populationFollowUp.png'),
    ),
    HomeMenuItem(
      key: 'JKJC',
      title: "健康检测",
      routeName: "/medical",
      iconWidget: _buildImgIcon('diagnosisDisplay.png'),
      isSelected: true,
    ),
    HomeMenuItem(
      key: 'SZZX',
      title: "设置中心",
      routeName: "/settings",
      iconData: Icons.settings,
    ),
  ];

  /// 登出
  Future<void> logOut() async {
    final result = await Get.find<IAccountManager>().logout();
    if (result) {
      Get.offAllNamed("/login");
    }
  }

  Future<void> changeMedical() async {
    try {
      Get.offAllNamed("/medical", id: 1001);
    } catch (e) {}
  }

  @override
  void onInit() {
    initMenus();
    super.onInit();
  }

  /// 切换活动菜单
  void switchActiveMenu(HomeMenuItem data) async {
    if (state.currentSelectMenu != data.routeName) {
      switchNavByName(data.routeName);
    }
  }

  void switchNavByName(String name) {
    state.currentSelectMenu = name;
    NavGotoHelper.goto(name);
  }

  void initMenus() {
    state.menuItems = [
      // HomeMenuItem(
      //   key: 'ZY',
      //   title: "主页",
      //   routeName: "/dashboard",
      //   iconData: Icons.home_outlined,
      //   isSelected: true,
      // ),
      HomeMenuItem(
        key: 'JMLB',
        title: "居民列表",
        routeName: "/patient/list",
        iconWidget: _buildImgIcon('healthRecord.png'),
      ),
      HomeMenuItem(
        key: 'JKJC',
        title: "健康检测",
        routeName: "/medical",
        iconWidget: _buildImgIcon('diagnosisDisplay.png'),
        isSelected: true,
      ),
      HomeMenuItem(
        key: 'SZZX',
        title: "设置中心",
        routeName: "/settings",
        iconData: Icons.settings,
      ),
    ];
  }

  void updateMenus() {
    if (Store.user.menuPermissionList?.isNotEmpty ?? false) {
      List<HomeMenuItem> menuItems = [];
      Store.user.menuPermissionList?.forEach((element) {
        for (var item in homeMenuItems) {
          if (item.key == element.code) {
            menuItems.add(item);
          }
        }
      });
      state.menuItems = menuItems;
    } else {
      // state.menuItems = homeMenuItems;
      state.menuItems = [
        // HomeMenuItem(
        //   key: 'ZY',
        //   title: "主页",
        //   routeName: "/dashboard",
        //   iconData: Icons.home_outlined,
        //   isSelected: true,
        // ),
        HomeMenuItem(
          key: 'JKDA',
          title: "健康档案",
          routeName: "/patient/detail",
          iconWidget: _buildImgIcon('healthRecord.png'),
        ),
        HomeMenuItem(
          key: 'JKJC',
          title: "健康检测",
          routeName: "/medical",
          iconWidget: _buildImgIcon('diagnosisDisplay.png'),
        ),
        HomeMenuItem(
          key: 'SZZX',
          title: "设置中心",
          routeName: "/settings",
          iconData: Icons.settings,
        ),
      ];
    }
  }
}
