import { COUNTER, ENCRYPT_KEY, ENCRYPT_IV } from './../consts';
import { CounterModel } from './Counter';
import BaseModel from './BaseModel';
import { index, getModelForClass, prop, DocumentType } from '@typegoose/typegoose';
import { genCode, aesEncryptcfb, aesDecryptcfb } from '../pubUtils/util';
const bcrypt = require('bcrypt');
const SALT_WORK_FACTOR = 5

/**
  * 用户字段接口
*/
@index({ tel: 1 })
@index({ uid: 1 })
export default class User extends BaseModel {

  @prop({ required: true })
  uid: number;

  @prop({ required: true })
  username: string;

  @prop({ required: true })
  userCode: string; // 用户唯一字符串标识

  @prop({ required: true })
  token: string;

  @prop({ required: true, set: (val: string) => aesEncryptcfb(val, ENCRYPT_KEY, ENCRYPT_IV), get: (val: string) => aesDecryptcfb(val, ENCRYPT_KEY, ENCRYPT_IV) })
  tel: string; // 账号

  @prop({ required: true, default: '' })
  telHash: string;

  @prop({ required: true })
  password: string;

  @prop({ required: true })
  salt: string;

  @prop({ required: true, default: '' })
  channelId: string;

  // 游客相关
  @prop({ required: false, default: false })
  isGuest: boolean;    // 是否是游客
  @prop({ required: false })
  guestId: string;     // 游客id
  @prop({ required: false, default: 0 })
  guestTime: number;   // 游客体验时间

  // 防沉迷相关
  @prop({ required: false, default: false })
  hasAuthenticated: boolean;    // 是否是已认证
  @prop({ required: false })
  birthday: string;             // 生日年月
  @prop({ required: false })
  pi: string;                   // 已通过实名认证用户唯一标识
  @prop({ required: false, default: 0 })
  todayPlayTime: number;        // 今日游戏时间
  @prop({ required: false, default: 0 })
  todayPlayType: number;        // 未成年防沉迷类型
  @prop({ required: false })
  reportTime: Date;               // 汇报时间

  @prop({ required: false, default: false })
  hasSetPw: boolean;               // 是否设置过密码

  // 最后登录 IP
  @prop({ required: false })
  ip: string;

  @prop({ required: true })
  lastLoginTime: Date;

  @prop({ required: true })
  createTime: Date;

  @prop({ required: true })
  platform: string;
  @prop({ required: true, default: [] })
  platforms: [{
    platform: string; // 平台：ios, android, web, pc
    unionId: string; // 用户标识
  }];

  @prop({ required: true, type: String, default: [], _id: false })
  device: string[];

  @prop({ required: true, default: 'com.bantu.zyz' })
  pkgName: string;

  // 服务器类型：official, channel, ios, oversea
  @prop({ required: true, default: 'official' })
  serverType: string;

  // 账号是否被屏蔽
  @prop({ required: true, default: false })
  blocked: boolean;

  // 用户权限：0-普通用户，1-测试用户
  @prop({ required: true, default: 0 })
  auth: number;

  public static async createUser(isGuest: boolean, tel: string, token: string, platform: string, pkgName: string, serverType: string, deviceId: string, guestTime: number = 0) {
    let _tel = aesEncryptcfb(tel, ENCRYPT_KEY, ENCRYPT_IV);
    const curTime: Date = new Date();

    const uid = await CounterModel.getNewCounter(COUNTER.UID);
    const userCode = genCode(8);
    const doc = new UserModel();
    let update = {};
    update = Object.assign(update, doc.toJSON(), { platform, pkgName, serverType, createTime: curTime, uid, userCode, username: `用户${uid}`, isGuest, token, lastLoginTime: curTime, guestTime });
    if(isGuest) update["guestId"] = _tel;
    delete update["device"];

    const user: UserType = await UserModel.findOneAndUpdate({ tel }, { $set: update, $addToSet: {device: deviceId}}, { upsert: true, new: true }).lean({ getters: true });
    return user;
  }

  public static async getLastDeviceGuest(deviceId: string, token: string) {
    const user: UserType = await UserModel.findOneAndUpdate({ device: { $elemMatch: { $eq: deviceId } }, isGuest: true }, { token }, { new: true }).sort({createTime: -1}).lean({ getters: true });
    return user;
  }

