import { ActivityModelType } from '../../db/Activity';
import { ActivityNewHeroGiftModelType } from '../../db/ActivityNewHeroGift';
import { ActivityBase } from './activityField';

// 礼包的内容
export class NewHeroGiftItem {
    index: number;             // 下标
    reward: string;         //任务奖励，格式:1&3&1(类型&id&数量) 类型定义:1.英雄，2.物品
    countMax: number = 0;   //可购买的最大次数，0表示不限制
    name: string;           //名字
    consumePoint: number;   //消耗点数

    buyCount: number = 0;   //兑换过的次数

    constructor(data: any) {
        this.index = data.index;
        this.reward = data.reward;
        this.countMax = data.countMax;
        this.name = data.name;
        this.consumePoint = data.consumePoint;
        this.buyCount = 0;
    }
}

// 新将礼包数据
export class NewHeroGiftData extends ActivityBase {
    name: string = '';//活动名称
    list: Array<NewHeroGiftItem> = [];//礼包列表
    totalPoint: number = 0;//获得总点数
    consumeTotalPoint: number = 0;//消耗总点数
    explain: { index: number, explain: string }[] = [];//客户端用的说明文字信息

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

    //解析玩家购买记录
    public setPlayerRecords(data: ActivityNewHeroGiftModelType) {
        if (!data) {
            return;
        }
        this.totalPoint = data.totalPoint ? data.totalPoint : 0;
        let records = data.records ? data.records : [];
        for (let item of this.list) {
            let buyRecords = records.filter(obj => { return obj && obj.index === item.index });
            item.buyCount = buyRecords.length;
        }
        for (let obj of records) {
            this.consumeTotalPoint += obj.point;
        }
    }


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

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