import 'dart:ui';

import 'package:electronic_assistant/base/base_controller.dart';
import 'package:electronic_assistant/data/repositories/account_repository.dart';
import 'package:electronic_assistant/data/repositories/config_repository.dart';
import 'package:electronic_assistant/module/chat/view.dart';
import 'package:electronic_assistant/module/record/record_handler.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:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';

import '../../data/consts/constants.dart';
import '../../dialog/show_update_version_dialog.dart';
import '../../router/app_pages.dart';
import '../../utils/desktop_shortcut_utils.dart';
import '../../utils/mmkv_util.dart';
import '../login/view.dart';

class MainController extends BaseController {
  final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();

  final List<TabBean> tabBeans = [
    TabBean(
        StringName.mainTabHome,
        Assets.images.mainTabHomeUnSelect.path,
        Assets.images.mainTabHomeSelected.path,
        "#969696".toColor(),
        ColorName.primaryTextColor),
    TabBean(
        StringName.mainTabFile,
        Assets.images.mainTabFileUnSelect.path,
        Assets.images.mainTabFileSelected.path,
        "#969696".toColor(),
        ColorName.primaryTextColor),
  ];

  final _currentIndex = 0.obs;

  int get currentIndex => _currentIndex.value;

  DateTime? _lastPressedAt; // 上次点击时间

  DateTime? get lastPressedAt => _lastPressedAt;

  void changeIndex(int index) {
    _currentIndex.value = index;
  }

  void updateIndex(int index) {
    _currentIndex.value = index;
  }

  void updateIndexByPageName(String pageName) {
    for (var value in tabBeans) {
      if (value.title == pageName) {
        _currentIndex.value = tabBeans.indexOf(value);
        break;
      }
    }
  }

  void openDrawer() {
    scaffoldKey.currentState?.openDrawer();
  }

  void closeDrawer() {
    scaffoldKey.currentState?.closeDrawer();
  }

  void onChatClick() {
    if (accountRepository.isLogin.value) {
      ChatPage.start(ChatFromType.fromMain);
    } else {
      LoginPage.start(fromType: LoginFromType.aiChat);
    }
  }

  void setLastPressedAt(lastPressedAt) {
    _lastPressedAt = lastPressedAt;
  }

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

  @override
  void onReady() {
    super.onReady();
    _initParameters();
    checkVersion();
  }

  void _initParameters() {
    if (parameters == null) {
      return;
    }
    String? action = parameters?[LaunchAction.key];
    if (action == LaunchAction.recordAudioAction) {
      Get.toNamed(RoutePath.record);
    }
  }

  void checkVersion() {
    configRepository.getAppVersion().then((bean) {
      if (bean == null) {
        return;
      }
      //检查是否已经弹出过更新弹窗
      if (KVUtil.getString(Constants.recordServerVersion, null) ==
          bean.version) {
        return;
      }
      UpdateVersionDialog.show(bean);
    });
  }

  void exit() async {
    await recordHandler.stopRecord(isStopService: true);
    SystemNavigator.pop();
  }
}

class TabBean {
  final String title;
  final String normalIcon;
  final String selectedIcon;
  final Color txtNormalColor;
  final Color txtSelectedColor;

  TabBean(this.title, this.normalIcon, this.selectedIcon, this.txtNormalColor,
      this.txtSelectedColor);
}
