import { ActivityModelType } from '../../db/Activity';
import { ActivityVipRechargeMoneyModelType } from '../../db/ActivityVipRechargeMoney';
import { ActivityBase } from './activityField';

// 奖励
export class VipRechargeMoneyItem {
    id: number;       //商品id
    vipLevel: number   //vip等级
    vipName: string; //vip名称
    name: string;      //商品名称
    reward: string;   //奖励
    condition: number; //充值条件
    imageName: string;
    content: string;  //描述
    countMax: number; //最大领取次数

    receiveCount: number; //领取次数
    constructor(data: any) {
        this.id = data.id;
        this.vipLevel = data.vipLevel;
        this.vipName = data.vipName;
        this.name = data.name;
        this.reward = data.reward;
        this.condition = data.condition;
        this.imageName = data.imageName;
        this.countMax = data.countMax;
        this.content = data.content;
        this.receiveCount = 0;
    }
}

// 累计充值数据
export class VipRechargeMoneyData extends ActivityBase {
    list: Array<VipRechargeMoneyItem> = [];//记录
    totalRMB: number = 0; //累计充值金额

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

    //解析玩家购买记录
    public setPlayerRecords(data: ActivityVipRechargeMoneyModelType) {
        if (!data) {
            return;
        }
        this.totalRMB = data.RMB;
        let records = data.record ? data.record : [];
        for (let item of this.list) {
            let recordArray = records.filter(obj => { return obj.id == item.id });
            item.receiveCount = recordArray.length;
        }
    }

    public initData(data: string) {
        let dataObj = JSON.parse(data);

        let arr = dataObj;
        for (let obj of arr) {
            this.list.push(new VipRechargeMoneyItem(obj))
        }
    }

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