import moment = require('moment');
import { ACTIVITY_TYPE, REFRESH_TIME, SERVER_OPEN_TIME, SIGNIN_CLOSE, SIGNIN_OPEN } from '../../consts';
import { ActivityModelType } from '../../db/Activity';
import { ActivitySignInModelType } from '../../db/ActivitySignIn';
import { ActivityBase } from './activityField';


// 每天签到配置数据
export class SignInItem {
    dayIndex: number;       // 第几天,从1开始
    reward: string;         // 签到奖励，格式:1&3&1(类型&id&数量) 类型定义:1.英雄，2.物品

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

    constructor(data: any) {
        this.dayIndex = data.dayIndex;
        this.reward = data.reward;
        this.isReceive = false;
    }

    public canReceive(): boolean {
        return !this.isReceive;
    }
}


// 签到活动数据
export class SignInData extends ActivityBase {
    list: Array<SignInItem> = [];
    roundIndex: number = 0;//活动周期
    price: number = 0;//vip价格，普通签到为0
    // rebet: number = 0;
    productID: string = '';//商品ID
    consume: string = ''//补签消耗
    isVip: boolean = false;//是否购买了当前的vip活动
    startDate: number = 1;//月卡开启
    endDate: number = 30;//月卡关闭
    days: number = 8;//新手签到使用，持续的天数

    //第几天的签到奖励
    public findDayItem(dayIndex: number) {
        let index = this.list.findIndex(obj => { return obj && obj.dayIndex == dayIndex })
        return (index != -1) ? this.list[index] : null;
    }

    //解析玩家签到记录
    public setPlayerRecords(data: ActivitySignInModelType) {
        this.isVip = false;
        if (!data) {
            return;
        }
        let records = data.records ? data.records : [];
        for (let obj of this.list) {
            let index = records.findIndex(record => { return obj.dayIndex == record.dayIndex })
            if (index != -1) {
                obj.isReceive = true;
            }
        }
        this.isVip = true
    }

    //设置新手签到时间
    public setNewPlayerDate(beginTime: Date, endTime: Date) {
        this.beginTime = moment(beginTime).valueOf();
        this.endTime = moment(endTime).valueOf();
        this.roundIndex = 1;
        let date = new Date();
        this.todayIndex = Math.ceil((moment(date).valueOf() - this.beginTime) / (24 * 60 * 60 * 1000));
    }

    public initData(data: string) {
        let dataObj = JSON.parse(data);
        this.price = dataObj.price;
        this.productID = dataObj.productID;
        this.consume = dataObj.consume;
        // this.rebet = dataObj.rebet;

        let date = new Date();

        if (this.type === ACTIVITY_TYPE.COMMON_SIGN_IN) {//通用签到
            this.beginTime = moment(this.beginTime).startOf('day').add(REFRESH_TIME, 'h').valueOf();
            this.endTime = moment(this.endTime).endOf('day').add(REFRESH_TIME, 'h').valueOf();
            this.roundIndex = moment(this.beginTime).diff(moment(SERVER_OPEN_TIME).startOf('months'), 'months') + 1;
            this.todayIndex = Math.ceil((moment(date).valueOf() - this.beginTime) / (24 * 60 * 60 * 1000));
        } else {
            this.startDate = dataObj.startDate ? dataObj.startDate : SIGNIN_OPEN;
            this.endDate = dataObj.endDate ? dataObj.endDate : SIGNIN_CLOSE;

            this.days = dataObj.days ? dataObj.days : 0;
            this.beginTime = moment(date.setDate(this.startDate)).startOf('day').add(REFRESH_TIME, 'h').valueOf();
            if (this.startDate > this.endDate) {//跨度下个月
                this.endTime = moment(date.setDate(this.endDate)).endOf('day').add(REFRESH_TIME, 'h').add(1, 'month').valueOf();
            } else {
                this.endTime = moment(date.setDate(this.endDate)).endOf('day').add(REFRESH_TIME, 'h').valueOf();
            }
            this.roundIndex = moment(this.beginTime).diff(moment(SERVER_OPEN_TIME).startOf('months'), 'months') + 1;
            if (this.type === ACTIVITY_TYPE.NEW_PLAYER_SIGN_IN) {
                this.roundIndex = 1;
            }
            this.todayIndex = Math.ceil((moment().valueOf() - this.beginTime) / (24 * 60 * 60 * 1000));

        }

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

    }

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