import BaseComponent from "../BaseComponent";
import EventBus, { Events, Event } from "../../Events/EventBus";


class SleepMode {
    init() {
        if (!jQuery('.sleepmode-ui')[0]) {
            return;
        }
        if (jQuery('.sleepmode-ui').data('initializied') !== 'true') {
            new SleepModeHandler(jQuery('.sleepmode-ui'));
        }
    }
}

class SleepModeHandler extends BaseComponent {

    _isAnimate = false;

    constructor(component_base_ui) {
        super(component_base_ui);
        this._handleEvents();
    }

    _handleEvents() {
        const _this = this;
        this._component_base_ui.on('click', function(event) {
            event.preventDefault();
            _this._exitSleepMode();
        })

        const goToSleepMode = new Event(
            Events.GO_TO_SLEEP_MODE,
            (payload) => {
                this._goToSleepMode(payload);
            }
        );
        EventBus.getInstance().registerEvent(goToSleepMode);
    }

    _goToSleepMode(payload) {
        if (this._isAnimate) {
            return;
        }
        this._isAnimate = true;
        this._component_base_ui.css('opacity', 0);
        this._component_base_ui.removeClass('hidden');
        this._component_base_ui.animate({
            opacity: 1
        }, 1000, () => {
            this._isAnimate = false;
        })
    }

    _exitSleepMode() {
        if (this._isAnimate) {
            return;
        }

        this._isAnimate = true;
        
        this._component_base_ui.animate({
            opacity: 0
        }, 1000, () => {
            this._isAnimate = false;
            this._component_base_ui.addClass('hidden');
            EventBus.getInstance().triggerEvent(
                Events.WAKE_UP, {}
            );
        })
    }

}

export default SleepMode;