import update from 'immutability-helper';

import { AppConstants } from '../constants/AppConstants.js';
import { EnumAccountActions } from '../ui/account/AccountActions.js';
import { EnumAppActions } from './AppActions.js';


const initialState = {
    locate: AppConstants.DEFAULT_LANG,
    listOfNotify: null,
    jsessionId: null,

    environment: null,
    isSSO: false,
    serverid: null,

    loadingStack: 0,
    loadingState: 'hide',

    authorizations: {},

    domini: {},
    dominiLoaded: false,

    i18nlabels: {},
    i18nlabelsLoaded: true, // TODO riportare a false e nel loading ri-set to true

    opzioni: {},
    opzioniLoaded: false,

    loggedOut: false,
    versionInformation: null,
    debugObj: null,
    showPopUp: false,

    alog: null,
    showAlogPopup: false,

    ritardoCaricamentoFlussi: false
};

const app = (state, action) => {
    if (typeof state === 'undefined') {
        return initialState
    }

    let notifyCollection = null;

    switch (action.type) {

        case EnumAccountActions.LOGIN_SUCCESS:
        case EnumAccountActions.SSO:

            let authorizations = action.authorizations;
            let jsessionId = action.jsessionId;
            let environment = null;

            let isSSO = false;

            if (action.type === EnumAccountActions.SSO) {
                isSSO = action.datiUtente.userId !== null && action.datiUtente.userId !== undefined;
            }

            if (action.datiUtente) {
                authorizations = action.datiUtente.authorizations;
                jsessionId = action.datiUtente.jsessionId;
                environment = action.datiUtente.environment;
            }

            if (action.user) {
                authorizations = action.user.authorizations;
                jsessionId = action.user.jsessionId;
            }

            let authMap = {};
            if (authorizations) {
                authorizations.forEach(element => {
                    authMap[element] = element;
                });
            }

            return update(state, {
                authorizations: { $set: authMap },
                jsessionId: { $set: jsessionId },
                environment: { $set: environment },
                isSSO: { $set: isSSO },
                listOfNotify: { $set: null }
            });

        case EnumAccountActions.LOGOUT:
            return update(state, {
                loggedOut: { $set: true },
                datiUtente: { $set: {} }
            });


        case EnumAppActions.NOTIFY_MESSAGE_ADD:
            if (state.listOfNotify === null) {
                notifyCollection = new Map();
            } else {
                //devo clonare la lista altrimenti non vengono intercettati i cambiamenti
                notifyCollection = new Map(state.listOfNotify);
            }

            if (action.messageId === null || action.messageId === undefined) {
                action.messageId = new Date().getTime();
            }

            notifyCollection.set(Number(action.messageId), {
                message: action.message,
                messageType: action.messageType,
                messageId: action.messageId
            });

            return update(state, { listOfNotify: { $set: notifyCollection } });

        case EnumAppActions.NOTIFY_MESSAGE_DELETE:
            //devo clonare la lista altrimenti non vengono intercettati i cambiamenti
            notifyCollection = new Map(state.listOfNotify);
            notifyCollection.delete(action.messageId);

            return update(state, { listOfNotify: { $set: notifyCollection } });

        case EnumAppActions.NOTIFY_MESSAGE_CLEAR:
            return update(state, { listOfNotify: { $set: null } });

        case EnumAppActions.LOAD_DOMINI:
            const domini = update(state.domini, { $merge: action.domini });

            return update(state, {
                domini: { $set: domini },
                dominiLoaded: { $set: true }
            });

        case EnumAppActions.LOAD_OPZIONI:
            const opzioni = update(state.opzioni, { $merge: action.opzioni });

            return update(state, {
                opzioni: { $set: opzioni },
                opzioniLoaded: { $set: true }
            });

        case EnumAppActions.LOAD_I18N:
            return update(state, {
                i18nlabels: { $merge: action.i18nlabels }
            });

        case EnumAppActions.WINDOW_WIDTH_CHANGED:
            return update(state, { deviceSize: { $set: action.deviceSize } });

        case EnumAppActions.LOADER_SHOW:
            if (state.loadingState === 'show') {
                return update(state, { loadingStack: { $set: state.loadingStack + 1 } });
            }

            return update(state, {
                loadingState: { $set: "show" },
                loadingStack: { $set: 1 }
            });

        case EnumAppActions.LOADER_HIDE:
            if (state.loadingState === 'hide') {
                return update(state, {
                    loadingState: { $set: "hide" },
                    loadingStack: { $set: 0 }
                });
            }

            if (state.loadingStack > 1) {
                return update(state, {
                    loadingStack: { $set: state.loadingStack - 1 }
                });
            }

            return update(state, {
                loadingState: { $set: "hide" },
                loadingStack: { $set: 0 }
            });

        case EnumAppActions.LOCATE_CHANGE:
            return update(state, { locate: { $set: action.locate } });

        case EnumAppActions.LOADING_DELAY_STREAMS:
            return update(state, { ritardoCaricamentoFlussi: { $set: action.ritardoCaricamentoFlussi } });

        default:
            return state;
    }
}

export default app;