import {
    RECEIVE_SHOP_CART_COUNT,
    REQUEST_SHOP_CART_ITEMS,
    RECEIVE_SHOP_CART_ITEMS,
    SET_CURRENT_STEP,
    RESET_STEPS,
    GO_TO_NEXT_STEP,
    GO_TO_PREVIOUS_STEP,
    LOAD_CART_STEPS,
    REQUEST_CART_DOCUMENTS,
    RECEIVE_CART_DOCUMENTS,
    REQUEST_CUSTOMER_DETAILS,
    RECEIVE_CUSTOMER_DETAILS,
    SELECT_COUNTRY_DELIVERY,
    SELECT_COUNTRY_BILLING,
    SET_ORDER_INFO,
    IS_CART_ITEMS_DISABLED,
    RECEIVE_ORDER_TOTAL_PRICE,
    UPDATE_CART_ITEMS,
    SET_NEXT_STEP,
    SET_PREV_STEP,
    SET_ORDER_PLACED,
    SET_ORDER_PLACED_REDIRECT
} from '../../constants/cartConstants';
import PriceHelper from '../../helpers/coMarket/PriceHelper';

const priceHelper = new PriceHelper();
const moduleReducers = {};

const calculateTotalPrice = (cartItems) => {
    let fixedPrice = priceHelper.sumPrices(cartItems.map(item => { return item.quantity * item.totalPrices.fixedPrice}));
    let recurrentPrice = priceHelper.sumPrices(cartItems.map(item => { return item.quantity * item.totalPrices.recurrentPrice})) || 0;
    let servicesPrice = priceHelper.sumPrices(cartItems.map(item => { return item.quantity * item.totalPrices.servicesPrice})) || 0;

    return  {
        fixedPrice,
        recurrentPrice: priceHelper.sumPrices([recurrentPrice, servicesPrice]),
        periodUnit: cartItems[0].periodUnit,
        currency: cartItems[0].country.currency
    }
}

moduleReducers[RECEIVE_SHOP_CART_COUNT] = (state, action) => {
    return Object.assign({}, state, {
        cartCount: action.cartCount
    });
};

moduleReducers[UPDATE_CART_ITEMS] = (state, action) => {
    const newState = Object.assign({}, state);
    newState.cartItems.forEach((item) => {
        if(item.idCart === action.newItem.idCart){
            item = action.newItem;
        }
     });

    return Object.assign({}, state, {
        cartItems: newState.cartItems,
        orderTotalPrice: calculateTotalPrice(newState.cartItems)
    });
};

moduleReducers[REQUEST_SHOP_CART_ITEMS] = (state, action) => {
    return Object.assign({}, state, {
        isLoading: action.isLoading
    });
};

moduleReducers[RECEIVE_SHOP_CART_ITEMS] = (state, action) => {
    return Object.assign({}, state, {
        cartItems: action.cartItems,
        isLoading: action.isLoading
    });
};

moduleReducers[LOAD_CART_STEPS] = (state, action) => {
    return Object.assign({}, state, {
        cartSteps: action.cartSteps
    });
};

moduleReducers[REQUEST_CUSTOMER_DETAILS] = (state, action) => {
    return Object.assign({}, state, {
        isLoading: action.isLoading
    });
};


moduleReducers[RECEIVE_CUSTOMER_DETAILS] = (state, action) => {
    return Object.assign({}, state, {
        customerDetails: action.customerDetails,
        isLoading: action.isLoading
    });
};

moduleReducers[SET_CURRENT_STEP] = (state, action) => {
    const cartSteps = Object.assign({}, state.cartSteps);

    if(state.currentStep && cartSteps[state.currentStep]){
        cartSteps[state.currentStep].status = 'completed';
    }
    cartSteps[action.currentStep].status = 'active';

    return Object.assign({}, state, {
        currentStep: action.currentStep,
        cartSteps
    });
};

moduleReducers[RESET_STEPS] = (state, action) => {
    const cartSteps = Object.assign({}, state.cartSteps);
    Object.keys(cartSteps).forEach(stepName => {
        cartSteps[stepName].status = cartSteps[stepName].previous ? 'inactive' : 'active';
    });

    return Object.assign({}, state, {
        cartSteps
    });
};

moduleReducers[GO_TO_NEXT_STEP] = (state, action) => {
    const cartSteps = Object.assign({}, state.cartSteps);
    const nextStep = cartSteps[state.currentStep].next || '';

    if(nextStep && nextStep !== ''){
        cartSteps[nextStep].status = 'active';
        cartSteps[state.currentStep].status = 'completed';
    }

    return Object.assign({}, state, {
        currentStep: nextStep,
        cartSteps
    });
};

moduleReducers[GO_TO_PREVIOUS_STEP] = (state, action) => {
    const cartSteps = Object.assign({}, state.cartSteps);
    const previousStep = cartSteps[state.currentStep].previous || '';

    if(previousStep && previousStep !== ''){
        cartSteps[previousStep].status = 'active';
        cartSteps[state.currentStep].status = 'completed';
    }

    return Object.assign({}, state, {
        currentStep: previousStep,
        cartSteps
    });
};

moduleReducers[REQUEST_CART_DOCUMENTS] = (state, action) => {
    return Object.assign({}, state, {
        isLoading: action.isLoading
    });
};


moduleReducers[RECEIVE_CART_DOCUMENTS] = (state, action) => {
    return Object.assign({}, state, {
        cartDocuments: action.cartDocuments,
        isLoading: action.isLoading
    });
};

moduleReducers[SELECT_COUNTRY_DELIVERY] = (state, action) => {
    return Object.assign({}, state, {
        selectedCountryDelivery: action.selectedCountryDelivery
    });
};

moduleReducers[SELECT_COUNTRY_BILLING] = (state, action) => {
    return Object.assign({}, state, {
        selectedCountryBilling: action.selectedCountryBilling
    });
};

moduleReducers[SET_ORDER_INFO] = (state, action) => {
    return Object.assign({}, state, {
        orderInfo: action.orderInfo
    });
};

moduleReducers[IS_CART_ITEMS_DISABLED] = (state, action) => {
    return Object.assign({}, state, {
        isCartItemsDisabled: action.isCartItemsDisabled
    });
};

moduleReducers[RECEIVE_ORDER_TOTAL_PRICE] = (state, action) => {
    return Object.assign({}, state, {
        orderTotalPrice: calculateTotalPrice(action.cartItems)
    });
};

moduleReducers[SET_NEXT_STEP] = (state, action) => {
    return Object.assign({}, state, {
        nextStepAction: action.nextStepAction
    });
};

moduleReducers[SET_PREV_STEP] = (state, action) => {
    return Object.assign({}, state, {
        prevStepAction: action.prevStepAction
    });
};

moduleReducers[SET_ORDER_PLACED] = (state, action) => {
    return Object.assign({}, state, {
        orderPlaced: action.orderPlaced
    });
};

moduleReducers[SET_ORDER_PLACED_REDIRECT] = (state, action) => {
    return Object.assign({}, state, {
        orderPlacedRedirect: action.orderPlacedRedirect
    });
};

const cartReducer = (state = {}, action) => {
    return moduleReducers[action.type] ?
        moduleReducers[action.type](state, action) :
        state;
};

export default cartReducer;
