import { random } from 'underscore';
import { ActivityModelType } from '../../db/Activity';
import { ActivityTreasureHuntShopModelType } from '../../db/ActivityTreasureHuntShop';
import { ActivityTreasureHuntTaskModelType } from '../../db/ActivityTreasureHuntTask';
import { ActivityTreasureHuntTreasureShopModelType } from '../../db/ActivityTreasureHuntTreasureShop';
import { ActivityTreasureHuntFirstPageModelType } from '../../db/ActivityTreasureHuntFirstPage';
import { splitString } from '../../pubUtils/util';
import { ActivityBase } from './activityField';


// 进入活动首页的数据
export class TreasureHuntFirstPageData {
    name: string = '';//页签名字
    index: number = 0;//下标
    reward: string = '';//奖励
    imageName: string = '';

    isReceive: boolean = false;//是否领取

    public setPlayerFirstPageRecord(record: ActivityTreasureHuntFirstPageModelType) {
        this.isReceive = false;
        if (!record) {
            return;
        }
        this.isReceive = record.isReceive ? record.isReceive : false;
    }

    public initData(data: any) {
        this.name = data.name;
        this.index = data.index;
        this.reward = data.reward;
        this.imageName = data.imageName;
        this.isReceive = false;
    }

    constructor(data: any) {
        this.initData(data)
    }
}

/************************************************************/
//购买价格
export class ConsumeData {
    count: number;       // 第几次购买，1开始
    consume: string;            //消耗资源 "2&31002&400"
    discount: number;       // 折扣

    constructor(data: any) {
        this.count = data.count;
        this.consume = data.consume;
        this.discount = data.discount;
    }
}

// 商品的数据
export class TreasureHuntShopItem {
    cellIndex: number;       // 第几个，从1开始
    name: string;            //名称
    price: number;          // 价格RMB 每次购买价格不变
    productID: string;      // 商品id
    reward: string;         //奖励
    countMax: number;      //最大购买次数
    discount: number;      //折扣
    imageName: string;
    consume: ConsumeData[];               //每次购买价格不同

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

    constructor(data: any) {
        this.cellIndex = data.cellIndex;
        this.name = data.name;
        this.price = data.price;
        this.productID = data.productID;
        this.reward = data.reward;
        this.countMax = data.countMax;
        this.discount = data.discount;
        this.imageName = data.imageName;
        this.consume = [];
        for (let obj of data.data) {
            this.consume.push(new ConsumeData(obj))
        }
        this.buyCount = 0;
    }

    public getConsume() {
        let index = this.consume.findIndex(obj => { return obj && obj.count === this.buyCount + 1 });
        return (index != -1) ? this.consume[index].consume : '';
    }
}

// 商店数据
export class TreasureHuntShopData {
    index: number = 0;//下标
    name: string = '';//页签名字
    list: Array<TreasureHuntShopItem> = [];//商品

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

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

    public setPlayerShopRecords(record: ActivityTreasureHuntShopModelType) {
        if (!record) {
            return;
        }
        for (let item of this.list) {
            let buyRecords = record.records.filter(obj => { return obj.id === item.cellIndex });
            item.buyCount = buyRecords.length;
        }
    }


    public initData(data: any) {
        this.index = data.index;
        this.name = data.name;
        let arr = data.data;
        for (let obj of arr) {
            this.list.push(new TreasureHuntShopItem(obj))
        }
    }

    constructor(data: any) {
        this.initData(data)
    }
}

/************************************************************/

// 寻宝备战的数据
export class TreasureHuntTaskItem {
    cellIndex: number;       // 第几个，从1开始
    name: string;            //名称
    taskType: number;          // 任务类型
    taskParam: string;      // 任务参数
    condition: number;      //任务条件数据
    reward: string;         //奖励
    fragment: number;      //碎片

    totalCount: number = 0;          //任务统计
    isReceive: boolean = false;       //是否领取奖励

    constructor(data: any) {
        this.cellIndex = data.cellIndex;
        this.name = data.name;
        this.taskType = data.taskType;
        this.taskParam = data.taskParam;
        this.condition = data.condition;
        this.reward = data.reward;
        this.fragment = data.fragment;
        this.totalCount = 0;
        this.isReceive = false;
    }
}

// 寻宝备战数据
export class TreasureHuntTaskData {
    name: string = '';//页签名字
    index: number = 0;//下标
    reward: string = '';//奖励
    fragment: number = 0;//碎片需求量
    list: Array<TreasureHuntTaskItem> = [];//任务

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

