import 'dart:async';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:vitalapp/architecture/defines.dart';
import 'package:vitalapp/architecture/utils/common_util.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 'state.dart';

class HomeController extends FControllerBase with HomeNavMixin {
  final state = HomeState();

  /// 当前选中路由
  int currentIndex = -1;

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

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

  /// 切换活动菜单
  void switchActiveMenu(HomeMenuItem data) {
    CommonUtil.throttle(() {
      switchNavByName(data.routeName);
    });
    // final index =
    //     state.menuItems.indexWhere((e) => e.routeName == data.routeName);
    // if (index < 0) return;

    // final array = state.menuItems;
    // array.firstWhereOrNull((e) => e.isSelected)?.isSelected = false;
    // array[index].isSelected = true;
    // state.menuItems = array;

    // Get.offAllNamed(data.routeName, id: 1001);
  }

  void switchNavByName(String name) {
    final index = state.menuItems.indexWhere((e) => e.routeName == name);
    if (index < 0 || currentIndex == index) return;

    final array = state.menuItems;

    array.firstWhereOrNull((e) => e.isSelected)?.isSelected = false;
    array[index].isSelected = true;
    state.menuItems = array;
    currentIndex = index;
    Get.offAllNamed(name, id: 1001);
  }

  void initMenus() {
    state.menuItems = [
      HomeMenuItem(
        title: "主页",
        routeName: "/dashboard",
        iconData: Icons.home_outlined,
        isSelected: true,
      ),
      HomeMenuItem(
        title: "健康检测",
        routeName: "/medical",
        iconWidget: _buildImgIcon('diagnosisDisplay.png'),
      ),
      HomeMenuItem(
        title: "设置中心",
        routeName: "/settings",
        iconData: Icons.settings,
      ),
    ];
  }

  void updateMenus() {
    state.menuItems = [
      HomeMenuItem(
        title: "主页",
        routeName: "/dashboard",
        iconData: Icons.home_outlined,
        isSelected: true,
      ),
      HomeMenuItem(
        title: "健康档案",
        routeName: "/patient/detail",
        iconWidget: _buildImgIcon('healthRecord.png'),
      ),
      HomeMenuItem(
        title: "医生签约",
        routeName: "/contract/package_list",
        iconWidget: _buildImgIcon('doctorSigning.png'),
      ),
      HomeMenuItem(
        title: "健康体检",
        routeName: "/check/form",
        iconWidget: _buildImgIcon('healthCheckup.png'),
      ),
      HomeMenuItem(
        title: "人群随访",
        routeName: "/check/follow_up",
        iconWidget: _buildImgIcon('populationFollowUp.png'),
      ),
      HomeMenuItem(
        title: "健康检测",
        routeName: "/medical",
        iconWidget: _buildImgIcon('diagnosisDisplay.png'),
      ),
      HomeMenuItem(
        title: "设置中心",
        routeName: "/settings",
        iconData: Icons.settings,
      ),
    ];
  }

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