import { FIRST_GIFT_STATE } from '../../consts';
import { ActivityModelType } from '../../db/Activity';
import { ActivityFirstGiftModelType } from '../../db/ActivityFirstGift';
import { deltaDays } from '../../pubUtils/util';
import { ActivityBase } from './activityField';

/************** 在数据库中首充礼包的格式 ***********/

interface FirstGiftRewardInDb {
    index: number;         // 第几天，从1开始
    reward: string;        // type&id&count, type: 1-武将 2-道具 3-礼包
    goldCount: number;     // 元宝数量
}

interface FirstGiftDataInDb {
    pageIndex: number;              // 首充第几页
    targetMoney: number;            // 充值到几元
    rewards: FirstGiftRewardInDb[]; // 可领取奖励
}

interface FirstGiftInDb {
    data: FirstGiftDataInDb[]
}

/************** 给客户端返回的数据 ***********/
// 首充礼包的数据
export class FirstGiftItem {
    index: number;          // 第几天，从1开始
    reward: string;           //奖励
    goldCount: number;

    isReceive: boolean = false;          //是否领取过奖励

    constructor(data: FirstGiftRewardInDb) {
        this.index = data.index;
        this.reward = data.reward;
        this.goldCount = data.goldCount;
    }

    public setReceived(daysNum: number[] = []) {
        if (daysNum.indexOf(this.index) !== -1) {
            this.isReceive = true;
        }
    }

    public canReceive(todayIndex: number) {
        return !this.isReceive && this.index <= todayIndex;
    }
}

export class FirstGiftPage {
    pageIndex: number;
    targetMoney: number;                      // 这页的目标充值金额
    state: number = FIRST_GIFT_STATE.NOT_PAY; //活动状态
    todayIndex: number = 0;                   // 玩家首次充值到达该金额后第几天
    items: FirstGiftItem[] = [];               //奖励
    
    constructor(data: FirstGiftDataInDb) {
        // console.log(data)
        this.pageIndex = data.pageIndex;
        this.targetMoney = data.targetMoney;
        for (let obj of data.rewards) {
            this.items.push(new FirstGiftItem(obj))
        }
    }

    public setPlayerRecord(playerData: ActivityFirstGiftModelType) {
        if(playerData) {
            this.state = FIRST_GIFT_STATE.PAY;
            this.todayIndex = deltaDays(playerData.createdAt, new Date, true) + 1;

            let daysNum = playerData.days||[];
            for (let obj of this.items) {
                obj.setReceived(daysNum);
            }
            if(playerData.isOver) {
                this.state = FIRST_GIFT_STATE.ALL_RECEIVED;
            }
        }
    }

    public isComplete() {
        for (let i = 0; i < this.items.length; i++) {
            if (!this.items[i].isReceive) {
                return false
            }
        }
        return true;
    }

    // 寻找每日奖励领取数据
    public findItem(index: number) {
        let listIndex = this.items.findIndex(obj => { return obj && obj.index == index });
        if (listIndex != -1) {
            return this.items[listIndex];
        }
        return null;
    }

    public getCanReceiveItems() {
        return this.items.filter(obj => { 
            return obj && obj.canReceive(this.todayIndex); 
        });
    }
}

// 30天任务活动数据
export class FirstGiftData extends ActivityBase {
    list: FirstGiftPage[] = [];//奖励
    private map: Map<number, number> = new Map(); // pageIndex => index

    // 根据页码获得对象
    public findPageByIndex(pageIndex: number) {
        let index = this.map.get(pageIndex);
        return this.list[index];
    }

    //解析玩家任务领取记录
    public setPlayerRecords(data: ActivityFirstGiftModelType[]) {
        this.todayIndex = 0;
        if (!data) {
            return;
        }
        for(let playerData of data) {
            let pageData = this.findPageByIndex(playerData.pageIndex);
            if(pageData) pageData.setPlayerRecord(playerData);
        }
    }

    public initData(data: string) {
        let dataObj: FirstGiftInDb = JSON.parse(data);

        let arr = dataObj.data;
        for (let obj of arr) {
            let page = new FirstGiftPage(obj);
            this.list.push(page);
            this.map.set(page.pageIndex, this.list.length - 1);
        }
    }

    public getShowResult() {
        return {
            ...this.getBaseKeys(),
            list: this.list,
        }
    }

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