import 'package:chat/data/UserData.dart';
import 'package:chat/models/group_info_model.dart';
import 'package:chat/utils/sql_model.dart';

import 'group_member_model.dart';

class GroupMemberTableModel {
  final String table = 'group_member';
  Sql sql;

  GroupMemberTableModel() {
    sql = Sql.setTable(table);
  }

  createTable() async {
    print('创建表$table');
    await sql.createTable(table, '''rowId INTEGER PRIMARY KEY, 
            userId INTEGER,
            sessionId INTEGER,
            memberId INTEGER,
            identity INTEGER,
            refName TEXT,
            nickName TEXT,
            inGroup INTEGER,
            avtar TEXT''');
  }

  Future clear() {
    return sql.clearTable(table);
  }

  Future clearSelfData() async {
    var curUserId = UserData().basicInfo.userId;
    var res =
        await sql.db.delete(table, where: 'userId=?', whereArgs: [curUserId]);
    print('删除结果$res');
  }

  void addSingleMember(int sessionId, GroupMemberModel memberModel) async {
    var curUserId = UserData().basicInfo.userId;

    await sql.insert({
      'userId': curUserId,
      'sessionId': sessionId,
      'memberId': memberModel.memberId,
      'identity': memberModel.identity,
      'refName': memberModel.refName,
      'nickName': memberModel.nickName,
      'inGroup': memberModel.inGroup,
      'avtar': memberModel.avtar,
    });
  }

  //更改成员在群状态
  void updateMemberState(int sessionId, int uId, int state) {
    var curUserId = UserData().basicInfo.userId;

    sql.db.update(table, {'inGroup': state},
        where: 'userId=? and sessionId = ? and memberId = ?',
        whereArgs: [curUserId, sessionId, uId]);
  }

  //批量插入群成员
  void addMembers(GroupInfoModel infoModel) {
    print('插入群成员到数据库');
    for (var i = 0; i < infoModel.members.length; i++) {
      addSingleMember(infoModel.sessionId, infoModel.members[i]);
    }
  }

  //批量删除群成员
  void deleteMembers(int sessionId) {
    var curUserId = UserData().basicInfo.userId;

    sql.db.delete(table,
        where: 'userId=? and sessionId = ?', whereArgs: [curUserId, sessionId]);
  }

//删除群成员缓存
  void clearMemberCache() {
    var curUserId = UserData().basicInfo.userId;

    sql.db.delete(table, where: 'userId=?', whereArgs: [curUserId]);
  }

  //删除群成员
  void deleteMember(int sessionId, int uId) {
    var curUserId = UserData().basicInfo.userId;
    print('数据库删除成员$uId');
    sql.db.delete(table,
        where: 'userId=? and sessionId = ? and memberId = ?',
        whereArgs: [curUserId, sessionId, uId]);
  }

  //查询群成员信息
  Future<List<GroupMemberModel>> getMemberInfo(int sessionId) async {
    var curUserId = UserData().basicInfo.userId;
    List lists = await sql.query(table,
        where: 'userId = ? and sessionId = ?',
        whereArgs: [curUserId, sessionId]);

    List<GroupMemberModel> result = [];

    for (var i = 0; i < lists.length; i++) {
      var info = GroupMemberModel.fromJson(lists[i]);
      result.add(info);
    }

    return result;
  }

  //修改成员身份
  updateMemberIdentity(int session, int uId, int identity) async {
    var curUserId = UserData().basicInfo.userId;

    sql.db.update(table, {'identity': identity},
        where: 'userId=? and sessionId = ? and memberId = ?',
        whereArgs: [curUserId, session, uId]);
  }

  //修改群成员的别名
  updateMemberRefName(int session, int uId, String refName) async {
    var curUserId = UserData().basicInfo.userId;

    sql.db.update(table, {'refName': refName},
        where: 'userId=? and sessionId = ? and memberId = ?',
        whereArgs: [curUserId, session, uId]);
  }

  //修改群公告
  updateDescription(int session, String description, int time) async {
    var curUserId = UserData().basicInfo.userId;

    sql.db.update(table, {'describe': description, 'decribeTime': time},
        where: 'userId=? and sessionId = ?', whereArgs: [curUserId, session]);
  }
}
