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

/**
  * 活动系统 - 成长任务活动
*/
@index({ roleId: 1, activityId: 1 })

export default class Activity_Growth extends BaseModel {
    @prop({ required: true })
    serverId: number;				// 活动Id
    @prop({ required: true })
    activityId: number;				// 活动Id
    @prop({ required: true })
    roleId: string;			        // 用户Id
    @prop({ required: true })
    dayIndex: number;				// 第几天
    @prop({ required: true })
    cellIndex: number;			    // 第几天的第几个奖励
    @prop({ required: true })
    taskType: number;			        // 任务类型
    @prop({ required: true })
    totalCount: number;				// 累计达成次数
    @prop({ required: true })
    receiveRewardCount: number;					// 领取奖励次数
    @prop({ required: true })
    data: string;					// 数据信息
    @prop({ required: true, type: String })
    records: string[];					// 数据信息

    //任务领取记录
    public static async addCellRecord(serverId: number, activityId: number, roleId: string, dayIndex: number, cellIndex: number, count: number,) {
        let result: ActivityGrowthModelType = await ActivityGrowthModel.findOneAndUpdate({ serverId, roleId, activityId, dayIndex, cellIndex },
            { $inc: { receiveRewardCount: count } }, { upsert: true, new: true }).lean(true);
        return result;
    }

    public static async setOrIncTask(serverId: number, activityId: number, roleId: string, dayIndex: number, cellIndex: number, taskType: number, param: UpdateTaskParam) {
        if(param.set) {
            return await this.setTaskCount(serverId, activityId, roleId, dayIndex, cellIndex, taskType, param.set, param.records);
        } else if (param.inc) {
            return await this.addTaskCount(serverId, activityId, roleId, dayIndex, cellIndex, taskType, param.inc, param.records);
        } else if (param.records){
            let result: ActivityGrowthModelType = await ActivityGrowthModel.findOneAndUpdate({ serverId, roleId, activityId, dayIndex, cellIndex },
                { $set: { records: param.records, taskType } }, { upsert: true, new: true }).lean();
            return result;
        }
    }

    //根据活动统计完成任务次数
    public static async setTaskCount(serverId: number, activityId: number, roleId: string, dayIndex: number, cellIndex: number, taskType: number, count: number, records?: string[]) {
        let result: ActivityGrowthModelType = await ActivityGrowthModel.findOneAndUpdate({ serverId, roleId, activityId, dayIndex, cellIndex },
            { $set: { totalCount: count, records: records||[], taskType } }, { upsert: true, new: true }).lean();
        return result;
    }

    //根据活动统计完成任务次数
    public static async addTaskCount(serverId: number, activityId: number, roleId: string, dayIndex: number, cellIndex: number, taskType: number, addCount: number, records?: string[]) {
        let result: ActivityGrowthModelType = await ActivityGrowthModel.findOneAndUpdate({ serverId, roleId, activityId, dayIndex, cellIndex },
            { $inc: { totalCount: addCount }, $set: { records: records||[], taskType } }, { upsert: true, new: true }).lean();
        return result;
    }

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

    //查询第几天的活动数据
    public static async findDataByDayIndex(serverId: number, activityId: number, roleId: string, dayIndex: number, lean = true) {
        let result: ActivityGrowthModelType[] = await ActivityGrowthModel.find({ serverId, roleId, activityId, dayIndex }).lean(lean);
        return result;
    }

    //查询第几天某个的活动数据
    public static async findDataByCellIndex(serverId: number, activityId: number, roleId: string, dayIndex: number, cellIndex: number) {
        let result: ActivityGrowthModelType = await ActivityGrowthModel.findOne({ serverId, roleId, activityId, dayIndex, cellIndex }).lean(true);
        return result;
    }

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

export const ActivityGrowthModel = getModelForClass(Activity_Growth);

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