import { DAILYRMBGIFTS_DAYS, DAILY_RMB_GIFT_STATUS } from '../../consts';
import { ActivityModelType } from '../../db/Activity';
import { ActivityDailyRMBGiftsModelType } from '../../db/ActivityDailyRMBGifts';
import { ActivityBase } from './activityField';

// 商品数据
export class DailyRMBGiftsItem {
    id: number;      // 第几个，从1开始
    productID: string; // 商品id支付时使用
    name: string;      // 名字
    price: number;    //价格
    reward: string;         //奖励，格式:1&3&1(类型&id&数量) 类型定义:1.英雄，2.物品

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

    constructor(data: any) {
        this.id = data.id;
        this.productID = data.productID;
        this.name = data.name;
        this.price = data.price;
        this.reward = data.reward;
        this.isReceive = false;
    }
}

// 每日特惠礼包RMB购买4挡,一次性支付购买7天活动数据
export class DailyRMBGiftsData extends ActivityBase {
    name: string = '';//活动名称
    day: number = DAILYRMBGIFTS_DAYS;//一次购买几天
    price: number = 0;//一次性购买的价格
    productID: string = '';//一次性购买的商品id
    list: Array<DailyRMBGiftsItem> = [];//每件商品信息
    isBuy: boolean = false;//是否一次性购买过

    unReceived: number[] = [];
    unReceiveCount: number = 0;//剩余领取次数（一次性购买）
    buttonStatus: number = DAILY_RMB_GIFT_STATUS.CAN_BUY;

    public unReceiveItem() {
        return this.list.filter(obj => { return !obj.isReceive });
    }

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

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

    //解析玩家领取记录
    public setPlayerRecords(playerRecords: ActivityDailyRMBGiftsModelType[]) {
        for(let playerRecord of playerRecords) {
            if(playerRecord.todayIndex == this.todayIndex) {
                this.isBuy = !!playerRecord.isBuy;
                let records = playerRecord.records ? playerRecord.records : [];
                for(let {id} of records) {
                    let item = this.findItem(id);
                    if(item) item.isReceive = true;
                }
            } else {
                this.unReceived.push(playerRecord.todayIndex);
            }
        }

        this.calTodayReceiveStatus();
        this.calButtonStatus();
    }

    public calTodayReceiveStatus() {
        this.unReceiveCount = this.unReceived.length;
        if(this.isBuy) {
            let hasAllReceived = !this.list.find(item => !item.isReceive);
            if(!hasAllReceived && this.unReceived.indexOf(this.todayIndex) == -1) {
                this.unReceiveCount++;
            }
        }
    }

    public calButtonStatus() {
        if(this.isBuy) { // 已购买
            let hasAllReceived = !this.list.find(item => !item.isReceive);
            if(hasAllReceived) { // 买了且全都领完
                this.buttonStatus = DAILY_RMB_GIFT_STATUS.HAS_RECEIVED;
            } else {
                this.buttonStatus = DAILY_RMB_GIFT_STATUS.CAN_RECEIVE;
            }
        } else {
            if(this.unReceiveCount > 0) {
                this.buttonStatus = DAILY_RMB_GIFT_STATUS.HAS_RECEIVED;
            }
        }
    }

    public shouldbuyNextDay() {
        return this.list.filter(item => item.isReceive && item.price != 0).length != 0;
    }

    public receiveItems(ids: number[]) {
        let items: DailyRMBGiftsItem[] = [];
        for(let item of this.list) {
            if(ids.indexOf(item.id) != -1) {
                item.isReceive = true;
                items.push(item);
            }
        }
        this.calButtonStatus();
        this.calTodayReceiveStatus();
        return items;
    }

    public initData(data: string) {
        this.isBuy = false;
        this.unReceiveCount = 0;
        let dataObj = JSON.parse(data);
        this.name = dataObj.name;
        this.day = dataObj.day;
        this.price = dataObj.price;
        this.productID = dataObj.productID;
        // let curDate = moment(new Date());
        // if (curDate.hour() < REFRESH_TIME) {
        //     this.beginTime = moment(new Date()).startOf('d').add(-1, 'd').add(REFRESH_TIME, 'h').valueOf();
        //     this.endTime = moment(new Date()).startOf('d').add(-1, 'd').add(REFRESH_TIME, 'h').valueOf();
        // } else {
        //     this.beginTime = moment(new Date()).startOf('d').add(REFRESH_TIME, 'h').valueOf();
        //     this.endTime = moment(new Date()).startOf('d').add(REFRESH_TIME, 'h').valueOf();
        // }

        // console.log('ddddddddddddbbbbbbb', this.activityId, this.beginTime, this.endTime)

        let arr = dataObj.data;
        for (let obj of arr) {
            this.list.push(new DailyRMBGiftsItem(obj))
        }
    }

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