import moment = require('moment');
import { ACTIVITY_TYPE, SHOP_REFRESH_TYPE, SIGNIN_CLOSE, SIGNIN_OPEN } from '../../consts';
import { ActivityModelType } from '../../db/Activity';
import { ActivitySignInModelType } from '../../db/ActivitySignIn';
import { getZeroPoint, getZeroPointOfTime } from '../../pubUtils/timeUtil';
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;
    }

    public setReceive(isReceive: boolean) {
        this.isReceive = isReceive;
    }
}


// 签到活动数据
export class SignInData extends ActivityBase {
    list: Array<SignInItem> = [];
    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;//新手签到使用，持续的天数
    signBeginTime: number = 0;
    signEndTime: number = 0;

    //第几天的签到奖励
    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 : [];
        let day: number[] = [];
        for(let obj of records) {
            let item = this.findDayItem(obj.dayIndex);
            if(item) {
                item.setReceive(true);

                if(this.type == ACTIVITY_TYPE.SIGN_IN || this.type == ACTIVITY_TYPE.SIGN_IN_VIP) {
                    let time = getZeroPointOfTime(obj.time, SHOP_REFRESH_TYPE.DAILY, 0)
                    let today = getZeroPoint(SHOP_REFRESH_TYPE.DAILY, 0);
                    // console.log('setPlayerRecords', time, today)
                    if(day.indexOf(time) == -1 && time < today) {
                        this.todayIndex++;
                        day.push(time);
                    }
                }
            }
        }
        this.isVip = true
    }

    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;
        this.signBeginTime = this.beginTime;
        this.signEndTime = this.endTime;

        if(this.type == ACTIVITY_TYPE.SIGN_IN||this.type == ACTIVITY_TYPE.SIGN_IN_VIP) {
            this.startDate = dataObj.startDate ? dataObj.startDate : SIGNIN_OPEN;
            this.endDate = dataObj.endDate ? dataObj.endDate : SIGNIN_CLOSE;

            this.days = dataObj.days ? dataObj.days : 0;
            this.signBeginTime = moment().startOf('M').valueOf();
            this.signEndTime = moment().endOf('M').valueOf();

            this.roundIndex = parseInt(moment().format('YYYYMM'));
            this.todayIndex = 1;

        } else if (this.type == ACTIVITY_TYPE.NEW_PLAYER_SIGN_IN) {
            this.roundIndex = 0;
        }

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

    }

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