import { i18n } from 'arm-common';
import { NotificationManager } from 'arm-core-layouts';

import { AppConstants } from '../constants/AppConstants.js';
import { DOMAINS_PRELOGIN } from '../constants/Domini.js';
import OPZIONI from '../constants/Opzioni.js';
import AccountActions, { EnumAccountActions } from '../ui/account/AccountActions.js';
import Rest from '../webapi/Rest.js';
import BaseActions from './BaseActions.js';


export const EnumAppActions = Object.freeze({
    WINDOW_WIDTH_CHANGED: 'AppActions.WINDOW_WIDTH_CHANGED',
    NOTIFY_MESSAGE_ADD: 'AppActions.NOTIFY_MESSAGE_ADD',
    NOTIFY_MESSAGE_DELETE: 'AppActions.NOTIFY_MESSAGE_DELETE',
    NOTIFY_MESSAGE_CLEAR: 'AppActions.NOTIFY_MESSAGE_CLEAR',
    LOADER_SHOW: 'AppActions.LOADER_SHOW',
    LOADER_HIDE: 'AppActions.LOADER_HIDE',
    LOADER_USER_SHOW: 'AppActions.LOADER_USER_SHOW',
    LOADER_USER_HIDE: 'AppActions.LOADER_USER_HIDE',
    LOAD_OPZIONI: 'AppActions.LOAD_OPZIONI',
    LOAD_DOMINI: 'AppActions.LOAD_DOMINI_APP',
    LOAD_I18N: 'AppActions.LOAD_I18N',

    LOCATE_CHANGE: 'AppActions.LOCATE_CHANGE',
    CHANGE_URL: 'AppActions.CHANGE_URL',

    LOADING_DELAY_STREAMS: 'AppActions.app_actions.LOADING_DELAY_STREAMS',
});




export const EnumNotifyType = Object.freeze({
    Success: 'SUCCESS',
    Info: 'INFO',
    Warning: 'WARNING',
    Danger: 'DANGER'
});


class AppActions extends BaseActions {
    getReducer(getState) {
        return getState().app;
    }

    preLoginLoadUtilities() {
        return function (dispatch) {
            const utilities = {
                codiciOpzioni: OPZIONI,
                codiciDomini: DOMAINS_PRELOGIN
            };
            return dispatch(AccountActions.loadUtilities(utilities));
        }
    }

    addNotify(tipo, messaggio) {
        switch (tipo) {
            case (EnumNotifyType.Info):
                NotificationManager.info(messaggio);
                break;
            case (EnumNotifyType.Success):
                NotificationManager.success(messaggio);
                break;
            case (EnumNotifyType.Warning):
                NotificationManager.warning(messaggio, { timeOut: 10000 });
                break;
            case (EnumNotifyType.Danger):
                NotificationManager.error(messaggio, { timeOut: 0 });
                break;
        }
    }

    deleteNotify(id) {
        return { type: EnumAppActions.NOTIFY_MESSAGE_DELETE, messageId: id };
    }

    clearNotify() {
        return { type: EnumAppActions.NOTIFY_MESSAGE_CLEAR }
    }
}

export default new AppActions();