import 'package:fis_common/index.dart';
import 'package:fis_jsonrpc/services/appletAPI.m.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/router/mobile/names.dart';
import 'package:flyinsono/architecture/states/states.dart';
import 'package:flyinsono/rpc.dart';
import 'package:get/get.dart';

import 'index.dart';

class WxAccountLinkingController extends GetxController {
  WxAccountLinkingController();

  final state = WxAccountLinkingState();

  final TextEditingController accountController = TextEditingController();
  final TextEditingController passwordController = TextEditingController();

  // tap
  void bindAccount() async {
    print("当前的 openid 为: ${Store.user.openid}");
    print("当前的 账号 为: ${accountController.text}");
    print("当前的 密码 为: ${passwordController.text}");
    if (Store.user.openid.isNullOrWhiteSpace) {
      Get.snackbar("绑定失败", "无微信账号信息",
          duration: Duration(seconds: 2),
          margin: EdgeInsets.only(top: 10),
          colorText: Color.fromARGB(255, 77, 77, 77));
      return;
    }
    bool isBindSuccess = await rpc.appletAPI.bindVcloudAccountAndOpenIdAsync(
        BindVcloudAccountAndOpenIdRequest(
            openId: Store.user.openid,
            account: accountController.text,
            password: passwordController.text));
    print("绑定结果: 成功？ $isBindSuccess");
    if (isBindSuccess) {
      Store.user.setToken(Store.user.openid);
      Get.snackbar("通知", "绑定成功",
          duration: Duration(seconds: 2),
          margin: EdgeInsets.only(top: 10),
          colorText: Color.fromARGB(255, 77, 77, 77));
      Get.offNamed(MobileRouteNames.Wechat.MiniProgramHome);
    } else {
      Get.snackbar("通知", "绑定失败",
          duration: Duration(seconds: 2),
          margin: EdgeInsets.only(top: 10),
          colorText: Color.fromARGB(255, 77, 77, 77));
      Store.user.setToken("");
    }
  }

  void reBindAccount() {
    Get.snackbar("通知", "暂不支持重新绑定账号",
        duration: Duration(seconds: 2),
        margin: EdgeInsets.only(top: 10),
        colorText: Color.fromARGB(255, 77, 77, 77));
  }

  Future<bool> checkUserInfoByOpenId(String openId) async {
    print("根据openid获取用户信息 当前的 openid 为: $openId");
    GetUserInfoByOpenIdResult result = await rpc.appletAPI
        .getUserInfoByOpenIdAsync(GetUserInfoByOpenIdRequest(openId: openId));
    if (result.nickName == null) {
      Get.snackbar("无账号信息", "请先绑定账号",
          duration: Duration(seconds: 2),
          margin: EdgeInsets.only(top: 10),
          colorText: Color.fromARGB(255, 77, 77, 77));
      return false;
    }
    print(
        "根据openid获取用户信息 获取到的信息为: 用户名${result.nickName} 医院${result.hospital} 用户id ${result.userId}");
    Store.user.handleWechatLogin(
        result.nickName ?? '', result.hospital ?? '', result.userId ?? '');
    return true;
  }

  /// 在 widget 内存中分配后立即调用。
  @override
  void onInit() {
    checkUserInfoByOpenId(Store.user.openid);
    state.isBinded = Store.user.token.isNotEmpty;
    state.isShowBindUI = !state.isBinded;
    super.onInit();
  }

  /// 在 onInit() 之后调用 1 帧。这是进入的理想场所
  @override
  void onReady() {
    super.onReady();
  }

  /// 在 [onDelete] 方法之前调用。
  @override
  void onClose() {
    super.onClose();
  }

  /// dispose 释放内存
  @override
  void dispose() {
    super.dispose();
  }
}
