import 'package:fis_jsonrpc/rpc.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:vitalapp/architecture/utils/prompt_box.dart';
import 'package:vitalapp/components/dialog_confirm.dart';
import 'package:vitalapp/managers/interfaces/account.dart';
import 'package:vitalapp/store/store.dart';
import 'package:fis_common/logger/logger.dart';

class JsonRpcExInterceptor extends JsonRpcInterceptor {
  static final _reloginNeededCodes = [1, 2, 3, 9013];
  static bool _isDisplayingAlertRelogin = false;
  @override
  Future<JsonRpcNetworkException> onHttpRequestError(
      JsonRpcNetworkException error) async {
    logger.e("JsonRpcExInterceptor-HttpRequestError", error);
    PromptBox.toast("网络请求错误");
    return super.onHttpRequestError(error);
  }

  @override
  Future<JsonRpcServerError> onResponseError(JsonRpcServerError error) async {
    if (error.code == null) {
      return error;
    }
    final code = error.code!;
    try {
      EasyLoading.dismiss(animation: false);
      //已在其他设备登录
      if (_reloginNeededCodes.contains(code)) {
        if (_isDisplayingAlertRelogin) {
          return error;
        }
        if (Store.user.isLogOn && !_isDisplayingAlertRelogin) {
          _isDisplayingAlertRelogin = true;
          await _alertRelogin(code);
          _isDisplayingAlertRelogin = false;
        }
        return error;
      }

      if (error.code != null && error.code != 1 && error.code != 2) {
        String? text;
        if (code == -1) {
          text = errorCodes['errorCodeUN'];
        } else {
          text = errorCodes['errorCode${error.code}'];
        }
        logger.e(text ?? '');
        PromptBox.toast(text ?? "Unkonwn RPC Exception");
      }
    } catch (e) {
      // PromptBox.toast("${i18nBook.errorCodes.errorCodeUN.t}[$errorCode]");
      logger.e('JsonRpcExInterceptor onResponseError', e);
    }
    return error;
  }

  Future<void> _alertRelogin(int code) async {
    // 先自动登出账号，防止未点确认就中止进程
    final result = await Get.find<IAccountManager>().logout();
    logger.i('JsonRpcExInterceptor logout:$result');

    PromptBox.dismiss(); // 清除toast提示

    final message = errorCodes["errorCode$code"]!;
    await DialogConfirm.show(
      message: message,
      disableCancel: true,
    );

    Get.offAllNamed("/login");
  }

  /// 翻译错误码
  static String translateErrorCode(int code) {
    final message = errorCodes["errorCode$code"] ?? "未知错误";
    return message;
  }

