import moment = require('moment');
import { ActivityModelType } from '../../db/Activity';
import { ActivityDailyMealModelType } from '../../db/ActivityDailyMeal';
import { getZeroPointD } from '../../pubUtils/timeUtil';
import { ActivityBase } from './activityField';


// 每日配置数据
export class DailyMealItem {
    name: string;       // 名称
    beginTime: number;       // 开始时间
    endTime: number;         // 结束时间
    type: number;            // 午饭晚饭类型，DAILY_MEAL_TYPE 1.午饭 2晚饭
    reward: string;         // 任务奖励，格式:1&3&1(类型&id&数量) 类型定义:1.英雄，2.物品
    consume: string;        //补签消耗资源

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

    constructor(data: any) {
        let todayZeroPoint = getZeroPointD();
        this.beginTime = moment(todayZeroPoint).startOf('d').add(data.beginHour, 'h').valueOf();
        this.endTime = moment(todayZeroPoint).startOf('d').add(data.endHour, 'h').valueOf();
        this.type = data.type;
        this.reward = data.reward;
        this.consume = data.consume;
        this.name = data.name;

        this.isReceive = false;
    }
}


// 每日免费午餐、晚餐活动数据
export class DailyMealData extends ActivityBase {
    list: Array<DailyMealItem> = [];
    name: string = ''//名字

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

    //解析玩家领取记录
    public setPlayerRecords(data: ActivityDailyMealModelType[]) {
        for (let obj of this.list) {
            let index = data.findIndex(record => { return obj.type == record.type })
            if (index != -1) {
                obj.isReceive = true;
            }
        }
    }

    public initData(data: string) {
        let dataObj = JSON.parse(data);
        this.name = dataObj.name;
        let arr = dataObj.data;
        for (let obj of arr) {
            this.list.push(new DailyMealItem(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, serverTime: number) {
        super(activityData, createTime, serverTime);
        this.initData(activityData.data)
    }
}