import { random } from 'underscore';
import { DAILY_COIN_BOX_STATUS } from '../../consts';
import { ActivityModelType } from '../../db/Activity';
import { ActivityDailyCoinModelType } from '../../db/ActivityDailyCoin';
import { splitString } from '../../pubUtils/util';
import { ActivityBase } from './activityField';

interface ConsumeExchangeFormulaInDb {
    key: number;
    countMin: number;         // 兑换次数下限
    countMax: number;         // 兑换次数上限
    consumeFormula: string;
}

interface CoinRewardFormulaInDb {
    key: number;
    levelMin: number;          // 玩家等级下限
    levelMax: number;          // 玩家等级上限
    rewardFormula: string;
}

interface DailyCoinDataInDb {
    countMax: number;           // 每日上限
    freeCount: number;          // 每日免费次数
    extraRewardCount: string;   // 可额外获得道具奖励的第X次
    extraReward: string;        // 额外获得的道具奖励
    double: number;             // 2倍暴击触发的概率
    fiveTimes: number;          // 5倍暴击触发的概率
    consumeExchangeFormula: ConsumeExchangeFormulaInDb[]; // 消耗的元宝
    coinRewardFormula: CoinRewardFormulaInDb[];       // 获得的硬币
}

// 每次配置数据
export class ConsumeExchangeFormulaItem {
    countMin: number;
    countMax: number;
    consumeFormula: string;

    constructor(data: ConsumeExchangeFormulaInDb) {
        this.countMin = data.countMin;
        this.countMax = data.countMax;
        this.consumeFormula = data.consumeFormula;
    }
}


export class CoinRewardFormulaItem {
    levelMin: number;
    levelMax: number;
    rewardFormula: string;

    constructor(data: CoinRewardFormulaInDb) {
        this.levelMin = data.levelMin;
        this.levelMax = data.levelMax;
        this.rewardFormula = data.rewardFormula;
    }
}

export class ExtraRewardItem {
    cellIndex: number;
    exchangeCount: number;
    reward: string;
    status: number = DAILY_COIN_BOX_STATUS.CAN_NOT_OPEN;

    constructor(cellIndex: number, exchangeCount: string, reward: string) {
        this.cellIndex = cellIndex;
        this.exchangeCount = parseInt(exchangeCount);
        this.reward = reward;
    }

    setRecord(count: number, receivedBox: number[]) {
        this.status = DAILY_COIN_BOX_STATUS.CAN_NOT_OPEN;
        if(count >= this.exchangeCount) this.status = DAILY_COIN_BOX_STATUS.CAN_OPEN;
        if(receivedBox.indexOf(this.cellIndex) != -1) this.status = DAILY_COIN_BOX_STATUS.RECEIVED;
    }

    addExchangeCount(exchangeCount: number) {
        if(this.status == DAILY_COIN_BOX_STATUS.CAN_NOT_OPEN && exchangeCount >= this.exchangeCount) {
            this.status = DAILY_COIN_BOX_STATUS.CAN_OPEN;
        }
    }

    receiveBox() {
        this.status = DAILY_COIN_BOX_STATUS.RECEIVED;
    }
}

// 每日兑换铜币活动数据
export class DailyCoinData extends ActivityBase {
    countMax: number = 0;           // 每日上限
    freeCount: number = 0;          // 每日免费次数
    double: number = 0;             // 2倍暴击触发的概率
    fiveTimes: number = 0;          // 5倍暴击触发的概率
    consumeExchangeFormulaItem: ConsumeExchangeFormulaItem[] = [];
    coinRewardFormulaItem: CoinRewardFormulaItem[] = [];
    extraReward: ExtraRewardItem[] = [];

    exchangeCount: number = 0;     // 已经兑换次数
    coinCount: number = 0;         // 已经兑换获得铜币数量
    recordMsg: string[] = [];      // 历史记录

    public getRate() {
        let ran = (random(99) + 1) * 0.01;//[1,100]
        ran -= this.double;
        if (ran <= 0) {
            return 2;
        }
        ran -= this.fiveTimes;
        if (ran <= 0) {
            return 5;
        }
        return 1;
    }

    // public isExtra(index: number) {
    //     return arr.indexOf(index);
    // }

    public findConsumeExchangeFormulaItem(index: number) {
        let itemIndex = this.consumeExchangeFormulaItem.findIndex(obj => { return obj.countMin <= index && obj.countMax >= index });
        return (itemIndex != -1) ? this.consumeExchangeFormulaItem[itemIndex] : null;
    }

    public findCoinRewardFormulaItem(level: number) {
        let itemIndex = this.coinRewardFormulaItem.findIndex(obj => { return obj.levelMin <= level && obj.levelMax >= level });
        return (itemIndex != -1) ? this.coinRewardFormulaItem[itemIndex] : null;
    }

    //解析玩家领取记录
    public setPlayerRecords(data: ActivityDailyCoinModelType) {
        if(!data) return;
        this.exchangeCount = data.exchangeCount||0;
        this.coinCount = data.coinCount || 0;
        this.recordMsg = data.recordMsg || [];
        for(let box of this.extraReward) {
            box.setRecord(this.exchangeCount, data.receivedBox||[]);
        }
    }

    public findBoxByCellIndex(cellIndex: number) {
        return this.extraReward.find(cur => cur.cellIndex == cellIndex);
    }

    public addExchangeCount(count: number) {
        this.exchangeCount += count;
        for(let box of this.extraReward) {
            box.addExchangeCount(this.exchangeCount);
        }
        return this.extraReward;
    }

    public receiveBox(cellIndex: number) {
        let item = this.extraReward.find(cur => cur.cellIndex == cellIndex);
        if(!item) return null;
        item.receiveBox();
        return item
    }

    public hasNotReceivedBox(): boolean {
         for(let box of this.extraReward) {
             if(box.status == DAILY_COIN_BOX_STATUS.CAN_OPEN) return true;
         }
         return false;
    }

    public getUnReceivedBox() {
        return this.extraReward.filter(cur => cur.status == DAILY_COIN_BOX_STATUS.CAN_OPEN);
    }

    public initData(data: string) {
        this.exchangeCount = 0;
        this.coinCount = 0;

        let dataObj: DailyCoinDataInDb = JSON.parse(data);
        this.countMax = dataObj.countMax;
        this.freeCount = dataObj.freeCount;
        this.double = dataObj.double;
        this.fiveTimes = dataObj.fiveTimes;

        for (let obj of dataObj.consumeExchangeFormula) {
            this.consumeExchangeFormulaItem.push(new ConsumeExchangeFormulaItem(obj))
        }
        for (let obj of dataObj.coinRewardFormula) {
            this.coinRewardFormulaItem.push(new CoinRewardFormulaItem(obj))
        }
        let counts = splitString(dataObj.extraRewardCount, '&');
        let extraReward = dataObj.extraReward.split('|');
        for(let i = 0; i < counts.length; i++) {
            this.extraReward.push(new ExtraRewardItem(i + 1, counts[i], extraReward[i]||''));
        }
    }

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