import 'dart:typed_data';
import 'dart:convert';

import 'package:chat/proto/chat.pbenum.dart';
import 'package:chat/utils/file_cache_mgr.dart';
import 'package:chat/utils/sql_util.dart';
import 'package:chat/data/group_data_mgr.dart';

class MsgState {
  static const int None = 0; //默认
  static const int UnUpload = 1; //未上传
  static const int Uploading = 2; //上传中
  static const int UploadFailed = 3; //上传失败
  static const int Uploaded = 4; //已上传
  static const int Sending = 5; //发送中
  static const int SendingFailed = 6; //发送失败,
  static const int SendingSuccess = 7; //发送成功
  static const int Downloading = 9; //下载中
  static const int DownloadFailed = 10; //下载失败
  static const int DownloadSuccess = 11; //下载成功
}

class MsgModel {
  int transTag; //翻译类型标记
  int channelType; //群聊还是会话
  int from;
  int friendId;
  int msgType;
  List<int> msgContent;
  int time; //消息的时间
  int sessionId; //会话id
  int readState; //0代表已显示
  String extraFile; //下载文件的识别码
  int extraInfo; //表示语音的长度信息
  List<int> translateContent; //腾讯翻译
  List<int> enTranslateContent; //google翻译
  String localFile; //文件的本地路径
  int state; //发送状态0默认,1代表未上传 2代表上传中，3代表已上传，4代表发送中,5发送失败，6发送成功
  int soundListened; //语音是否听过0没有，1有
  List<int> refMsgContent; //引用的内容
  List<int> altUsers; //@的用户

  MsgModel(this.from, this.friendId, this.msgType, this.msgContent, this.time,
      this.sessionId,
      {this.extraFile,
      this.extraInfo,
      this.transTag = 0,
      this.translateContent,
      this.enTranslateContent,
      this.channelType = 0,
      int readState = 0,
      this.localFile,
      this.state = MsgState.None,
      this.soundListened = 0,
      this.refMsgContent,
      this.altUsers});

  copy() {
    return MsgModel(this.from, this.friendId, this.msgType, this.msgContent,
        this.time, this.sessionId,
        extraFile: this.extraFile,
        transTag: this.transTag,
        translateContent: this.translateContent,
        enTranslateContent: this.enTranslateContent,
        extraInfo: this.extraInfo,
        readState: this.readState ?? 0,
        localFile: this.localFile,
        state: this.state,
        soundListened: this.soundListened,
        refMsgContent: this.refMsgContent,
        altUsers: this.altUsers);
  }

  //从本地数据库数据初始化消息体
  MsgModel.fromJson(Map<String, dynamic> json, [bool isGroup = false]) {
    transTag = json['transTag'];
    from = json['fromId'];
    friendId = json['friendId'];
    msgType = json['msgType'];
    msgContent = json['msgContent'] ?? Uint8List(0);
    time = json['time'];
    sessionId = json['sessionId'];
    readState = json['readState'] ?? 0;
    if (json['extraFile'] != '') {
      extraFile = json['extraFile'];
    }
    extraInfo = json['extraInfo'] ?? 0;

    if (json['translateContent'] != null) {
      translateContent = json['translateContent'];
    }
    if (json['enTranslateContent'] != null) {
      enTranslateContent = json['enTranslateContent'];
    }

    if (json['localFile'] != '') {
      localFile = FileCacheMgr.replacePath(json['localFile']);
    }

    soundListened = json['soundListened'];
    refMsgContent = json['refMsgContent'];
    altUsers = json['altUsers'];

    //群聊还是私聊
    if (isGroup) {
      channelType = 1;
    } else {
      channelType = 0;
    }

    state = json['msgState'];
  }

  updateSoundListened() {
    soundListened = 1;
    SqlUtil().updateSoundListened(this);
  }

  //人工付费翻译收费
  getNeedMoney() {
    int needMoney = 0;
    if (msgType == ChatType.TextChatType.value) {
      int length = utf8.decode(msgContent).length;
      needMoney = length ~/ 50 + 1; //每50个字耗费1H币

    } else if (msgType == ChatType.ShortVoiceChatType.value) {
      //没20秒扣除1Hb
      needMoney = extraInfo != null ? extraInfo ~/ 20000 + 1 : 0;
    }

    return needMoney;
  }

  //获得翻译的结果数组
  getTransTextList() {
    List<String> textList = [];
    if (translateContent != null && translateContent.length > 0) {
      try {
        var transTxt = utf8.decode(translateContent);
        if (transTxt.contains('[ ')) {
          transTxt = transTxt.replaceAll('[ ', '[');
        }
        if (transTxt.contains(' ]')) {
          transTxt = transTxt.replaceAll(' ]', ']');
        }

        print('翻译结果:$transTxt');

        if (altUsers != null && altUsers.length > 0) {
          var alterStr = '';
          for (var i = 0; i < altUsers.length; i++) {
            var name = GroupInfoMgr().getGroupFdName(sessionId, altUsers[i]);
            alterStr = '@$name $alterStr';
          }
          transTxt = '$alterStr $transTxt';
        }

        textList.add(transTxt);
      } catch (e) {
        var origin = utf8.decode(msgContent);
        print('译文异常，原文 $origin  发送人 $from  发送时间$time');
      }
    }

    //人工翻译之后就不需要这个字段了
    if (enTranslateContent != null &&
        enTranslateContent.length > 0 &&
        (transTag != 4 && transTag != 10)) {
      var enTransTxt = utf8.decode(enTranslateContent);

      if (enTransTxt.contains('[ ')) {
        enTransTxt = enTransTxt.replaceAll('[ ', '[');
      }
      if (enTransTxt.contains(' ]')) {
        enTransTxt = enTransTxt.replaceAll(' ]', ']');
      }

      if (altUsers != null && altUsers.length > 0) {
        var alterStr = '';
        for (var i = 0; i < altUsers.length; i++) {
          var name = GroupInfoMgr().getGroupFdName(sessionId, altUsers[i]);
          alterStr = '@$name $alterStr';
        }
        enTransTxt = '$alterStr $enTransTxt';
      }

      textList.add(enTransTxt);
    }

    if (msgType == ChatType.TextChatType.value) {
      if (msgContent != null && msgContent.length > 0) {
        var text = utf8.decode(msgContent);
        textList.add(text);
      }
    }

    return textList;
  }
}
