import moment from 'moment';
import { TimerUtil } from '@ibizstudio/runtime';
import './notification-signal.less';
/**
 * 通知信号指令
 *
 * @export
 * @class NotificationSignal
 */
export const NotificationSignal = {
    /**
     * 指定初始化
     *
     * @param {HTMLDivElement} el
     * @param {*} binding
     * @param {VNode} vNode
     * @param {VNode} oldVNode
     */
    bind(el, binding, vNode, oldVNode) {
        nsc.init(el);
    },
    /**
     * 指令更新
     *
     * @param {HTMLDivElement} el
     * @param {*} binding
     * @param {VNode} vNode
     * @param {VNode} oldVNode
     */
    componentUpdated(el, binding, vNode, oldVNode) {
        if (binding.value != binding.oldValue) {
            if (binding.value == true) {
                nsc.loading();
            }
            else if (binding.value == false) {
                nsc.loadingEnd();
            }
        }
    }
};
/**
 * 信号闪烁控制器
 *
 * @export
 * @class NotificationSignal
 */
export class NotificationSignalController {
    /**
     * Creates an instance of NotificationSignalController.
     * @memberof NotificationSignalController
     */
    constructor() {
        /**
         * 加载计数
         *
         * @protected
         * @type {number}
         * @memberof NotificationSignalController
         */
        this.loadingCount = 0;
        /**
         * 加载动画宽度百分比
         *
         * @protected
         * @type {number}
         * @memberof NotificationSignalController
         */
        this.loadingWidth = 0;
        /**
         * 是否加载动画中
         *
         * @protected
         * @type {boolean}
         * @memberof NotificationSignalController
         */
        this.isLoading = false;
        if (NotificationSignalController.instance) {
            return NotificationSignalController.instance;
        }
    }
    /**
     * 初始化
     *
     * @param {HTMLDivElement} el
     * @memberof NotificationSignalController
     */
    init(el) {
        el.classList.add('nsc-container');
        const div = document.createElement('div');
        div.classList.add('nsc-content');
        el.appendChild(div);
        this.el = div;
    }
    /**
     * 成功
     *
     * @param {number} [num=1500]
     * @memberof NotificationSignalController
     */
    success(num = 1500) {
        this.active();
        this.el.classList.add('success');
        TimerUtil.timeout(() => {
            this.inactive();
            this.el.classList.remove('success');
        }, num, 'nsc-success');
    }
    /**
     * 警告
     *
     * @param {number} [num=1500]
     * @memberof NotificationSignalController
     */
    waring(num = 1500) {
        this.active();
        this.el.classList.add('waring');
        TimerUtil.timeout(() => {
            this.inactive();
            this.el.classList.remove('waring');
        }, num, 'nsc-waring');
    }
    /**
     * 错误
     *
     * @param {number} [num=1500]
     * @memberof NotificationSignalController
     */
    error(num = 1500) {
        this.active();
        this.el.classList.add('error');
        TimerUtil.timeout(() => {
            this.inactive();
            this.el.classList.remove('error');
        }, num, 'nsc-error');
    }
    /**
     * 激活态
     *
     * @protected
     * @memberof NotificationSignalController
     */
    active() {
        this.el.classList.add('active');
        this.el.classList.remove('inactive');
    }
    /**
     * 无活动态
     *
     * @protected
     * @memberof NotificationSignalController
     */
    inactive() {
        this.el.classList.add('inactive');
        this.el.classList.remove('active');
    }
    /**
     * 显示加载态
     *
     * @memberof NotificationSignalController
     */
    loading() {
        if (this.loadingCount === 0 && this.el) {
            if (!this.isLoading) {
                this.active();
                this.loadingWidth = 0;
                this.el.classList.add('loading');
                this.setElWidth(this.loadingWidth);
                this.isLoading = true;
                this.loadingChange();
                this.loadingInterval = setInterval(() => {
                    this.loadingChange();
                }, 300);
            }
            else {
                this.loadingCount++;
                this.loadingEnd();
                return;
            }
        }
        this.loadingCount++;
    }
    /**
     * 加载动画变更
     *
     * @protected
     * @memberof NotificationSignalController
     */
    loadingChange() {
        this.loadingWidth += Math.abs(Math.random() * 5);
        if (this.loadingWidth >= 99) {
            this.loadingWidth = 99;
            this.clearLoadingInterval();
        }
        this.setElWidth(this.loadingWidth);
    }
    /**
     * 结束加载态
     *
     * @memberof NotificationSignalController
     */
    loadingEnd() {
        if (this.loadingCount > 0) {
            this.loadingCount--;
        }
        else {
            this.loadingCount = 0;
        }
        if (this.loadingCount === 0 && this.el) {
            this.clearLoadingInterval();
            this.setElWidth(100);
            this.clearLoadingEndTimer();
            this.loadingEndTimer = setTimeout(() => {
                this.isLoading = false;
                this.inactive();
                this.el.classList.remove('loading');
            }, 300);
        }
    }
    /**
     * 清除加载动画定时器
     *
     * @protected
     * @memberof NotificationSignalController
     */
    clearLoadingInterval() {
        if (this.loadingInterval) {
            clearInterval(this.loadingInterval);
            this.loadingInterval = null;
        }
    }
    /**
     * 清除结束定时器
     *
     * @protected
     * @memberof NotificationSignalController
     */
    clearLoadingEndTimer() {
        if (this.loadingEndTimer) {
            clearTimeout(this.loadingEndTimer);
            this.loadingEndTimer = null;
        }
    }
    /**
     * 设置容器宽度百分比
     *
     * @protected
     * @param {number} width
     * @memberof NotificationSignalController
     */
    setElWidth(width) {
        this.el.style.width = width + '%';
    }
    /**
     * 获取当前时间
     *
     * @protected
     * @returns {string}
     * @memberof NotificationSignalController
     */
    getLocalDate() {
        return moment().format('MM-DD HH:mm:ss');
    }
    /**
     * 获取唯一实例
     *
     * @static
     * @returns {NotificationSignalController}
     * @memberof NotificationSignalController
     */
    static getInstance() {
        return NotificationSignalController.instance;
    }
}
/**
 * 唯一实例
 *
 * @private
 * @static
 * @memberof NotificationSignalController
 */
NotificationSignalController.instance = new NotificationSignalController();
// 导出服务
export const nsc = NotificationSignalController.getInstance();
