import { getEdmundAbbottV2, IEdmundAbbottBodyV2, IRule } from '@rh-support/api';
import { IAction } from '@rh-support/types';
import map from 'lodash/map';
import { AppRouteSections } from './RouteReducer';

export enum RulesReducerConstants {
    CustomerPortalRuleVisibilityChanged = 'CustomerPortalRuleVisibilityChanged',
    EARuleVisibilityChanged = 'EARuleVisibilityChanged',
    requestEdmundAbbottRules = 'requestEdmundAbbottRules',
    receivedEdmundAbbottRules = 'receivedEdmundAbbottRulesError',
    receivedEdmundAbbottRulesError = 'receivedEdmundAbbottRulesError'
}

export interface IAllRules {
    customerPortalRules: IRules;
    EARules: IRules;
    isLoadingEdmundAbbottRules: boolean;
    isLoadingEdmundAbbottRulesError: boolean;
}

export interface IRuleayloadType extends IAllRules {
    rule: IRule;
    rules: IRule[];
}
type IActionType = IAction<RulesReducerConstants, IRuleayloadType>;
export type RulesDispatchType = (value: IActionType) => void;

export interface IRules {
    sidebarSections: string[];
    mainSections?: string[];
    rules?: IRule[];
}

// export interface IRule {
//     rule_type?: string;
//     rule_sub_type?: string;
//     title?: string;
//     cta?: string;
//     description?: string;
//     visible?: boolean;
// }

export const initialRulesState: IAllRules = {
    customerPortalRules: {
        sidebarSections: [
            AppRouteSections.PRODUCT_SELECTOR,
            AppRouteSections.SUGGESTIONS
        ],
        rules: [{
            title: 'Having an account or billing issue?',
            description: '[Contact Customer Service](/support/contact) for help.',
            cta: '',
            visible: true
        }]
    },
    EARules: {
        sidebarSections: [
            AppRouteSections.ISSUE,
            AppRouteSections.RESULTS,
            AppRouteSections.OPEN_CASE,
            AppRouteSections.IMPROVE_CASE,
            AppRouteSections.CASE_MGM
        ],
        mainSections: [AppRouteSections.SUGGESTIONS],
        rules: []
    },
    isLoadingEdmundAbbottRules: true,
    isLoadingEdmundAbbottRulesError: false
};

export const rulesReducer = (state: IAllRules, action: IActionType): IAllRules => {
    switch (action.type) {

        case RulesReducerConstants.CustomerPortalRuleVisibilityChanged: {
            const portalRules = state.customerPortalRules.rules;
            const newRules = map(portalRules, (rule) => (rule.cta === action.payload.rule.cta) ? {...rule, visible: false} : rule);
            return {...state, customerPortalRules: {...state.customerPortalRules, rules: newRules}};
        }
        case RulesReducerConstants.EARuleVisibilityChanged: {
            const EARules = state.EARules.rules;
            const newRules = map(EARules, (rule) => (rule.cta === action.payload.rule.cta) ? {...rule, visible: false} : rule);
            return {...state, EARules: {...state.EARules, rules: newRules}};
        }
        case RulesReducerConstants.requestEdmundAbbottRules: {
            return { ...state, isLoadingEdmundAbbottRules: true, isLoadingEdmundAbbottRulesError: false };
        }
        case RulesReducerConstants.receivedEdmundAbbottRules: {
            return { ...state, EARules: {...state.EARules, rules: action.payload.rules}, isLoadingEdmundAbbottRules: false, isLoadingEdmundAbbottRulesError: false };
        }
        case RulesReducerConstants.receivedEdmundAbbottRulesError: {
            return { ...state, isLoadingEdmundAbbottRules: false, isLoadingEdmundAbbottRulesError: true };
        }
        default: {
          return state;
        }
    }
};

// Actions
export const setCustomerPortalRulesVisibilityChange = (dispatch: RulesDispatchType, rule: IRule) => {
    dispatch({ type: RulesReducerConstants.CustomerPortalRuleVisibilityChanged, payload: {rule}});
};
export const setEARulesVisibilityChange = (dispatch: RulesDispatchType, rule: IRule) => {
    dispatch({ type: RulesReducerConstants.EARuleVisibilityChanged, payload: {rule}});
};
export const fetchEdmundAbbottRules = async (dispatch: RulesDispatchType, body: Partial<IEdmundAbbottBodyV2>) => {
    dispatch({ type: RulesReducerConstants.requestEdmundAbbottRules });
    try {
        const response = await getEdmundAbbottV2(body);
        const EAResponse: IRule[] = map(response.response, (rule: IRule) => {
            return {...rule, visible: true};
        });
        dispatch({ type: RulesReducerConstants.receivedEdmundAbbottRules, payload: {rules: EAResponse} });
    } catch (error) {
        dispatch({ type: RulesReducerConstants.receivedEdmundAbbottRulesError });
    }
};
