import { ActivityModelType } from '../../db/Activity';
import { ActivityNewHeroGKModelType } from '../../db/ActivityNewHeroGK';
import { ActivityBase } from './activityField';


// 每日配置数据
export class NewHeroGKItem {
    pageIndex: number;
    begin: number;          // 第几天开始,从1开始，1表示第一天0点开始
    end: number;            // 第几天结束,2表示第二天24点结束
    index: number;          // 下标
    gk: number;             // 关卡
    name: string;           // 名称
    reward: string;         // 奖励，格式:1&3&1(类型&id&数量) 类型定义:1.英雄，2.物品

    isSuccess: boolean;     //是否成功

    constructor(data: any, pageIndex: number) {
        this.pageIndex = pageIndex;
        this.begin = data.begin;
        this.end = data.end;
        this.index = data.index;
        this.gk = data.gk;
        this.name = data.name;
        this.reward = data.reward;
        this.isSuccess = false;
    }
}


// 每日配置数据
export class NewHeroGKPage {
    items: Array<NewHeroGKItem> = [];
    pageIndex: number;           // 页码
    name: string;           // 名称
    hid: number;         //武将id
    title: string = '';//客户端用说明文字

    constructor(data: any) {
        this.pageIndex = data.pageIndex;
        this.name = data.name;
        this.hid = data.hid;
        this.title = data.title;
        this.items = [];
        for (let obj of data.items) {
            this.items.push(new NewHeroGKItem(obj, this.pageIndex))
        }
    }
}

// 每日关卡活动数据
export class NewHeroGKData extends ActivityBase {
    list: Array<NewHeroGKPage> = [];
    name: string;           // 名称

    public findItemByGK(pageIndex: number, gk: number) {
        for (let pageData of this.list) {
            if (pageData.pageIndex == pageIndex) {
                let index = pageData.items.findIndex(obj => { return obj.gk == gk })
                return (index != -1) ? pageData.items[index] : null;
            }
        }
        return null;
    }

    public findNewHeroGKItem(pageIndex: number, dayIndex: number) {
        for (let pageData of this.list) {
            if (pageData.pageIndex == pageIndex) {
                let index = pageData.items.findIndex(obj => { return obj.begin <= dayIndex && obj.end >= dayIndex })
                return (index != -1) ? pageData.items[index] : null;
            }
        }
        return null;
    }

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

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

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