    public setPlayerTaskRecords(record: ActivityTreasureHuntTaskModelType[]) {
        for (let item of this.list) {
            let index = record.findIndex(obj => { return obj.cellIndex === item.cellIndex });
            if (index != -1) {
                item.totalCount = record[index].totalCount ? record[index].totalCount : 0;
                item.isReceive = record[index].receiveRewardCount ? true : false;
            }
        }
    }

    public findItemByTaskType(type: number) {
        return this.list.filter(obj => { return obj && obj.taskType === type });
    }

    public initData(data: any) {
        this.name = data.name;
        this.index = data.index;
        this.reward = data.reward;
        this.fragment = data.fragment;
        let arr = data.data;
        for (let obj of arr) {
            this.list.push(new TreasureHuntTaskItem(obj))
        }
    }

    constructor(data: any) {
        this.initData(data)
    }
}

/************************************************************/

// 寻宝大冒险的数据
export class TreasureHuntChallengeData {
    name: string = '';//页签名字
    index: number = 0;//下标
    consume: string = '';//消耗
    warid: string = '';//随机的关卡号
    fixReward: string = '';//客户端显示奖励
    jackpotReward: string = '';//客户端显示奖励
    imageName: string = '';

    public randomGK() {
        let gkArray = splitString(this.warid, '&');
        let index = random(gkArray.length - 1);
        return gkArray[index];
    }

    public initData(data: any) {
        this.name = data.name;
        this.index = data.index;
        this.consume = data.consume;
        this.warid = data.warid;
        this.fixReward = data.fixReward;
        this.jackpotReward = data.jackpotReward;
        this.imageName = data.imageName;
    }

    constructor(data: any) {
        this.initData(data)
    }
}


/************************************************************/

//天子宝库
// 商品的数据
export class TreasureHuntTreasureShopItem {
    cellIndex: number;       // 第几个，从1开始
    name: string;            //名称
    reward: string;         //奖励
    countMax: number;      //最大购买次数
    imageName: string;
    consume: "";               //购买价格

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

    constructor(data: any) {
        this.cellIndex = data.cellIndex;
        this.name = data.name;
        this.reward = data.reward;
        this.countMax = data.countMax;
        this.imageName = data.imageName;
        this.consume = data.consume;
        this.buyCount = 0;
    }
}

// 天子宝库商店数据
export class TreasureHuntTreasureShopData {
    index: number = 0;//下标
    name: string = '';//页签名字
    list: Array<TreasureHuntTreasureShopItem> = [];//商品

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

    public setPlayerTreasureShopRecords(record: ActivityTreasureHuntTreasureShopModelType) {
        if (!record) {
            return;
        }
        for (let item of this.list) {
            let buyRecords = record.records.filter(obj => { return obj.id === item.cellIndex });
            item.buyCount = buyRecords.length;
        }
    }


    public initData(data: any) {
        this.index = data.index;
        this.name = data.name;
        let arr = data.data;
        for (let obj of arr) {
            this.list.push(new TreasureHuntTreasureShopItem(obj))
        }
    }

    constructor(data: any) {
        this.initData(data)
    }
}

/************************************************************/

// 寻宝骑兵活动数据
export class TreasureHuntData extends ActivityBase {
    name: string = '';//活动名字
    day: string = '';//活动持续时间
    roundIndex = 0;//周期数
    firstPage: TreasureHuntFirstPageData = null;//首页奖励
    shop: TreasureHuntShopData = null; //每日物资
    tasks: TreasureHuntTaskData = null; //寻宝备战
    challenge: TreasureHuntChallengeData = null;//寻宝大冒险
    treasureShop: TreasureHuntTreasureShopData = null; //天子宝库（商店）


    public initData(data: any) {
        let dataObj = JSON.parse(data);
        this.name = dataObj.name;
        this.day = dataObj.day;

        let arr = dataObj.data;
        {
            let index = arr.findIndex(obj => { return obj.index === 1 });
            this.firstPage = new TreasureHuntFirstPageData(arr[index]);
        }
        {
            let index = arr.findIndex(obj => { return obj.index === 2 });
            this.shop = new TreasureHuntShopData(arr[index]);
        }
        {
            let index = arr.findIndex(obj => { return obj.index === 3 });
            this.tasks = new TreasureHuntTaskData(arr[index]);
        }
        {
            let index = arr.findIndex(obj => { return obj.index === 4 });
            this.challenge = new TreasureHuntChallengeData(arr[index]);
        }
        {
            let index = arr.findIndex(obj => { return obj.index === 5 });
            this.treasureShop = new TreasureHuntTreasureShopData(arr[index]);
        }
    }

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