import {combineReducers} from 'redux';

import {GenericAction} from 'mattermost-redux/types/actions';

import ActionTypes from '../action_types';
import {RHS_STATE_MY} from '../constants';

function showRHS(state = null, action: GenericAction) {
    switch (action.type) {
    case ActionTypes.RECEIVED_SHOW_RHS_ACTION:
        return action.showRHSPluginAction;
    default:
        return state;
    }
}

function rhsView(state = RHS_STATE_MY, action: GenericAction) {
    switch (action.type) {
    case ActionTypes.RECEIVED_RHS_VIEW:
        return action.data;
    default:
        return state;
    }
}

function prevRhsView(state = RHS_STATE_MY, action: GenericAction) {
    switch (action.type) {
    case ActionTypes.RECEIVED_RHS_VIEW:
        return action.prevView || state;
    default:
        return state;
    }
}

function rhsUser(state = null, action: GenericAction) {
    switch (action.type) {
    case ActionTypes.RECEIVED_RHS_USER:
        return action.data;
    default:
        return state;
    }
}

function rhsBadge(state = null, action: GenericAction) {
    switch (action.type) {
    case ActionTypes.RECEIVED_RHS_BADGE:
        return action.data;
    default:
        return state;
    }
}

function rhsTypeId(state = null, action: GenericAction) {
    switch (action.type) {
    case ActionTypes.RECEIVED_RHS_TYPE:
        return action.data.typeId;
    default:
        return state;
    }
}

function rhsTypeName(state = null, action: GenericAction) {
    switch (action.type) {
    case ActionTypes.RECEIVED_RHS_TYPE:
        return action.data.typeName;
    default:
        return state;
    }
}

function createBadgeModalVisible(state = false, action: GenericAction) {
    switch (action.type) {
    case ActionTypes.OPEN_CREATE_BADGE_MODAL:
        return true;
    case ActionTypes.CLOSE_CREATE_BADGE_MODAL:
        return false;
    default:
        return state;
    }
}

function editBadgeModalData(state = null, action: GenericAction) {
    switch (action.type) {
    case ActionTypes.OPEN_EDIT_BADGE_MODAL:
        return action.data;
    case ActionTypes.CLOSE_EDIT_BADGE_MODAL:
        return null;
    default:
        return state;
    }
}

function createTypeModalVisible(state = false, action: GenericAction) {
    switch (action.type) {
    case ActionTypes.OPEN_CREATE_TYPE_MODAL:
        return true;
    case ActionTypes.CLOSE_CREATE_TYPE_MODAL:
        return false;
    default:
        return state;
    }
}

function editTypeModalData(state = null, action: GenericAction) {
    switch (action.type) {
    case ActionTypes.OPEN_EDIT_TYPE_MODAL:
        return action.data;
    case ActionTypes.CLOSE_EDIT_TYPE_MODAL:
        return null;
    default:
        return state;
    }
}

function grantModalData(state = null, action: GenericAction) {
    switch (action.type) {
    case ActionTypes.OPEN_GRANT_MODAL:
        return action.data || {};
    case ActionTypes.CLOSE_GRANT_MODAL:
        return null;
    default:
        return state;
    }
}

function subscriptionModalData(state = null, action: GenericAction) {
    switch (action.type) {
    case ActionTypes.OPEN_SUBSCRIPTION_MODAL:
        return action.data;
    case ActionTypes.CLOSE_SUBSCRIPTION_MODAL:
        return null;
    default:
        return state;
    }
}

export default combineReducers({
    showRHS,
    rhsView,
    prevRhsView,
    rhsUser,
    rhsBadge,
    rhsTypeId,
    rhsTypeName,
    createBadgeModalVisible,
    editBadgeModalData,
    createTypeModalVisible,
    editTypeModalData,
    grantModalData,
    subscriptionModalData,
});
