import { ActivityModelType } from '../../db/Activity';
import { UserOrderModelType } from '../../db/UserOrder';
import { ActivityBase } from './activityField';

// 商品的数据
export class YuanBaoShopItem {
    id: number;             // 第几个，从1开始
    price: number;          // 价格RMB
    productID: string;      // 商品id
    name: string;            //名称
    baseReward: string;      //购买即可获得的基础奖励
    firstReward: string;      //首充时，赠送的奖励
    extraReward: string;      //不是首冲时，赠送的奖励
    imageName: string;

    buyCount: number = 0;          //购买过的次数

    constructor(data: any) {
        this.id = data.id;
        this.price = data.price;
        this.productID = data.productID;
        this.name = data.name;
        this.baseReward = data.baseReward;
        this.firstReward = data.firstReward;
        this.extraReward = data.extraReward;
        this.imageName = data.imageName;
        this.buyCount = 0;
    }
}

// 元宝充值商店数据
export class YuanBaoShopData extends ActivityBase {
    list: Array<YuanBaoShopItem> = [];//商品

    //商品
    public findItem(productID: string): YuanBaoShopItem {
        let listIndex = this.list.findIndex(obj => { return obj && obj.productID == productID });
        if (listIndex != -1) {
            return this.list[listIndex];
        }
        return null;
    }

    //解析玩家购买记录
    public setPlayerRecords(data: UserOrderModelType[]) {
        this.todayIndex = 0;
        if (!data) {
            return;
        }
        for (let obj of this.list) {
            let arr = data.filter(order => { return order.productID === obj.productID })
            obj.buyCount = arr.length;
        }
    }

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

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

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