import 'package:chat/models/ChatMsg.dart';
import 'package:chat/models/group_info_model.dart';
import 'package:chat/proto/all.pbserver.dart';
import 'package:chat/utils/chat_data_table.dart';
import 'package:chat/utils/conversation_table.dart';
import 'package:chat/utils/group_chat_table.dart';
import 'package:chat/utils/group_info_table.dart';
import 'package:chat/utils/group_member_table.dart';
import 'package:chat/utils/user_info_table.dart';
import 'package:sqflite/sqflite.dart';
import 'package:path/path.dart';

class SqlUtil {
  static const _VERSION = 1;

  static const _DbName = "chat.db";

  static Database _database;

  static FriendTableModel friendTableModel; //好友列表
  static ChatDataTableModel chatDataTableModel; //私聊数据
  static GroupInfoTableModel groupInfoTableModel; //群聊信息
  static GroupChatTableModel groupChatTableModel; //群聊聊天数据
  static GroupMemberTableModel groupMemberTableModel; //群聊成员数据
  static UserInfoTable userInfoTable;

  ///初始化
  static init() async {
    print('初始化数据库');
    var databasesPath = await getDatabasesPath();

    String path = join(databasesPath, _DbName);
    print(path);
    try {
      _database = await openDatabase(path, version: _VERSION);
      friendTableModel = FriendTableModel();
      chatDataTableModel = ChatDataTableModel();
      groupChatTableModel = GroupChatTableModel();
      groupInfoTableModel = GroupInfoTableModel();
      groupMemberTableModel = GroupMemberTableModel();

      userInfoTable = UserInfoTable();

      await friendTableModel.createTable();
      await chatDataTableModel.createTable();
      await groupChatTableModel.createTable();
      await groupInfoTableModel.createTable();
      await groupMemberTableModel.createTable();
      await userInfoTable.createTable();
    } catch (e) {
      print(e);
    }
  }

  static Future<List<FriendModel>> getFriendList() {
    return friendTableModel.getAllConversation();
  }

  ///获取当前数据库对象
  static Database getCurrentDatabase() {
    return _database;
  }

  //查询未读消息条数
  static Future<Map<int, int>> getUnreadCount(bool isGroup) async {
    if (isGroup) {
      return groupChatTableModel.getUnreadCount();
    } else {
      return chatDataTableModel.getUnreadCount();
    }
  }

  //查询记录
  static Future<Map<int, List<MsgModel>>> getAllRecord(
      {bool isGroup = false}) async {
    if (isGroup) {
      return groupChatTableModel.getAllRecord();
    } else {
      return chatDataTableModel.getAllRecord();
    }
  }

  //获取群的数量
  static Future<int> getGroupCount() async {
    return groupInfoTableModel.getGroupCount();
  }

  //获取所有群信息
  static Future<List<GroupInfoModel>> getAllGroupInfo() async {
    return groupInfoTableModel.getGroupList();
  }

  ///插入到数据库
  static Future insert(MsgModel msgModel) async {
    if (msgModel.channelType == ChatChannelType.Group.value) {
      groupChatTableModel.insert(msgModel);
    } else {
      chatDataTableModel.insert(msgModel);
    }
  }

  //更新机器翻译内容
  updateMachineTranslateContent(int session, int sendTime,
      List<int> transContent, List<int> enTransContent, int tranState,
      {bool isGroup = false}) async {
    if (isGroup) {
      groupChatTableModel.updateMachineTranslateContent(
          session, sendTime, transContent, enTransContent, tranState);
    } else {
      chatDataTableModel.updateMachineTranslateContent(
          session, sendTime, transContent, enTransContent, tranState);
    }
  }

  //更新人工翻译内容
  updateUserTranslateContent(
      int session, int sendTime, List<int> transContent, int tranState) async {
    chatDataTableModel.updateUserTranslateContent(
        session, sendTime, transContent, tranState);
  }

  //更新人工翻译评价
  updateUserTranslateRate(int session, int sendTime, int tranState) async {
    chatDataTableModel.updateUserTranslateRate(session, sendTime, tranState);
  }

  //更新消息发送状态
  updateMsgState(int session, int sendTime, int state,
      {bool isGroup = false}) async {
    if (isGroup) {
      groupChatTableModel.updateMsgState(session, sendTime, state);
    } else {
      chatDataTableModel.updateMsgState(session, sendTime, state);
    }
  }

  //更新红包状态
  updateWalletState(MsgModel msgModel) async {
    chatDataTableModel.updateWalletState(msgModel);
  }

  //更新数据是否已被读取
  updateReadState(int session, {bool isGroup = false}) async {
    if (isGroup) {
      groupChatTableModel.updateReadState(session);
    } else {
      chatDataTableModel.updateReadState(session);
    }
  }

  //更新文件本地路径
  updateLocalFile(String extraData, String local,
      {bool isGroup = false}) async {
    if (isGroup) {
      groupChatTableModel.updateLocalFile(extraData, local);
    } else {
      chatDataTableModel.updateLocalFile(extraData, local);
    }
  }

  //更新语音已读状态
  updateSoundListened(MsgModel msg) {
    bool isGroup = msg.channelType == ChatChannelType.Group.value;
    if (isGroup) {
      groupChatTableModel.updateSoundListened(msg);
    } else {
      chatDataTableModel.updateSoundListened(msg);
    }
  }

  deleteSigleRecordWith(int session, int timeatmp,
      {bool isGroup = false}) async {
    if (isGroup) {
      groupChatTableModel.deleteSigleRecordWith(session, timeatmp);
    } else {
      chatDataTableModel.deleteSigleRecordWith(session, timeatmp);
    }
  }

  //删除聊天数据
  delteRecord(int session, {bool isGroup = false}) async {
    if (isGroup) {
      groupChatTableModel.delteRecord(session);
    } else {
      chatDataTableModel.delteRecord(session);
    }
  }

  ///关闭
  static close() {
    _database?.close();
    _database = null;
  }
}
