
import 'package:chat/data/UserData.dart';
import 'package:chat/models/ChatMsg.dart';
import 'package:chat/proto/all.pbserver.dart';

class LastMessageModel {
  int channelType;
  int friendId;
  int sessionId;
  int fromId;
  int updateAt;
  ChatType type;
  List<int> msgContent;

  LastMessageModel(
      {this.sessionId,
      this.friendId,
      this.fromId,
      this.updateAt,
      this.type,
      this.msgContent,
      this.channelType = 0});

  LastMessageModel.fromChatMsg(MsgModel msgModel) {
//    print('### gg msgModel from:${msgModel.from}  msgModel.friendId ${ msgModel.friendId}  msgModel.sessionId ${msgModel.sessionId} msgtype ${msgModel.msgType}');
    sessionId = msgModel.sessionId;
    fromId = msgModel.from;
    if (msgModel.from == UserData().basicInfo.userId) {
      //自己
      friendId = msgModel.friendId;
    } else {
      friendId = msgModel.from;
    }

    ///翻译管家
    if(msgModel.channelType==ChatChannelType.Session.value && msgModel.msgType==ChatType.GroupChatNoticeType.value){
      friendId =  msgModel.friendId;
    }


    channelType = msgModel.channelType;

    type = ChatType.valueOf(msgModel.msgType);
    msgContent = msgModel.msgContent;
    if (type == ChatType.TextChatType) {
      if (msgModel.translateContent != null &&
          msgModel.translateContent.length > 0) {
        msgContent = msgModel.translateContent;
      }
    }

    updateAt = msgModel.time;
  }

  updateWithMessage(MsgModel msgModel) {
    fromId = msgModel.from;

    type = ChatType.valueOf(msgModel.msgType);
    msgContent = msgModel.msgContent;
    if (type == ChatType.TextChatType) {
      if (msgModel.translateContent != null &&
          msgModel.translateContent.length > 0) {
        msgContent = msgModel.translateContent;
      }
    }

    updateAt = msgModel.time;
  }

  LastMessageModel.fromUnreadMsg(UserUnreadMsgNotice notice) {
    var lastMsg = notice.lastUnreadMsg;

    sessionId = notice.targetId;
    fromId = lastMsg.sendUserId;
    friendId = fromId;

    channelType = notice.channelType.value;

    print('未读$sessionId from：$fromId');

    type = lastMsg.cType;
    msgContent = lastMsg.contentBuff;
    if (lastMsg.hasTencentTranslate()) {
      msgContent = lastMsg.tencentTranslate;
    }

    ///解析红包消息
    if(lastMsg.cType== ChatType.RedWalletChatType){
      RedWallet wallet = RedWallet.fromBuffer(lastMsg.contentBuff);
      var myId = UserData().basicInfo.userId;

      if (myId == wallet.suId) {
        friendId = wallet.tuId;
      } else {
        friendId = wallet.suId;
      }
    }else if(lastMsg.cType == ChatType.GroupChatNoticeType){
      GroupChatNotice notice = GroupChatNotice.fromBuffer(lastMsg.contentBuff);

      var optId = notice.operatuId;
      print('### gourp add optid ${optId.id}');
      List<BaseUserInfo> optedIds = notice.operateduId;
      for(int k=0;k<optedIds.length;k++){
        print('### gourp add optedIds ${optedIds[k].id}');
      }

    }



    updateAt = lastMsg.sendTime.toInt();
  }

  updateWithUnreadMsg(UserUnreadMsgNotice notice) {
    var lastMsg = notice.lastUnreadMsg;

    fromId = lastMsg.sendUserId;
    type = lastMsg.cType;
    msgContent = lastMsg.contentBuff;
    if (lastMsg.hasTencentTranslate()) {
      msgContent = lastMsg.tencentTranslate;
    }

    ///解析红包消息
    if(lastMsg.cType== ChatType.RedWalletChatType){
      RedWallet wallet = RedWallet.fromBuffer(lastMsg.contentBuff);
      var myId = UserData().basicInfo.userId;

      if (myId == wallet.suId) {
        friendId = wallet.tuId;
      } else {
        friendId = wallet.suId;
      }
    }

    updateAt = lastMsg.sendTime.toInt();
  }
}
