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

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

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

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

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


// 每日兑换铜币活动数据
export class DailyCoinData extends ActivityBase {
    countMax: number = 0;//每日上限
    freeCount: number = 0;//每日免费次数
    extraRewardCount: string = '';//可额外获得道具奖励的第X次
    extraReward: string = '';//额外获得的道具奖励
    double: number = 0;//2倍暴击触发的概率
    fiveTimes: number = 0;//5倍暴击触发的概率
    consumeExchangeFormulaItem: ConsumeExchangeFormulaItem[] = [];
    coinRewardFormulaItem: CoinRewardFormulaItem[] = [];

    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) {
        let arr = splitString(this.extraRewardCount, '&');
        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) {
        this.exchangeCount = data && data.exchangeCount ? data.exchangeCount : 0;
        this.coinCount = data && data.coinCount ? data.coinCount : 0;
        this.recordMsg = data && data.recordMsg ? data.recordMsg : [];
    }

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

        let dataObj = JSON.parse(data);
        this.countMax = dataObj.countMax;
        this.freeCount = dataObj.freeCount;
        this.extraRewardCount = dataObj.extraRewardCount;
        this.extraReward = dataObj.extraReward;
        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 curDate = moment(new Date());
        // if (curDate.hour() < REFRESH_TIME) {
        //     this.beginTime = curDate.startOf('d').add(-1, 'd').add(REFRESH_TIME, 'h').valueOf();
        //     this.endTime = moment(this.beginTime).add(1, 'd').valueOf();
        // } else {
        //     this.beginTime = curDate.startOf('d').add(REFRESH_TIME, 'h').valueOf();
        //     this.endTime = moment(this.beginTime).add(1, 'd').valueOf()
        // }
        // console.log('ddddddddddddbbbbbbb')

    }

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