import BaseModel from './BaseModel';
import { index, getModelForClass, prop, DocumentType } from '@typegoose/typegoose';
import { nowSeconds } from '../pubUtils/timeUtil';
class Reward {
    @prop({ required: true })
    id: number;
    @prop({ required: true })
    count: number;
}

class TrainBox {
    @prop({ required: true })
    roleId: string;
    @prop({ required: true, type: Reward })
    good: Reward;//领取奖励
    @prop({ required: true })
    index: number;//领取位置
}

export class TrainInstance {
    @prop({ required: true })
    hid: number;
    @prop({ required: true })
    progress: number;//进度
    @prop({ required: true })
    endTime: number;//挑战胜利后，宝箱领取有效时间
    @prop({ required: true, default: [], type: TrainBox, _id: false })
    trainBoxs: TrainBox[];//宝箱领取记录
}

class Rank {
    @prop({ required: true })
    roleId: string;
    @prop({ required: true })
    score: number;
}


@index({guildCode:1, trainId:1})
export default class GuildTrain extends BaseModel {
    @prop({ required: true })
    guildCode: string;            
    @prop({ required: true })
    trainId: number;
    @prop({ required: true })
    isComplete: boolean;
    @prop({ required: true, default: [], type: TrainInstance, _id: false })
    trainInstances: TrainInstance[];
    @prop({ required: true, default: [], type: Rank, _id: false })
    ranks: Rank[];
    @prop({ required: true, default: false})
    locked: boolean; //逻辑删除
    
    public static async findGuildTrain(guildCode: string, select?: string, locked = false, lean = true) {
        const guildTrains: GuildTrainType[] = await GuildTrainModel.find({ guildCode, locked }).select(select).lean(lean);
        return guildTrains;
    }

    public static async findGuildTrainByTrainIds(guildCode: string, trainIds:Array<number>, locked = false, lean = true) {
        const guildTrains: GuildTrainType[] = await GuildTrainModel.find({ trainId:{ $in:trainIds}, guildCode, locked }).lean(lean);
        return guildTrains;
    }

    public static async findTrainByTrainIdNotLock(guildCode: string, trainId: number, select?: string, locked = false, lean = true) {
        const guildTrain: GuildTrainType = await GuildTrainModel.findOne({ guildCode, trainId, locked }).select(select).lean(lean);
        return guildTrain;
    }

    public static async updateGuildTrain(guildCode: string, trainId: number, update: GuildTrainTypeParam, lean = true) {
        const guildTrain: GuildTrainType = await GuildTrainModel.findOneAndUpdate({ guildCode, trainId}, { $set: update }, {new: true}).lean(lean);
        return guildTrain;
    }

    public static async updateGuildTrainProgress(guildCode: string, trainId: number, hid:number, progress: number, ranks:Array<Rank>, isComplete: boolean,lean = true) {
        const guildTrain: GuildTrainType = await GuildTrainModel.findOneAndUpdate({ guildCode, trainId, 'trainInstances.hid':hid}, 
        { $set: {'trainInstances.$.progress': progress, ranks, isComplete,'trainInstances.$.endTime': (nowSeconds() + 24 * 60 * 60)} },{new: true}).lean(lean);

        return guildTrain;
    }

    public static async openGuildTrain(guildCode: string, trainId: number, trainInstances: Array<TrainInstance>, lean = true) {
        const guildTrain: GuildTrainType = await GuildTrainModel.findOneAndUpdate({ guildCode, trainId },{ trainId, locked: false, ranks: [], trainInstances, isComplete: false, guildCode}, {new: true, upsert: true}).lean(lean);
        return guildTrain;
    }

    public static async findTrainInstanceBoxByIndex(guildCode: string, trainId: number, hid: number, progress: number, locked = false, lean = true) {
        const guildTrain: GuildTrainType = await GuildTrainModel.findOne({ guildCode, trainId, locked, 'trainInstances.hid': hid, 'trainInstances.progress': {$gte: progress} }).lean(lean);
        return guildTrain;
    }

    public static async receiveBoxByIndex(guildCode: string, roleId: string, trainId: number, hid: number, index: number, good: Reward, locked = false, lean = true) {
        const guildTrain: GuildTrainType = await GuildTrainModel.findOneAndUpdate({ guildCode, trainId, locked, 'trainInstances.hid': hid, },
            { $push:{ 'trainInstances.$.trainBoxs': {roleId, index, good} }}, {new: true}).lean(lean);
        return guildTrain;
    }

    public static async resetGuildTrain(guildCode: string) {
        const result = await GuildTrainModel.updateMany({guildCode}, {$set:{ locked: true }});
        return result;
    }

    public static async getGuildTrainBoxs(guildCode: string) {
        const result: GuildTrainType[] = await GuildTrainModel.find({guildCode, 'trainInstances.endTime':{$gte: nowSeconds()}, locked: false});
        return result;
    }

    public static async removeTrainRank(guildCode: string, roleId: string, trainId: number, locked = false, lean = true) {
        const guildTrain: GuildTrainType = await GuildTrainModel.findOneAndUpdate({ guildCode, trainId, locked },
            { $pull:{ ranks: { roleId } }}, {new: true}).lean(lean);
        return guildTrain;
    }

}


export const GuildTrainModel = getModelForClass(GuildTrain);

export interface GuildTrainType extends Pick<DocumentType<GuildTrain>, keyof GuildTrain> { };
export type GuildTrainTypeParam = Partial<GuildTrainType>; // 将所有字段变成可选项
