import { DAILYRMBGIFTS_DAYS } 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;//是否一次性购买过

    unReceiveCount: number = 0;//剩余领取次数（一次性购买）

    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(data: ActivityDailyRMBGiftsModelType, unReceiveCount: number) {
        this.isBuy = false;
        this.unReceiveCount = 0;
        if (!data) {
            return;
        }
        let records = data.records ? data.records : [];
        let isOver = true;
        for (let item of this.list) {
            let index = records.findIndex(obj => { return obj.id === item.id })
            if (index != -1) {
                item.isReceive = true;
            }
            if (!item.isReceive) {
                isOver = false;
            }
        }

        this.isBuy = data.isBuy ? data.isBuy : false;
        this.unReceiveCount = unReceiveCount;
        if (this.isBuy && !isOver) {
            this.unReceiveCount += 1;
        }
    }

    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) {
        super(activityData, createTime)
        this.initData(activityData.data)
    }
}