import moment = require("moment");
import { POP_NOTICE_TIME_SHOW_TYPE, POP_NOTICE_TIME_TYPE } from "../../consts";
import { ActivityModelType } from "../../db/Activity";
import { ActivityBase } from "./activityField";

interface PopNoticeInDb {
    bg: string;     // 背景图片
    canClose: boolean; // 是否可以选择今天不再弹出
    sort: number;      // 活动的排序，越大的越前
    skip: string;      // 前往
    sysType: number;   // dic_zyz_systemOpenTime的id，功能开启
    timePosition: string; // 控制公告持续时间的显示位置'&'表示不显示持续时间
    btnPosition: string; // 按钮位置
    timeType: POP_NOTICE_TIME_TYPE;   // 1-手写公告 2-指定时间 3-按某个活动时间 4-按某个商品时间 5-无时间
    timeStr: string;    // 公告时间
    beginTime: number;      // 后台选择的时间，13位时间戳
    endTime: number;      // 后台选择的时间，13位时间戳
    activityId: number; // 活动id
    shopItemId: number; // 商品id
}

interface PopNoticeTimeParam {
    timeType: POP_NOTICE_TIME_TYPE;
    timeStr: string;
    beginTime: number;      // 后台选择的时间，13位时间戳
    endTime: number;      // 后台选择的时间，13位时间戳
    activityId: number;
    shopItemId: number;
}

/**
 * 打脸公告
 */
 export class PopNoticeData extends ActivityBase {
    bg: string;     // 背景图片
    canClose: boolean; // 是否可以选择今天不再弹出
    sort: number;      // 活动的排序，越大的越前
    skip: string;      // 前往
    sysType: number;   // dic_zyz_systemOpenTime的id，功能开启
    showTimeType: POP_NOTICE_TIME_SHOW_TYPE = POP_NOTICE_TIME_SHOW_TYPE.NO; // 显示类型
    timePosition: string; // 控制公告持续时间的显示位置'&'表示不显示持续时间
    btnPosition: string; // 按钮位置
    time: string = '';      // 公告持续时间
    countdownTime: number = 0;      // 公告持续时间，10位时间戳
    param: PopNoticeTimeParam;

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

    public initData(data: string) {
        let dataObj: PopNoticeInDb = JSON.parse(data);
        this.bg = dataObj.bg;
        this.canClose = dataObj.canClose;
        this.sort = dataObj.sort;
        this.sysType = dataObj.sysType;
        this.skip = dataObj.skip;
        this.timePosition = dataObj.timePosition;
        this.btnPosition = dataObj.btnPosition;
        this.param = {
            timeType: dataObj.timeType,
            timeStr: dataObj.timeStr,
            beginTime: dataObj.beginTime,
            endTime: dataObj.endTime,
            activityId: dataObj.activityId,
            shopItemId: dataObj.shopItemId,
        }
        this.setTime();
    }

    private setTime(beginTime?: number, endTime?: number) {
        let param = this.param;
        switch (param.timeType) {
            case POP_NOTICE_TIME_TYPE.USE_STR:
                this.showTimeType = POP_NOTICE_TIME_SHOW_TYPE.READ_STR;
                this.time = param.timeStr;
                break;
            case POP_NOTICE_TIME_TYPE.USE_TIME:
                this.showTimeType = POP_NOTICE_TIME_SHOW_TYPE.READ_COUNTDOWN;
                this.time = `活动时间：${moment(param.beginTime).format('MM.DD')} - ${moment(param.endTime).format('MM.DD')}`
                this.countdownTime = Math.floor(param.endTime/1000);
                break;
            case POP_NOTICE_TIME_TYPE.USE_ACTIVITY:
            case POP_NOTICE_TIME_TYPE.USE_SHOPITEM:
                this.showTimeType = POP_NOTICE_TIME_SHOW_TYPE.READ_COUNTDOWN;
                if(beginTime && endTime) {
                    this.time = `活动时间：${moment(beginTime * 1000).format('MM.DD')} - ${moment(endTime * 1000).format('MM.DD')}`
                    this.countdownTime = endTime;
                }
                break;
            case POP_NOTICE_TIME_TYPE.NO:
                this.showTimeType = POP_NOTICE_TIME_SHOW_TYPE.NO;
                break;
        }
    }

    public setActivityTime(beginTime: number, endTime: number) {
        if(this.param.timeType != POP_NOTICE_TIME_TYPE.USE_ACTIVITY && this.param.timeType != POP_NOTICE_TIME_TYPE.USE_SHOPITEM) return;
        this.setTime(beginTime, endTime);
    } 

    public needActivityTime() {
        return this.param.timeType == POP_NOTICE_TIME_TYPE.USE_ACTIVITY;
    }

    public needShopTime() {
        return this.param.timeType == POP_NOTICE_TIME_TYPE.USE_SHOPITEM;
    }
}