import { TASK_TYPE } from '../../consts';
import { ActivityModelType } from '../../db/Activity';
import { ActivityDailyChallengesModelType } from '../../db/ActivityDailyChallenges';
import { ActivityBase } from './activityField';


// 今日挑战的每日配置数据
export class DailyItem {
    dayIndex: number;       // 第几天,从1开始
    cellIndex: number;      // 当天第几行，从1开始
    name: string;           // 任务名称
    taskType: number;       // 任务类型 dic_zyz_taskType.json
    taskParam: string;      //任务数据 dic_zyz_taskType.json
    condition: number;      //任务数据条件 dic_zyz_taskType.jsonT
    reward: string;         // 任务奖励，格式:1&3&1(类型&id&数量) 类型定义:1.英雄，2.物品

    totalCount: number = 0; //完成任务累计次数
    receiveRewardCount: number = 0;          //领取奖励次数

    constructor(data: any) {
        this.dayIndex = data.dayIndex;
        this.cellIndex = data.cellIndex;
        this.name = data.name;
        this.taskType = data.taskType;
        this.taskParam = data.taskParam;
        this.condition = data.condition;
        this.reward = data.reward;
        this.totalCount = 0;
        this.receiveRewardCount = 0;
    }

    public canReceive(): boolean {
        return this.receiveRewardCount == 0;
    }

    public isComplete(): boolean {
        let complete = this.totalCount >= this.condition;
        return complete;
    }
}


// 今日挑战活动数据
export class DailyChallengesData extends ActivityBase {
    list: Array<DailyItem> = [];

    public findDailyChallengesItem(dayIndex: number, cellIndex: number, type: number) {
        let index = this.list.findIndex(obj => { return obj && obj.dayIndex == dayIndex && obj.cellIndex == cellIndex && obj.taskType == type })
        return (index != -1) ? this.list[index] : null;
    }

    public findTaskByType(type: TASK_TYPE, dayIndex: number) {
        return this.list.filter(obj => {
            return obj && obj.taskType == type && obj.dayIndex == dayIndex;
        })
    }

    //解析玩家领取记录
    public setPlayerRecords(data: ActivityDailyChallengesModelType[]) {
        for (let obj of this.list) {
            let index = data.findIndex(record => { return obj.dayIndex == record.dayIndex && obj.cellIndex == record.cellIndex })
            if (index != -1) {
                obj.totalCount = data[index].totalCount ? data[index].totalCount : 0;
                obj.receiveRewardCount = data[index].receiveRewardCount ? data[index].receiveRewardCount : 0;
            }
        }
    }

    public initData(data: string) {
        let arr = JSON.parse(data);
        for (let obj of arr) {
            this.list.push(new DailyItem(obj))
        }
    }

    constructor(activityData: ActivityModelType, createTime: number, serverTime: number) {
        super(activityData, createTime, serverTime)
        this.initData(activityData.data)
    }
}