import BaseModel from './BaseModel';
import { index, getModelForClass, prop, DocumentType, } from '@typegoose/typegoose';
import { getZeroPointD } from '../pubUtils/timeUtil';
import { TASK_FUN_TYPE } from '../consts';
import { UpdateTaskParam } from '../domain/roleField/task';

@index({ leagueCode: 1, roleId: 1, configId: 1 })
export default class GVGUserTask extends BaseModel {

    @prop({ required: true, default: '' })
    roleId: string;          // 玩家

    @prop({ required: true, default: '' })
    leagueCode: string;        // 联军id

    @prop({ required: true, default: 0 })
    configId: number;          // 赛期

    @prop({ required: true, default: 0 })
    id: number;              // 任务id

    @prop({ required: true, enum: TASK_FUN_TYPE })
    type: number;					// 任务类型

    @prop({ required: true, default: 0 })
    count: number;           // 1-已达成
    
    @prop({ required: true, default: 0 })
    status: number;          // 1-未领取 2-已领取

    public static async findByRole(configId: number, leagueCode: string, roleId: string) {
        let today = getZeroPointD();
        const result: GVGUserTaskType[] = await GVGUserTaskModel.find({ configId, leagueCode, roleId, createdAt: { $gte: today } }, { _id: 0, id: 1, count: 1, status: 1 }).lean();
        return result;
    }

    public static async setTaskRec(configId: number, leagueCode: string, roleId: string, taskType: number, taskId: number, count: number) {
        let today = getZeroPointD();
        let rec: GVGUserTaskType = await GVGUserTaskModel.findOneAndUpdate({ configId, leagueCode, roleId, id: taskId, createdAt: { $gte: today } }, { $setOnInsert: { taskType, status: 1 }, $set: { count } }, { new: true, upsert: true }).lean();
        return rec;
    }

    public static async incTaskRec(configId: number, leagueCode: string, roleId: string, taskType: number, taskId: number, count: number) {
        let today = getZeroPointD();
        let rec: GVGUserTaskType = await GVGUserTaskModel.findOneAndUpdate({ configId, leagueCode, roleId, id: taskId, createdAt: { $gte: today } }, { $setOnInsert: { taskType, status: 1 }, $inc: { count } }, { new: true, upsert: true }).lean();
        return rec;
    }

    public static async setOrIncTask(configId: number, leagueCode: string, roleId: string, taskType: number, taskId: number, param: UpdateTaskParam) {
        if(param.set) {
            return await this.setTaskRec(configId, leagueCode, roleId, taskType, taskId, param.set);
        } else if (param.inc) {
            return await this.incTaskRec(configId, leagueCode, roleId, taskType, taskId, param.inc);
        }
    }

    public static async findByTaskId(configId: number, leagueCode: string, roleId: string, taskId: number) {
        let today = getZeroPointD();
        let rec: GVGUserTaskType = await GVGUserTaskModel.findOne({ configId, leagueCode, roleId, id: taskId, createdAt: { $gte: today } }).lean();
        return rec;
    }

    public static async receiveTask(configId: number, leagueCode: string, roleId: string, taskId: number) {
        let today = getZeroPointD();
        let rec: GVGUserTaskType = await GVGUserTaskModel.findOneAndUpdate({ configId, leagueCode, roleId, id: taskId, createdAt: { $gte: today } }, { $set: { status: 2 } }, { new: true }).lean();
        return rec;
    }
}

export const GVGUserTaskModel = getModelForClass(GVGUserTask);

export interface GVGUserTaskType extends Pick<DocumentType<GVGUserTask>, keyof GVGUserTask> {
    id: number;
};
export type GVGUserTaskUpdate = Partial<GVGUserTaskType>; // 将所有字段变成可选项
