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


/**
  * GM账号字段接口
*/
@index({ uid: 1 })

export default class GMUser extends BaseModel {
    @prop({ required: true })
    uid: number;

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

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

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

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

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

    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 getGmAccountList(lean = true) {
        const user: GMUserType[] = await GMUserModel.find().select('uid username name token').lean(lean);
        return user;
    }

    public static async getGmAccountByToken(token: string, lean = true) {
        // console.log(token)
        const user: GMUserType = await GMUserModel.findOne({ token }).select('uid username name token').lean(lean);
        return user;
    }

    public static async getGmAccountByUid(uid: number, lean = true) {
        const user: GMUserType = await GMUserModel.findOne({ uid }).select('uid username name token').lean(lean);
        return user;
    }

    public static async getGmAccountByUsername(username: string, lean = true) {
        const user: GMUserType = await GMUserModel.findOne({ username }).select('uid username name token').lean(lean);
        return user;
    }

    public static async login(username: string, password: string, token: string, lean = true) {
        const user: GMUserType = await GMUserModel.findOne({ username }).select('salt').lean();
        if (user) {
            let { salt } = user;
            let { npassword } = await this.encryptPass(password, salt);
            const checkUser: GMUserType = await GMUserModel.findOneAndUpdate({ username, password: npassword }, { token }).select('uid username name token').lean(lean);
            return checkUser;
        } else {
            return null
        }
    }

    public static async createGmAccount(username: string, password: string, name: string, token: string, lean = true) {
        const uid = await CounterModel.getNewCounter(COUNTER.GMUID);
        let r = await this.encryptPass(password);
        const user: GMUserType = await GMUserModel.findOneAndUpdate({ username }, { uid, password: r.npassword, salt: r.salt, name, token }, { upsert: true, new: true }).select('uid username name').lean(lean);
        return user;
    }

    public static async changePass(uid: number, password: string, lean = true) {
        let r = await this.encryptPass(password);
        const user: GMUserType = await GMUserModel.findOneAndUpdate({ uid }, { password: r.npassword, salt: r.salt }).select('uid username name').lean(lean);
        return user;
    }
}

export const GMUserModel = getModelForClass(GMUser);

export interface GMUserType extends Pick<DocumentType<GMUser>, keyof GMUser> { };
