import AbstractUIService from '../../Framework/Services/AbstractUIService';
import {
    dismissError,
    reportError,
    setVisible,
    setWorkInProgress,
    alertMessage,
    dismissAlert,
} from '../Reducers/UI/Actions';
import { Services } from '../ServiceLocator';

class Work {
    constructor(name) {
        this.name = name;
    }

    done() {
        Services.ui.endWork(this.name);
    }
}

export default class UIService extends AbstractUIService {
    show(name) {
        Services.store.dispatch(setVisible(name, true));
    }

    hide(name) {
        Services.store.dispatch(setVisible(name, false));
    }

    isVisible(name) {
        return Boolean(Services.store.state.ui.visibilityMap[name]);
    }

    toggle(name, show) {
        if (show === undefined) {
            show = !Services.ui.isVisible(name);
        }
        Services.store.dispatch(setVisible(name, show));
    }

    beginWork(name) {
        Services.store.dispatch(setWorkInProgress(name, true));
        return new Work(name);
    }

    endWork(name) {
        Services.store.dispatch(setWorkInProgress(name, false));
    }

    reportError(errorMessage) {
        Services.store.dispatch(reportError(errorMessage));
    }

    dismissError() {
        if (Services.store.state.ui.error) {
            Services.store.dispatch(dismissError());
        }
    }

    alertMessage(message) {
        Services.store.dispatch(alertMessage(message));
    }

    dismissAlert() {
        Services.store.dispatch(dismissAlert());
    }
}
