import 'dart:async';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:vnoteapp/architecture/defines.dart';
import 'package:vnoteapp/architecture/utils/prompt_box.dart';
import 'package:vnoteapp/managers/interfaces/account.dart';
import 'package:vnoteapp/pages/controllers/home_nav_mixin.dart';
import 'package:vnoteapp/pages/home/models/menu.dart';
import 'package:vnoteapp/store/store.dart';

import 'state.dart';

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

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

  /// 切换活动菜单
  void switchActiveMenu(HomeMenuItem data) {
    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) return;

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

    Get.offAllNamed(name, id: 1001);
  }

  @override
  void onReady() {
    _initMenus();
    super.onReady();
  }

  void _initMenus() {
    state.menuItems = [
      HomeMenuItem(
        title: "主页",
        routeName: "/dashboard",
        iconData: Icons.home_outlined,
        isSelected: true,
      ),
      HomeMenuItem(
        title: "医生签约",
        routeName: "/patient/create",
        iconData: Icons.library_books_outlined,
      ),
      HomeMenuItem(
        title: "档案管理",
        routeName: "/patient/list",
        iconData: Icons.storage,
      ),
      HomeMenuItem(
        title: "设置中心",
        routeName: "/settings",
        iconData: Icons.settings,
      ),
      HomeMenuItem(
        title: "虚拟菜单01",
        routeName: "/mock01",
        iconData: Icons.flood,
      ),
      HomeMenuItem(
        title: "虚拟菜单02",
        routeName: "/mock02",
        iconData: Icons.flag,
      ),
      HomeMenuItem(
        title: "虚拟菜单03",
        routeName: "/mock03",
        iconData: Icons.run_circle,
      ),
    ];
  }
}