  public static async updateToken(tel: string, token: string, deviceId: string) {

    const curTime: Date = new Date();

    let user = await UserModel.findOneAndUpdate({ tel }, { $set: { token, lastLoginTime: curTime }, $addToSet: {device: deviceId}}, { new: true }).lean({ getters: true });
    return user;
  }

  public static async createOrUpdate(isGuest: boolean, tel: string, token: string, platform: string, pkgName: string, serverType: string, deviceId: string) {
    // console.log(tel);

    let user: UserType = await UserModel.findOne({ tel }).lean({ getters: true });
    if (!user) {
      user = await UserModel.createUser(isGuest, tel, token, platform, pkgName, serverType, deviceId);
    } else {
      user = await UserModel.updateToken(tel, token, deviceId);
    }
    return user;
  }

  private static async encryptPass(password: string, salt?: string) {
    if (!salt) {
        salt = await bcrypt.genSalt(SALT_WORK_FACTOR);
    }
    let npassword = await bcrypt.hash(password, salt);
    return { npassword, salt };
  }

  public static async setPass(uid: number, password: string) {
    let r = await this.encryptPass(password);
    const user: UserType = await UserModel.findOneAndUpdate({ uid }, { $set: { password: r.npassword, salt: r.salt, hasSetPw: true }}, {new: true}).lean({ getters: true });
    return user;
  }

  public static async bindTel(uid: number, tel: string) {
    const user: UserType = await UserModel.findOneAndUpdate({ uid, isGuest: true }, { $set: { tel, isGuest: false }}, {new: true}).lean({ getters: true });
    return user;
  }

  public static async checkPass(tel: string, password: string, token: string, deviceId: string) {

    const user: UserType = await UserModel.findOne({ tel }).select('salt').lean({ getters: true });
    if (user) {
      const curTime: Date = new Date();
        let { salt } = user;
        let { npassword } = await this.encryptPass(password, salt);
        const checkUser: UserType = await UserModel.findOneAndUpdate({ tel, password: npassword }, { $set: { token, lastLoginTime: curTime }, $addToSet: {device: deviceId}}, { new: true }).lean({ getters: true });
        return checkUser;
    } else {
        return null
    }
  }

  public static async findUserByToken(token: string) {
    const user: UserType = await UserModel.findOne({ token }).select('uid token serverType auth tel userCode pkgName').lean({ getters: true });
    return user;
  }

  public static async findTokenByTel(tel: string) {

    const user: UserType = await UserModel.findOne({ tel }).lean();
    return user.token;
  }

  public static async findTokenByUid(uid: number) {
    const user: UserType = await UserModel.findOne({ uid }).lean();
    return user ? user.token : null;
  }

  public static async findUserByTel(tel: string) {

    const user: UserType = await UserModel.findOne({ tel }).select('uid tel hasSetPw').lean({ getters: true });
    return user;
  }

  public static async findUserByUid(uid: number) {
    const user: UserType = await UserModel.findOne({ uid }).select('uid tel').lean({ getters: true });
    return user;
  }

  public static async findUserByUserCode(userCode: string) {
    const user: UserType = await UserModel.findOne({ userCode }).lean({ getters: true });
    return user;
  }

  public static async addAuth(uid: number, auth: number) {
    const user: UserType = await UserModel.findOneAndUpdate({ uid }, { auth }, {new: true}).select('uid tel').lean({ getters: true });
    return user;
  }

  public static async authentication(uid: number, birthday: string, pi: string) {
    const user: UserType = await UserModel.findOneAndUpdate({ uid }, { hasAuthenticated: true, birthday, pi }, {new: true}).lean({ getters: true });
    return user;
  }

  public static async findUserByField(field: string, value?: Array<number|string>) {
    let searchObj = {};
    if(field != 'all') {
      searchObj[field] = {
        $in: value
      };
    }
    const user: UserType[] = await UserModel.find(searchObj).select('uid tel username serverType auth').lean({ getters: true });
    return user;
  }


  public static async updatePlayTime(userCode: string, guestTimeInc: number, todayPlayTime: number, todayPlayType?: number) {
    let update = { todayPlayTime, todayPlayType, reportTime: new Date() };
    if(todayPlayType) update.todayPlayType;
    const user: UserType = await UserModel.findOneAndUpdate({ userCode }, { $inc: { guestTime: guestTimeInc }, $set: update}, { new: true }).lean({ getters: true });
    return user;
  }
}


export const UserModel = getModelForClass(User);

export interface UserType extends Pick<DocumentType<User>, keyof User>{};