  static final errorCodes = {
    "errorCodeUN": "未知错误",
    "errorCode0": "操作成功",
    "errorCode1": "登录已过期，请重新登录",
    "errorCode2": "登录信息已过期，请重新登录",
    "errorCode3": "登录信息过旧，请重新登录",
    "errorCode4": "用户IP在黑名单，禁止访问",
    "errorCode5": "您尚无权限操作，请联系医院负责人",
    "errorCode6": "上传检查图像失败，文件地址为空",
    "errorCode7": "上传文件失败，文件名为空",
    "errorCode8": "查询错误，查询信息不完善",
    "errorCode9": "查询错误，查询信息不完善",
    "errorCode10": "系统异常错误",
    "errorCode11": "验证码为空",
    "errorCode12": "验证码已过期",
    "errorCode13": "错误的验证码",
    "errorCode14": "不支持的Http请求方式",
    "errorCode15": "下载失败",
    "errorCode16": "下载失败，文件不存在",
    "errorCode17": "上传文件失败，文件内容为空",
    "errorCode18": "接口文档错误，文件不存在",
    "errorCode19": "上传文件失败，错误的文件格式",
    "errorCode800": "会诊不存在或已结束",
    "errorCode802": "申请已被处理，请勿重复操作",
    "errorCode805": "会诊不存在",
    "errorCode806": "设备不在线",
    "errorCode807": "专家不在线",
    "errorCode809": "专家正忙",
    "errorCode811": "会诊已取消",
    "errorCode812": "会诊已超时",
    "errorCode813": "会诊参与人员不存在",
    "errorCode816": "居民已存在，请勿重复添加",
    "errorCode824": "预约记录不存在",
    "errorCode825": "会诊医院不匹配",
    "errorCode827": "保存会诊记录失败",
    "errorCode830": "保存会诊发起人失败",
    "errorCode831": "申请方不在线",
    "errorCode832": "不是会诊发起方",
    "errorCode833": "会诊已结束",
    "errorCode834": "预约记录未审批",
    "errorCode835": "会诊不在进行中",
    "errorCode838": "急诊编号生成失败",
    "errorCode844": "邀请方不在线",
    "errorCode845": "邀请方忙线中",
    "errorCode847": "急诊专家为空",
    "errorCode851": "已在其他设备加入会诊",
    "errorCode852": "申请方正忙",
    "errorCode853": "急诊设备不存在",
    "errorCode854": "急诊设备未绑定",
    "errorCode858": "急诊设备未找到",
    "errorCode859": "急诊专家未找到",
    "errorCode860": "设备未开启直播",
    "errorCode861": "当前设备正在会诊或者急诊中",
    "errorCode2000": "未查找到设备",
    "errorCode2001": "该设备已被绑定，请勿重复添加",
    "errorCode2002": "添加或迁移设备到医院，未填写设备编码",
    "errorCode2003": "该设备已被绑定，请勿重复添加",
    "errorCode2004": "设备未绑定医院",
    "errorCode2005": "未选择下发设备",
    "errorCode2006": "AI应用code为空",
    "errorCode2007": "设备连接云服务失败",
    "errorCode2008": "设备连接云服务失败",
    "errorCode2009": "设备连接云服务失败",
    "errorCode2010": "设备连接云服务失败",
    "errorCode2011": "设备连接云服务失败",
    "errorCode2012": "未选择下发设备",
    "errorCode2013": "设备编码为空",
    "errorCode2014": "移除失败",
    "errorCode2015": "未查找到设备",
    "errorCode2016": "未填写设备名称",
    "errorCode2017": "设备连接云服务失败",
    "errorCode2018": "设备连接云服务失败",
    "errorCode2026": "该设备已经绑定过其他医院！请联系管理员解绑。",
    "errorCode2032": "用户未加入医院",
    "errorCode2033": "该设备归属其他科室，暂时无法添加",
    "errorCode2037": "设备描述不能为空",
    "errorCode3000": "管理端医院不能选择自己的子级医院作为父级医院",
    "errorCode3001": "管理员密码为空",
    "errorCode3002": "管理员未找到",
    "errorCode3003": "名称必传",
    "errorCode3004": "管理员Code必传",
    "errorCode3005": "管理员名称已存在",
    "errorCode3006": "管理员查询类型有误",
    "errorCode3007": "管理员Code未找到",
    "errorCode3008": "医院管理员删除失败",
    "errorCode3009": "用户code必传",
    "errorCode3010": "密码必传",
    "errorCode3011": "角色必传",
    "errorCode3012": "角色不存在",
    "errorCode3013": "没有选择医院则无法选择角色",
    "errorCode3014": "认证记录Code必传",
    "errorCode3015": "唯一码必传",
    "errorCode3016": "唯一码重复",
    "errorCode3017": "权限未找到",
    "errorCode3018": "选择自身作为上级",
    "errorCode3019": "权限必传",
    "errorCode3020": "角色未找到",
    "errorCode3021": "超级管理员角色不可操作",
    "errorCode3022": "设备未找到",
    "errorCode3023": "设备Code必传",
    "errorCode3024": "设备类型必传",
    "errorCode3025": "设备类型不能重复",
    "errorCode3026": "设备类型已绑定设备",
    "errorCode3027": "上级医院不存在",
    "errorCode3028": "医院不存在",
    "errorCode3029": "虚拟医院不能操作",
    "errorCode3030": "用户未找到",
    "errorCode3031": "词库Code或词条名称为空",
    "errorCode3032": "编辑词条失败",
    "errorCode3033": "词库Code或词条Code为空",
    "errorCode3034": "结论或描述为空",
    "errorCode3035": "编辑词条内容失败",
    "errorCode3036": "词条Code或父级Code为空",
    "errorCode3037": "Json格式不正确",
    "errorCode3038": "版本号格式不正确",
    "errorCode3039": "名称不可删除",
    "errorCode3040": "手机号不可删除",
    "errorCode3041": "基础信息不可删除",
    "errorCode3042": "密码错误",
    "errorCode3043": "Code为空",
    "errorCode3045": "已经是最新版本",
    "errorCode3047": "未配置模板",
    "errorCode3050": "词条库名称重复，请重新输入",
    "errorCode3051": "模版名称重复",
    "errorCode3999": "系统错误",
    "errorCode4000": "导出失败，请先选择需导出的居民",
    "errorCode4001": "您尚无权限操作",
    "errorCode4002": "检查已结束，无法继续操作",
    "errorCode4003": "转诊失败，系统不支持转诊给申请人",
    "errorCode4004": "该转诊已超时",
    "errorCode4005": "存在尚未结束的扫查，请先结束扫查再操作",
    "errorCode4006": "不支持转诊至该医院",
    "errorCode4007": "本医院中未找到该居民",
    "errorCode4008": "本医院中不存在该设备",
    "errorCode4009": "该关联操作不属于同一设备，请先检查要关联的操作设备是否一致",
    "errorCode4010": "请先同意再继续",
    "errorCode4011": "转诊居民需转为已登记状态",
    "errorCode4012": "性别填写有误",
    "errorCode4013": "设备未绑定医院",
    "errorCode4014": "居民code不存在",
    "errorCode4015": "检查code不存在",
    "errorCode4016": "检查code不存在",
    "errorCode4017": "设备中不存在该检查",
    "errorCode4018": "请先完善个人信息再操作",
    "errorCode4019": "检查code不存在",
    "errorCode4020": "未完成扫查，请先扫查再操作",
    "errorCode4021": "操作失败，转诊单已被执行其他操作",
    "errorCode4022": "开始时间不能晚于结束时间",
    "errorCode4023": "请先完善个人信息再操作",
    "errorCode4024": "转诊code不存在",
    "errorCode4025": "病例信息未修改，请修改完再进行保存",
    "errorCode4026": "检查code不存在",
    "errorCode4027": "孕龄未填写",
    "errorCode4028": "该检查无图像信息",
    "errorCode4029": "扫查应用为空",
    "errorCode4030": "扫查应用类别为空",
    "errorCode4031": "未选择测量模式",
    "errorCode4032": "查询必填",
    "errorCode4033": "请填写注释内容",
    "errorCode4034": "请先选择并设置测量项",
    "errorCode4035": "请设置测量样式",
    "errorCode4036": "未设置服务对象",
    "errorCode4037": "检查code不存在",
    "errorCode4038": "分页查找转诊居民检查列表，不在一家医院",
    "errorCode4039": "未选择起止时间",
    "errorCode4040": "计算方式传值有误",
    "errorCode4041": "服务对象错误",
    "errorCode4042": "登记状态错误",
    "errorCode4043": "无效版本号",
    "errorCode4044": "当前版本不支持",
    "errorCode4045": "用户不存在",
    "errorCode4046": "居民code不能为空",
    "errorCode4049": "无权操作此转诊单",
    "errorCode4050": "该居民下无检查记录",
    "errorCode4056": "未找到可导出的数据",
    "errorCode5000": "检查code不存在",
    "errorCode5001": "检查记录不存在",
    "errorCode5002": "报告模板内容为空",
    "errorCode5003": "请先填写报告",
    "errorCode5004": "请填写手机号",
    "errorCode5005": "报告编码不能为空",
    "errorCode5006": "未设置报告分享地址",
    "errorCode5007": "未设置报告分享地址",
    "errorCode5008": "未配置短信模板",
    "errorCode5009": "分享失败",
    "errorCode5010": "请填写分享邮箱",
    "errorCode5011": "报告模板code不存在",
    "errorCode5012": "消息地址为空",
    "errorCode5014": "创建报告失败",
    "errorCode5015": "转诊居民不存在",
    "errorCode5016": "转诊居民未受理",
    "errorCode5017": "已有同名注释",
    "errorCode6000": "手机号码不正确",
    "errorCode6001": "请填写居民姓名",
    "errorCode6002": "请填写手机号",
    "errorCode6003": "居民姓名限制20个字",
    "errorCode6004": "请填写正确的手机号",
    "errorCode6005": "用户不存在",
    "errorCode6006": "居民已存在，请勿重复添加",
    "errorCode6007": "您尚无权限操作，请联系医院负责人",
    "errorCode6008": "请先完善个人信息再操作",
    "errorCode6009": "用户不存在",
    "errorCode6010": "不支持申请的角色",
    "errorCode6011": "请完善申请信息",
    "errorCode6012": "请完善申请信息",
    "errorCode6013": "您尚无权限操作，请联系医院负责人",
    "errorCode6014": "请先完善个人信息再操作",
    "errorCode6015": "请上传头像",
    "errorCode6016": "不支持申请的角色",
    "errorCode6017": "请填写医院名称",
    "errorCode6018": "医院code不存在",
    "errorCode6019": "岗位code不存在",
    "errorCode6020": "请输入岗位名称",
    "errorCode6021": "适用科室编码不存在",
    "errorCode6022": "职称code不存在",
    "errorCode6023": "请输入职称",
    "errorCode6024": "错误的邮箱格式",
    "errorCode6025": "请填写登录密码",
    "errorCode6026": "账号不存在",
    "errorCode6027": "用户不存在",
    "errorCode6028": "请输入邮箱",
    "errorCode6029": "未注册的用户",
    "errorCode6030": "请输入手机号",
    "errorCode6031": "未注册的用户",
    "errorCode6032": "手机号不存在",
    "errorCode6033": "邮箱不存在",
    "errorCode6034": "验证码错误",
    "errorCode6035": "修改密码，当前操作人",
    "errorCode6036": "请输入正确的密码",
    "errorCode6037": "该账号尚未注册",
    "errorCode6038": "请输入密码",
    "errorCode6039": "错误的地区信息",
    "errorCode6040": "未设置地区信息",
    "errorCode6041": "当前版本不支持",
    "errorCode6042": "用户不存在",
    "errorCode6043": "该用户已认证",
    "errorCode6044": "账号已存在，请直接登录",
    "errorCode6045": "账号已存在，请直接登录",
    "errorCode6046": "账号已存在，请直接登录",
    "errorCode6047": "手机号已绑定其他账号!",
    "errorCode6048": "该邮箱已绑定用户",
    "errorCode6049": "账号不能为空",
    "errorCode6050": "账号格式不符合规则",
    "errorCode6051": "请输入正确的验证码",
    "errorCode6053": "原密码不正确",
    "errorCode6055": "职位名称重复，请重新输入",
    "errorCode6056": "科室名称重复，请重新输入",
    "errorCode6057": "头像图片地址不合法",
    "errorCode6058": "昵称太长",
    "errorCode6062": "账号已锁定，次日即可自动解锁",
    "errorCode6064": "用户密码与历史密码重复，请重新输入",
    "errorCode6066": "语音识别错误",
    "errorCode6070": "超过最大用户数量限制",
    "errorCode6071": "超过最大设备数量限制",
    "errorCode6072": "服务器已经被禁用",
    "errorCode6077": "未设置所属医院",
    "errorCode6078": "对接医院不能是上级医院",
    "errorCode7001": "课程编码为空",
    "errorCode7002": "老师编码为空",
    "errorCode7003": "老师信息未找到",
    "errorCode7004": "课程未找到",
    "errorCode7005": "已审核状态不能删除课程",
    "errorCode7006": "已审核状态不能更新课程",
    "errorCode7009": "最大人数不能为0",
    "errorCode7010": "用户组名称为空",
    "errorCode7013": "班级学员列表为空",
    "errorCode7014": "用户组编码为空",
    "errorCode7015": "视频名称为空",
    "errorCode7016": "视频地址为空",
    "errorCode7017": "视频编码为空",
    "errorCode7019": "直播老师不存在",
    "errorCode7021": "课程正在直播中",
    "errorCode7022": "老师不在线",
    "errorCode7023": "学生不在线",
    "errorCode7024": "课程未审核",
    "errorCode7025": "试卷未找到",
    "errorCode7026": "试卷编码未空",
    "errorCode7027": "学生答题数据未找到",
    "errorCode7029": "直播课程未找到",
    "errorCode7030": "课程不在直播中",
    "errorCode7031": "课程已结束",
    "errorCode7032": "课程DB处理错误",
    "errorCode7040": "学生未支付",
    "errorCode7044": "报告已提交，请勿重复提交",
    "errorCode7046": "更新包已存在，请更改或删除更新包名",
    "errorCode7050": "编码不能为空",
    "errorCode7052": "描述过长",
    "errorCode7056": "当前设备已被占用",
    "errorCode7058": "用户已经在其他设备使用远程连接",
    "errorCode7059": "学生编码为空",
    "errorCode7063": "专辑名称为空",
    "errorCode7064": "无法作为学员加入自己创建的班级",
    "errorCode7065": "用户组名称不能重复",
    "errorCode7067": "没有超声机分享入口",
    "errorCode7076": "学生已在班级中",
    "errorCode7077": "用户所属机构和设备所属机构不在同一组织下",
    "errorCode7079": "设备不支持",
    "errorCode9001": "当前账号未设置团队，无法保存数据",
    "errorCode9011": "当前版本为最新版本",
    "errorCode9013": "当前账号已在其他设备登录",
  };
}
