import { ActivityModelType } from '../../db/Activity';
import { ActivitySelfServiceShopModelType } from '../../db/ActivitySelfServiceShop';
import { ActivityBase } from './activityField';

// 自助商店数据坑位数据
export class SelfServiceShopItemInfo {
    cellIndex: number;      // 第几个坑位，从1开始
    cellType: number;    //坑位类型 SELF_SERVICE_SHOP_CELL_TYPE 1.特殊 2.普通
    gift: string;         //礼包内容

    constructor(data: any) {
        this.cellIndex = data.cellIndex;
        this.cellType = data.cellType;
        this.gift = data.gift;
    }
}

// 自助商店数据
export class SelfServiceShopItem {
    index: number;      // 第几个货架，从1开始
    name: string;            //名称
    countMax: number = 0;       // 最大可购买次数 0表示无限
    price: number;          //价格
    productID: string;      //商品id
    consume: string;         //消耗其他资源

    data: Array<SelfServiceShopItemInfo> = [];

    buyCount: number = 0; //已经购买次数

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

    constructor(cellData: any) {
        this.index = cellData.index;
        this.name = cellData.name;
        this.countMax = cellData.countMax;
        this.price = cellData.price;
        this.productID = cellData.productID;
        this.consume = cellData.consume;
        this.data = [];
        for (let obj of cellData.data) {
            this.data.push(new SelfServiceShopItemInfo(obj))
        }
    }
}

// 自选商店
export class SelfServiceShopData extends ActivityBase {
    list: Array<SelfServiceShopItem> = [];//货架
    name: string = '';
    count: number = 1;//每天可挑战次数胜利，才会统计
    warid: string = '';//可挑战关卡
    unitPrice: string = '';//元宝购买代币价格
    unitCountMax: number = 0;//元宝购买代币最大次数
    unitReward: string = '';//购买获得代币资源

    challengeCount: number = 0;//挑战次数
    unitBuyCount: number = 0;//元宝购买代币次数

    public getItemByProductID(productID: string): SelfServiceShopItem {
        let listIndex = this.list.findIndex(obj => { return obj.productID == productID });
        return (listIndex != -1) ? this.list[listIndex] : null;
    }


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

    //解析玩家购买记录
    public setPlayerRecords(data: ActivitySelfServiceShopModelType[]) {
        for (let item of this.list) {
            let buyData = data.filter(obj => { return obj.index === item.index })
            item.buyCount = buyData.length
        }
    }

    public initData(data: string) {
        let dataObj = JSON.parse(data);
        this.name = dataObj.name;
        this.count = dataObj.count;
        this.warid = dataObj.warid;
        this.unitPrice = dataObj.unitPrice;
        this.unitCountMax = dataObj.unitCountMax;
        this.unitReward = dataObj.unitReward;
        this.challengeCount = 0;

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

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