import moment = require('moment');
import { ActivityModelType } from '../../db/Activity';
import { ActivityMonthlyTicketModelType } from '../../db/ActivityMonthlyTicket';
import { deltaDays } from '../../pubUtils/util';
import { ActivityBase } from './activityField';


// 月卡数据
export class MonthlyTicketData extends ActivityBase {

    name: string = '';//名称
    id: number = 0;//序列
    productID: string = '';//商品id
    price: number = 0;//价格
    days: number = 0;//持续天数
    baseReward: string = '';//每天领取的奖励
    firstReward: string = '';//首次购买获得的奖励
    imageName: string = '';
    rebate: number = 0;//返利倍数

    endTime: number = 0;//结束时间
    dayIndex: number = 0;//已经领取了几天
    isOpen: boolean = false;//是否开启中
    todayIndex: number = 0;//现在可以领取第几天的奖励
    //解析玩家领取记录
    public setPlayerRecords(data: ActivityMonthlyTicketModelType) {
        if (!data) {//没有购买月卡
            return;
        }
        this.dayIndex = data.dayIndex;
        this.endTime = moment(data.endTime).valueOf();
        if (moment(new Date).valueOf() < this.endTime) {
            this.isOpen = true;
            this.todayIndex = deltaDays(moment(data.createdAt).startOf('d').toDate(), new Date) + 1;
        } else {
            this.todayIndex = 0;
        }
    }

    public initData(data: string) {
        let dataObj = JSON.parse(data);

        this.id = dataObj.id;
        this.name = dataObj.name;
        this.productID = dataObj.productID;
        this.price = dataObj.price;
        this.days = dataObj.days;
        this.baseReward = dataObj.baseReward;
        this.firstReward = dataObj.firstReward;
        this.imageName = dataObj.imageName;
        this.rebate = dataObj.rebate;

        this.isOpen = false;
        this.todayIndex = 0;
        this.dayIndex = 0;
        this.endTime = 0;
    }

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