import {AnyAction, Dispatch} from 'redux';

import ActionTypes from 'action_types/';
import {BadgeDetails, BadgeID, BadgeTypeDefinition} from 'types/badges';
import {GrantModalData, RHSState, SubscriptionModalData} from 'types/general';
import {id as pluginId} from '../manifest';

/**
 * Stores`showRHSPlugin` action returned by
 * registerRightHandSidebarComponent in plugin initialization.
 */
export function setShowRHSAction(showRHSPluginAction: () => void) {
    return {
        type: ActionTypes.RECEIVED_SHOW_RHS_ACTION,
        showRHSPluginAction,
    };
}

export function setRHSUser(userID: string | null) {
    return {
        type: ActionTypes.RECEIVED_RHS_USER,
        data: userID,
    };
}

export function setRHSBadge(badgeID: BadgeID | null) {
    return {
        type: ActionTypes.RECEIVED_RHS_BADGE,
        data: badgeID,
    };
}

export function setRHSView(view: RHSState) {
    return (dispatch: Dispatch<AnyAction>, getState: () => any) => {
        const state = getState();
        const pluginState = state['plugins-' + pluginId];
        const currentView = pluginState?.rhsView;
        dispatch({
            type: ActionTypes.RECEIVED_RHS_VIEW,
            data: view,
            prevView: currentView,
        });
        return {data: true};
    };
}

export function setRHSType(typeId: number | null, typeName: string | null) {
    return {
        type: ActionTypes.RECEIVED_RHS_TYPE,
        data: {typeId, typeName},
    };
}

export function openGrant(user?: string, badge?: string) {
    return (dispatch: Dispatch<AnyAction>) => {
        dispatch(openGrantModal({prefillUser: user, prefillBadgeId: badge}));
        return {data: true};
    };
}

export function openCreateType() {
    return (dispatch: Dispatch<AnyAction>) => {
        dispatch(openCreateTypeModal());
        return {data: true};
    };
}

export function openCreateBadge() {
    return (dispatch: Dispatch<AnyAction>) => {
        dispatch(openCreateBadgeModal());
        return {data: true};
    };
}

export function openCreateBadgeModal() {
    return {type: ActionTypes.OPEN_CREATE_BADGE_MODAL};
}

export function closeCreateBadgeModal() {
    return {type: ActionTypes.CLOSE_CREATE_BADGE_MODAL};
}

export function openEditBadgeModal(badge: BadgeDetails) {
    return {type: ActionTypes.OPEN_EDIT_BADGE_MODAL, data: badge};
}

export function closeEditBadgeModal() {
    return {type: ActionTypes.CLOSE_EDIT_BADGE_MODAL};
}

export function openCreateTypeModal() {
    return {type: ActionTypes.OPEN_CREATE_TYPE_MODAL};
}

export function closeCreateTypeModal() {
    return {type: ActionTypes.CLOSE_CREATE_TYPE_MODAL};
}

export function openEditTypeModal(badgeType: BadgeTypeDefinition) {
    return {type: ActionTypes.OPEN_EDIT_TYPE_MODAL, data: badgeType};
}

export function closeEditTypeModal() {
    return {type: ActionTypes.CLOSE_EDIT_TYPE_MODAL};
}

export function openGrantModal(data?: GrantModalData) {
    return {type: ActionTypes.OPEN_GRANT_MODAL, data: data || {}};
}

export function closeGrantModal() {
    return {type: ActionTypes.CLOSE_GRANT_MODAL};
}

export function openSubscriptionModal(data: SubscriptionModalData) {
    return {type: ActionTypes.OPEN_SUBSCRIPTION_MODAL, data};
}

export function closeSubscriptionModal() {
    return {type: ActionTypes.CLOSE_SUBSCRIPTION_MODAL};
}

export function openAddSubscription() {
    return (dispatch: Dispatch<AnyAction>) => {
        dispatch(openSubscriptionModal({mode: 'create'}));
        return {data: true};
    };
}

export function openRemoveSubscription() {
    return (dispatch: Dispatch<AnyAction>) => {
        dispatch(openSubscriptionModal({mode: 'delete'}));
        return {data: true};
    };
}

