import * as moment from 'moment';
import { DateFormatType } from '../../model/datetime-display/date-format-type';
import { DatetimeSeparatorType } from '../../model/datetime-display/datetime-separator-type';
import { TimeFormatType } from '../../model/datetime-display/time-format-type';
import { ConditionalDisplayElement } from '../base/conditional-display-element';
import { GraphStateElement } from '../shared/graph/graph-state-element';
import { LOGGER_SERVICE_TOKEN } from '../../logger';
import timeZoneOptions from './time-zone-select-json';
export class DatetimeDisplayElement extends ConditionalDisplayElement {
    constructor(element, injector, permissionChecker, variableCommunicator, variableStore, graphStore, signalRAppId) {
        super(element, permissionChecker, variableCommunicator, variableStore, signalRAppId);
        this.permissionChecker = permissionChecker;
        this.variableCommunicator = variableCommunicator;
        this.variableStore = variableStore;
        this.graphStore = graphStore;
        this.timeZoneMap = new Map();
        this.logger = injector.get(LOGGER_SERVICE_TOKEN);
        // 初始化时区
        timeZoneOptions.forEach((item) => {
            this.timeZoneMap.set(item.key, item.utcoffset);
        });
        if (this.model.size && this.model.useGraph) {
            this.initGraph(this.model.size.width, this.model.size.height);
        }
        if (this.model.showDate || this.model.showWeek || this.model.showTime) {
            this.$dateTimeText = this.$element.find('text').last();
            const format = this.getDatetimeFormat(this.model);
            this.getAndSetCurrentTime(format);
            setInterval(() => {
                this.getAndSetCurrentTime(format);
            }, 100);
        }
    }
    getAndSetCurrentTime(format) {
        var _a;
        const currentTimeZone = this.timeZoneMap.get((_a = this.model.timeZone) !== null && _a !== void 0 ? _a : '');
        const content = moment().utcOffset(currentTimeZone !== null && currentTimeZone !== void 0 ? currentTimeZone : this.timeZoneMap.get('')).format(format);
        this.$dateTimeText.text(content);
        let fontF = this.$dateTimeText.css('font-family');
        if (fontF && fontF.lastIndexOf('msyh') === -1) {
            this.$dateTimeText.css('font-family', fontF += ', msyh');
        }
    }
    getDatetimeFormat(displaySetting) {
        let timeFormat = '';
        // 显示4位年份
        const yearFormat = displaySetting.showFullYear ? 'YYYY' : 'YY';
        // 分隔符
        let seperator = '';
        // 日月清零
        const monthFormat = displaySetting.showMonthDayWithoutZero ? 'M' : 'MM';
        const dayFormat = displaySetting.showMonthDayWithoutZero ? 'D' : 'DD';
        if (displaySetting.showDate) {
            switch (displaySetting.datetimeSeparator) {
                case DatetimeSeparatorType.Slash:
                    seperator = '/';
                    break;
                case DatetimeSeparatorType.Line:
                    seperator = '-';
                    break;
                case DatetimeSeparatorType.Dot:
                    seperator = '.';
                    break;
                case DatetimeSeparatorType.CHS:
                    // 年月日格式在构造时候处理
                    break;
            }
            // TODO 方法重写
            timeFormat = this.getFormat(displaySetting.dateFormat, yearFormat, monthFormat, dayFormat, seperator);
        }
        if (displaySetting.showWeek) {
            timeFormat += ' ddd';
        }
        if (displaySetting.showTime) {
            timeFormat += ' ';
            if (displaySetting.isTwelveHourHex) {
                timeFormat += 'A ';
                switch (displaySetting.timeFormat) {
                    case TimeFormatType.HHMMSS:
                        timeFormat += 'h:mm:ss';
                        break;
                    case TimeFormatType.HHMM:
                        timeFormat += 'h:mm';
                        break;
                }
            }
            else {
                switch (displaySetting.timeFormat) {
                    case TimeFormatType.HHMMSS:
                        timeFormat += 'HH:mm:ss';
                        break;
                    case TimeFormatType.HHMM:
                        timeFormat += 'HH:mm';
                        break;
                }
            }
        }
        return timeFormat;
    }
    initGraph(width, height) {
        if (this.model.useGraph === true) {
            const graphStateElement = new GraphStateElement(this.model.graphSetting, width, height, this.graphStore, this.logger);
            graphStateElement.switchToState(0);
            this.$element.prepend(graphStateElement.Element);
        }
    }
    getFormat(dateFormatType, yearFormat, monthFormat, dayFormat, seperator) {
        const yearSeperator = '年';
        const monthSeperator = '月';
        const daySeperator = '日';
        let _dateFormat = '';
        if (seperator === '') {
            switch (dateFormatType) {
                case DateFormatType.YYMMDD:
                    _dateFormat = yearFormat + yearSeperator + monthFormat + monthSeperator + dayFormat + daySeperator;
                    break;
                case DateFormatType.MMDDYY:
                    _dateFormat = monthFormat + monthSeperator + dayFormat + daySeperator + yearFormat + yearSeperator;
                    break;
                case DateFormatType.DDMMYY:
                    _dateFormat = dayFormat + daySeperator + monthFormat + monthSeperator + yearFormat + yearSeperator;
                    break;
            }
        }
        else {
            switch (dateFormatType) {
                case DateFormatType.YYMMDD:
                    _dateFormat = yearFormat + seperator + monthFormat + seperator + dayFormat;
                    break;
                case DateFormatType.MMDDYY:
                    _dateFormat = monthFormat + seperator + dayFormat + seperator + yearFormat;
                    break;
                case DateFormatType.DDMMYY:
                    _dateFormat = dayFormat + seperator + monthFormat + seperator + yearFormat;
                    break;
            }
        }
        return _dateFormat;
    }
}
