/*
 * @Author: XianKaiQun
 * @Date: 2020-07-23 16:00:59
 * @LastEditors  : WuWei
 * @LastEditTime : 2023-07-07 10:09:49
 * @Description: 
 */
import 'dart:io';

import 'package:app_package/app_package.dart';
import 'package:app_package/src/api/message.dart';
import 'package:app_package/src/api/personal.dart';
import 'package:app_package/src/api/root.dart';
import 'package:app_package/src/models/current_userinfo.dart';
import 'package:app_package/src/models/msg_count.dart';
import 'package:flutter/material.dart';
import 'package:wisdom_cli/wisdom_cli.dart';

class PrivateMineStore with ChangeNotifier {
  final BuildContext context, providerContext;
  Map<String, dynamic>? routingArgs;
  PrivateMineStore(this.context, this.providerContext) {
    routingArgs = (ModalRoute.of(context)?.settings.arguments ??
            new Map<String, dynamic>()) as Map<String, dynamic>? ??
        {};
  }
  final appGlobalStore = GetIt.I.get<AppGlobalStore>();
  CurrentUserinfoModel? currentUserinfo;
  List<MsgCountModel> msgCountList = [];

  String version = '';  

  ///未读消息数   [null]可以当做正在加载中，不显示
  int? unread() {
    if ((msgCountList.length) == 0) {
      return null;
    }
    var item = msgCountList.firstWhere((e) => e.msgType == 'myUnread');
    return item.count as int?;
  }

  ///未完成任务数   [null]可以当做正在加载中，不显示
  int? taskMsg() {
    if ((msgCountList.length) == 0) {
      return null;
    }
    var item = msgCountList.firstWhere((e) => e.msgType == 'myTaskUnfinish');
    return item.count as int?;
  }

    
    

  Future getData() async {
    version = await AppPackageInfo.instance.getPackageInfoVersion();
    notifyListeners();
    await Future.wait([
      getMsgcount(),
      getUserInfo(),
    ]);
  }

  ///获取当前登录信息
  Future getUserInfo() async {
    var res = await PersonalApi.getCurrentUserinfo();
    currentUserinfo = res.data;
    appGlobalStore.setCurUserInfo(res.data);
    notifyListeners();
  }

  ///打电话给客服
  callService(String phone) async {
    var url = 'tel:$phone';
    launch(url);
  }

  ///删除微信绑定
  Future deleteWeChatBind(
      EmployeeThirdpartyAuthorizesOfCurrentUserinfoModel item) async {
    final confirm = await WModalUtil.showSync('确认删除“${item.nickname}”的微信绑定？');
    if (confirm!) {
      WLoadingUtil.show();
      try {
        await PersonalApi.postUnbind({'openId': item.openId});
        await getUserInfo();
      } finally {
        WLoadingUtil.hide();
      }
    }
  }

  ///获取未读消息数
  Future getMsgcount() async {
    var res = await MessageApi.getMsgCount();
    msgCountList = res.data ?? [];
    notifyListeners();
  }

  ///我的任务
  Future route2task() async {
    Navigator.pushNamed(context, 'task/index',
        arguments: {'appBarTitle': '我的任务'});
  }

  ///我的消息
  Future route2notice() async {
    Navigator.pushNamed(context, 'notice', arguments: {'appBarTitle': '我的消息'});
  }

  String? versionDetail;

  /// 获取版本信息内容
  Future<void> getAppInfo() async {
    var res = await RootApi.getTypeAndVersion({
      'platformType': Platform.isAndroid ? 'android' : 'ios',
      'version': await AppPackageInfo.instance.getPackageInfoVersion(),
    });
    versionDetail = res;
    notifyListeners();
  }

  ///退出登录
  Future<void> logout() async {
    final boolean = await WModalUtil.showSync('确认退出登录？');
    if (!boolean!) return;
    final authGlobalStore = GetIt.I.get<AuthGlobalStore>();
    await authGlobalStore.logout();
  }

  /// 版本介绍
  Future route2version() async {
    Navigator.pushNamed(context, 'version_detail');
  }
}
