import { ActivityModelType } from '../../db/Activity';
import { ActivityDailyGKModelType } from '../../db/ActivityDailyGK';
import { ActivityBase } from './activityField';


// 每日配置数据
export class DailyGKItem {
    dayIndex: number;       // 第几天,从1开始
    gk: number;           // 关卡
    name: string;           // 名称
    reward: string;         // 奖励，格式:1&3&1(类型&id&数量) 类型定义:1.英雄，2.物品
    isSuccess: boolean;      //是否成功

    constructor(data: any) {
        this.dayIndex = data.dayIndex;
        this.gk = data.gk;
        this.name = data.name;
        this.reward = data.reward;
        this.isSuccess = false;
    }
}


// 每日关卡活动数据
export class DailyGKData extends ActivityBase {
    list: Array<DailyGKItem> = [];

    public findItemByGK(gk: number) {
        let index = this.list.findIndex(obj => { return obj && obj.gk == gk })
        return (index != -1) ? this.list[index] : null;
    }

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

    //解析玩家记录
    public setPlayerRecords(data: ActivityDailyGKModelType) {
        if (!data)
            return;
        let records = data.days ? data.days : [];
        for (let obj of this.list) {
            let index = records.findIndex(dayIndex => { return obj.dayIndex == dayIndex })
            if (index != -1) {
                obj.isSuccess = true
            }
        }
    }

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

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