import moment = require('moment');
import { ACTIVITY_TIME_TYPE, REFRESH_TIME, SERVER_OPEN_TIME } from '../../consts';
import { ActivityModelType } from '../../db/Activity';
import { deltaDays } from '../../pubUtils/util';

// 活动数据
export abstract class ActivityBase {
    activityId: number = 0;
    beginTime: number = 0;
    endTime: number = 0;
    type: number = 0;
    todayIndex: number = 0;//从1开始
    delayDay: number = 0;//延迟多少天开启，0表示按照原计划开启

    roundIndex: number = 0;//周期活动第几个周期，从1开始
    nextRefreshTime: number = 0;//周期活动下次刷新时间

    abstract initData(data: string): void;

    //今天是活动第几天
    public today(): number {
        return this.todayIndex;
    }

    constructor(activityData: ActivityModelType, createTime: number) {
        this.activityId = activityData.activityId;
        this.delayDay = activityData.delayDay ? activityData.delayDay : 0;
        this.beginTime = moment(activityData.beginTime).add(this.delayDay, 'd').valueOf();
        this.endTime = moment(activityData.endTime).add(this.delayDay, 'd').valueOf();
        this.todayIndex = deltaDays(moment(this.beginTime).toDate(), new Date) + 1;
        this.roundIndex = 1;
        this.nextRefreshTime = this.endTime;

        this.type = activityData.type;
        // console.log('今天是活动第几天', activityData.beginTime, new Date, this.todayIndex)
        switch (activityData.timeType) {
            case ACTIVITY_TIME_TYPE.SERVER_OPEN_TIME: {
                this.beginTime = moment(SERVER_OPEN_TIME).add(this.delayDay, 'd').startOf('d').add(REFRESH_TIME, 'h').valueOf();
                if (activityData.days > 0) {
                    this.endTime = moment(this.beginTime).add(activityData.days, 'd').valueOf();
                } else {
                    this.endTime = moment(this.beginTime).add(1, 'd').valueOf();
                }
                this.todayIndex = deltaDays(moment(this.beginTime).toDate(), new Date) + 1;
                console.log('活动时间数据11...', activityData.timeType, this.beginTime, this.endTime, this.todayIndex, this.roundIndex, this.nextRefreshTime)
                break;
            }
            case ACTIVITY_TIME_TYPE.ROLE_REGISTER_TIME: {
                this.beginTime = moment(createTime * 1000).add(this.delayDay, 'd').startOf('d').add(REFRESH_TIME, 'h').valueOf();
                if (activityData.days > 0) {
                    this.endTime = moment(this.beginTime).add(activityData.days, 'd').valueOf();
                } else {
                    this.endTime = moment(this.beginTime).add(1, 'd').valueOf();
                }
                this.todayIndex = deltaDays(moment(this.beginTime).toDate(), new Date) + 1;

                console.log('活动时间数据22...', activityData.timeType, this.beginTime, this.endTime, this.todayIndex, this.roundIndex, this.nextRefreshTime)
                break;
            }
            case ACTIVITY_TIME_TYPE.DATE_TIME: {
                console.log('活动时间数据33...', activityData.timeType, this.beginTime, this.endTime, this.todayIndex, this.roundIndex, this.nextRefreshTime)
                break;
            }
            default: {
                break;
            }
        }
        if (activityData.interval > 0) {
            this.roundIndex = Math.ceil((moment(new Date).valueOf() - this.beginTime) / (activityData.interval * 1000));
            this.nextRefreshTime = moment(this.beginTime).add(activityData.interval * this.roundIndex, 'second').valueOf();
            this.todayIndex = Math.ceil(((moment(new Date).valueOf() - this.beginTime) / (24 * 60 * 60 * 1000)));
        }
        // console.log('活动时间数据...', '活动id:', activityData.activityId, '类型:', activityData.timeType, '开始时间:', this.beginTime, moment(this.beginTime).toDate(),
        //     '结束:', this.endTime, moment(this.endTime).toDate(),
        //     '今天第几天:', this.todayIndex, '回合:', this.roundIndex, '下次刷新:', this.nextRefreshTime, moment(this.nextRefreshTime).toDate())
    }
}
