import BaseModel from './BaseModel';
import { index, getModelForClass, prop, DocumentType } from '@typegoose/typegoose';

export class Record {
    @prop({ required: true })
    hid: number;				        // 选中的武将id
    @prop({ required: true })
    count: number;			        // 次数
    @prop({ required: true })
    reward: string;			        // 奖励内容
    @prop({ required: true })
    time: Date;				        // 抽卡时间
    @prop({ required: true })
    hasGetFloor: boolean;  // 是否抽到保底了
}

/**
  * 活动系统 - 每日关卡活动
*/
@index({ roleId: 1 })

export default class Activity_New_Hero_Gacha extends BaseModel {
    @prop({ required: true })
    serverId: number;				// 服id
    @prop({ required: true })
    activityId: number;				// 活动Id
    @prop({ required: true })
    roleId: string;			        // 用户Id
    @prop({ required: true })
    selectedHid: number;				    // 玩家选中的武将id
    @prop({ required: true })
    count: number;				        // 抽卡次数，抽中大奖后重新统计
    @prop({ required: true })
    records: Record[];				    // 抽卡历史记录
    @prop({ required: true })
    hasGetFloor: boolean;  // 是否抽到保底了

    //重置统计次数
    public static async resetCount(serverId: number, activityId: number, roleId: string, count: number) {
        let result: ActivityNewHeroGachaModelType = await ActivityNewHeroGachaModel.findOneAndUpdate({ serverId, roleId, activityId, },
            { $set: { count } }, { upsert: true, new: true }).lean(true);
        return result;
    }

    //抽卡记录
    public static async addRecord(serverId: number, activityId: number, roleId: string, hid: number, count: number, reward: string, hasGetFloor: boolean) {
        let result: ActivityNewHeroGachaModelType = await ActivityNewHeroGachaModel.findOneAndUpdate({ serverId, roleId, activityId },
            { $set: { count, hasGetFloor }, $push: { records: { hid, count, reward, time: new Date(), hasGetFloor } } }, { upsert: true, new: true }).lean(true);
        return result;
    }

    //设置选中武将
    public static async selectedHero(serverId: number, activityId: number, roleId: string, selectedHid: number) {
        let result: ActivityNewHeroGachaModelType = await ActivityNewHeroGachaModel.findOneAndUpdate(
            { serverId, roleId, activityId }, { $set: { selectedHid } }, { upsert: true, new: true }).lean(true);
        return result;
    }

    //根据活动id查询活动数据
    public static async findData(serverId: number, activityId: number, roleId: string) {
        let result: ActivityNewHeroGachaModelType = await ActivityNewHeroGachaModel.findOne({ serverId, roleId, activityId }).lean(true);
        return result;
    }

    //删除活动领取记录
    public static async deleteActivity(serverId: number, activityId: number, roleId: string) {
        await ActivityNewHeroGachaModel.deleteMany({ serverId, roleId, activityId });
    }
}

export const ActivityNewHeroGachaModel = getModelForClass(Activity_New_Hero_Gacha);

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