import { ACTIVITY_TYPE } from '../../consts';
import { ActivityModelType } from '../../db/Activity';
import { ActivityBuyRecordsModelType } from '../../db/ActivityBuyRecords';
import { ActivityGrowthFundModelType } from '../../db/ActivityGrowthFund';
import { splitString } from '../../pubUtils/util';
import { ActivityBase } from './activityField';


// 成长基金每个奖励的数据
export class GrowthFundItem {
    id: number;             // 编号
    name: string;            //名称
    cellIndex: number;      // 第几个，从1开始
    taskType: number;       // 任务类型 dic_zyz_taskType.json
    taskParam: string;      //任务数据 dic_zyz_taskType.json
    condition: number;       //0
    reward: string;         // 任务奖励，格式:1&3&1(类型&id&数量) 类型定义:1.英雄，2.物品
    isComplete: boolean    //是否完成任务
    taskParamArray: number[]//  
    // isReceive: boolean = false;          //是否领取过奖励

    constructor(data: any) {
        this.id = data.id;
        this.cellIndex = data.cellIndex;
        this.name = data.name;
        this.taskType = data.taskType;
        this.taskParam = data.taskParam;
        this.reward = data.reward;
        this.condition = data.condition;
        this.isComplete = false;
        // this.isReceive = false;
        this.taskParamArray = splitString(data.taskParam, '&')

    }
}

// 成长基金每页奖励的数据
export class GrowthFundPage {
    pageIndex: number = 0;//页
    price: number = 0;//高阶价格
    productID: string = '';//商品id
    items: GrowthFundItem[] = [];//所有任务

    constructor(data: any) {
        this.pageIndex = data.pageIndex;
        this.price = data.price;
        this.productID = data.productID;
        for (let itemData of data.data) {
            this.items.push(new GrowthFundItem(itemData))
        }

    }

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

// 成长基金活动数据
export class GrowthFundData extends ActivityBase {
    list: Array<GrowthFundPage> = [];
    buyRecords: Array<number> = [];//高阶购买记录
    receiveRecords: Array<ActivityGrowthFundModelType> = [];//领取记录

    public unLockItem(condition: number) {
        let items = [];
        for (let page of this.list) {
            if (this.isVipActivity() && !this.isBuy(page.pageIndex)) {
                continue;
            }
            for (let item of page.items) {
                switch (this.type) {
                    case ACTIVITY_TYPE.GROWTH_FUND_MAIN:
                    case ACTIVITY_TYPE.GROWTH_FUND_MAIN_VIP: {
                        if (!this.isReceive(page.pageIndex, item.cellIndex) && item.taskParamArray[1] == condition) {
                            item.isComplete = true;
                            items.push(Object.assign(item, { pageIndex: page.pageIndex, activityId: this.activityId }))
                        }
                        break;
                    }
                    case ACTIVITY_TYPE.GROWTH_FUND_TOWER:
                    case ACTIVITY_TYPE.GROWTH_FUND_TOWER_VIP: {
                        if (!this.isReceive(page.pageIndex, item.cellIndex) && item.condition <= condition) {
                            item.isComplete = true;
                            items.push(Object.assign(item, { pageIndex: page.pageIndex, activityId: this.activityId }))
                        }
                        break;
                    }
                    case ACTIVITY_TYPE.GROWTH_FUND_MAIN_ELITE:
                    case ACTIVITY_TYPE.GROWTH_FUND_MAIN_ELITE_VIP: {
                        if (!this.isReceive(page.pageIndex, item.cellIndex) && item.taskParamArray[1] == condition) {
                            item.isComplete = true;
                            items.push(Object.assign(item, { pageIndex: page.pageIndex, activityId: this.activityId }))
                        }
                        break;
                    }
                }
            }
        }
        return items;
    }

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

    //是否是高阶，需要购买
    public isVipActivity() {
        return this.type == ACTIVITY_TYPE.GROWTH_FUND_MAIN_VIP
            || this.type == ACTIVITY_TYPE.GROWTH_FUND_TOWER_VIP
            || this.type == ACTIVITY_TYPE.GROWTH_FUND_MAIN_ELITE_VIP
    }

    //是否购买page
    public isBuy(pageIndex: number) {
        let index = this.buyRecords.findIndex(obj => { return obj == pageIndex });
        return index != -1;
    }

    //是否领取过
    public isReceive(pageIndex: number, cellIndex: number) {
        let index = this.receiveRecords.findIndex(obj => { obj && obj.pageIndex == pageIndex && cellIndex == obj.cellIndex })
        return (index !== -1);
    }

    //全部领取完成
    public isComplete() {
        for (let i = 0; i < this.list.length; i++) {
            let page = this.list[i];
            for (let item of page.items) {
                let index = this.receiveRecords.findIndex(obj => { obj && obj.pageIndex == page.pageIndex && item.cellIndex == obj.cellIndex })
                if (index == -1) {
                    return false
                }
            }
        }
        return true;
    }

    public findGrowthFundItem(pageIndex: number, cellIndex: number) {
        let index = this.list.findIndex(obj => { return obj && obj.pageIndex == pageIndex })
        return this.list[index].findItem(cellIndex);
    }

    //解析玩家领取记录
    public setPlayerRecords(data: ActivityGrowthFundModelType[]) {
        this.receiveRecords = data;
    }

    //高阶版购买记录
    public initBuyRecords(buyRecords: ActivityBuyRecordsModelType[]) {
        for (let obj of buyRecords) {
            this.buyRecords.push(obj.pageIndex);
        }
    }

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

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