import {
    doFetchProductsList,
    doFetchFilterOptions,
    doFetchAttributeFilterProducts,
    doFetchSortProducts,
    doFetchSearchProducts,
    doFetchAttributeList,
    doPostCartItem,
    doFetchQuote,
    doFetchCartProducts,
    doFetchTotalAmt,
    doDeleteCartProduct,
    doFetchRecentlyViewedList,
    doSideBarFilter
} from '@/services/product.service';
import { 
     doFetchProductList
 } from '@/services/cache.service';
 import { 
    getProductArray,getProductsList
} from '@/services/util.service';
import { getField, updateField } from 'vuex-map-fields';
import humps from 'lodash-humps';

export const productList = {
    namespaced: true,
    state: {
        inProgress: false,
        productData: { items: [] },
        filterList: {},
        attributeList: {},
        guestQuote: [],
        cartProduct: [],
        cartProductList: [],
        productTotalAmt: [],
        recentlyViewed: [],
        sideBarFilterDetails: {},
        inProgressSideBarFilter: false,
        emptyProductList: false,
    },
    getters: {
        getField
    },
    mutations: {
        updateField,

        inProgress(state, yesOrNo) {
            state.inProgress = yesOrNo
        },
        setProductsDetails(state, list) {
            state.productData = list
        },
        setEmptyProductList(state, flag) {
            state.emptyProductList = flag
        },
        setFilterList(state, list) {
            state.filterList = list
        },
        setAttributeList(state, attributeList) {
            state.attributeList = attributeList
        },
        setGuestQuote(state, quoteId) {
            state.guestQuote = quoteId
        },
        setCartProduct(state, product) {
            state.cartProduct = product
        },
        setCartProductList(state, list) {
            state.cartProductList = list
        },
        setProductAmt(state, amt) {
            state.productTotalAmt = amt;
        },
        setRecentlyViewedList(state, list) {
            state.recentlyViewed = list
        },
        setSideBarFilterDetails(state, list) {
            state.sideBarFilterDetails = list
        },
        inProgressSideBarFilter(state, yesOrNo) {
            state.inProgressSideBarFilter = yesOrNo
        },
    },
    actions: {
        fetchProductsList: async({ commit }) => {
            const productData = await doFetchProductsList();
            if (productData) {
                commit('setProductsDetails', humps(productData));
                commit('setEmptyProductList', false);
            } else {
                commit('setEmptyProductList', true);
            }
        },

        fetchFilterOptions: async({ commit }) => {
            const filterList = await doFetchFilterOptions();
            if (filterList) {
                commit('setFilterList', humps(filterList));
            }
        },

        fetchFilterProducts: async({ commit }, id) => {
            const productData = await doFetchAttributeFilterProducts(id.attributeCode, id.value);
            if (productData) {
                commit('setProductsDetails', humps(productData));
                commit('setEmptyProductList', false);
            } else {
                commit('setEmptyProductList', true);
            }
        },
        fetchRecentlyViewedList: async({ commit }, skus) => {
            const recentlyViewed = await doFetchRecentlyViewedList(skus);
            if (recentlyViewed) {
                commit('setRecentlyViewedList', humps(recentlyViewed));
            }
        },

        fetchSearchProducts: async({ commit }, search) => {
            const productData = await doFetchSearchProducts(search);
            if (productData) {
                commit('setProductsDetails', humps(productData));
                commit('setEmptyProductList', false);
            } else {
                commit('setEmptyProductList', true);
            }
        },
        /*eslint-disable*/
        fetchSortProducts: async({ commit }, pageDetails) => {
            commit('inProgress', true);
            const productData = await doFetchSortProducts(pageDetails.page, pageDetails.size, pageDetails.sortBy, pageDetails.categoryId, pageDetails.search, pageDetails.sideFilter);
            if (productData) {
                let categoryIds = pageDetails.categoryId.split(",");
                let sortedProducts = new Array(categoryIds.length).fill("");
                for (let i = 0; i < productData.items.length; i++) {
                    let product = productData.items[i];
                    let productIndex = categoryIds.indexOf(product.id + "");
                    sortedProducts[productIndex] = product;
                }
                productData.items = sortedProducts;
                commit('setProductsDetails', humps(productData));
                commit('setEmptyProductList', false);
                commit('inProgress', false);
            } else {
                commit('setEmptyProductList', true);
                commit('inProgress', false);
            }
        },
        fetchAttributeList: async({ commit }) => {
            const attributeList = await doFetchAttributeList();
            if (attributeList) {
                commit('setAttributeList', humps(attributeList));
            }
        },
        addToCart: async({ commit }, data) => {
            const guestQuote = await doFetchQuote();
            if (guestQuote) {
                commit('setGuestQuote', (guestQuote));
                const cartProduct = await doPostCartItem(data.sku, data.qty, guestQuote);

                if (cartProduct) {
                    commit('setCartProduct', (cartProduct));
                    const cartProductList = await doFetchCartProducts(guestQuote);
                    if (cartProductList) {
                        commit('setCartProductList', (cartProductList));
                        const productTotalAmt = await doFetchTotalAmt(guestQuote);
                        if (productTotalAmt) {
                            commit('setProductAmt', humps(productTotalAmt));
                        }
                    }
                }
            }
        },
        removeCartProduct: async({ commit }, { productId, guestQuoteId }) => {
            let trueOrFalse = await doDeleteCartProduct(productId, guestQuoteId);

            if (trueOrFalse) {
                let cartProductList = await doFetchCartProducts(guestQuoteId);

                if (cartProductList) {
                    commit('setCartProductList', (cartProductList));
                }
                const productTotalAmt = await doFetchTotalAmt(guestQuoteId);
                if (productTotalAmt) {
                    commit('setProductAmt', humps(productTotalAmt));
                }
            }
        },
        fetchSideBarFilter: async({ commit, dispatch }, pageDetails) => {
            commit('inProgress', true);
            commit('setProductsDetails', { "items": [] });
            const filterDetails = await doSideBarFilter(pageDetails.page, pageDetails.size, pageDetails.sortBy, pageDetails.categoryId, pageDetails.sideFilter, pageDetails.search);
            if (filterDetails) {
                commit('setSideBarFilterDetails', humps(filterDetails));
                let result = filterDetails.items.map(a => a.id);
                //console.log('result', result);
                if (result.length > 0) {
                    dispatch('fetchSortProducts', {
                        page: pageDetails.page,
                        size: pageDetails.size,
                        sortBy: pageDetails.sortBy,
                        categoryId: (result).toString(),
                        search: pageDetails.search,
                        sideFilter: pageDetails.sideFilter
                    });
                    commit('setEmptyProductList', false);
                    commit('inProgress', false);
                } else {
                    commit('setProductsDetails', { "items": [] });
                    commit('setEmptyProductList', true);
                    commit('inProgress', false);
                }
            }
        },
        fetchSideBarDefaultProducts: async({ commit }, pageDetails) => {
            commit('inProgress', true);
            commit('setProductsDetails', { "items": [] });
            const filterDetails = await doSideBarFilter(pageDetails.page, pageDetails.size, pageDetails.sortBy, pageDetails.categoryId, pageDetails.sideFilter, pageDetails.search);
            if (filterDetails) {
                commit('setSideBarFilterDetails', humps(filterDetails));
            }
            commit('inProgress', false);
        },
        fetchDefaultProducts: async({ commit, dispatch }, pageDetails) => {
            console.log('fetchDefaultProducts called');
            commit('inProgress', true);
            let sortedProducts = { "items": [] };
             if (pageDetails.categoryId) {
                const productData = await doFetchProductList();
                const productArray = await getProductArray(productData,pageDetails.categoryId);
               // console.log('productArray',productArray);
                const productsList = await getProductsList(productData,productArray);
              //  console.log('productsList',productsList);
                // let catProduct = productData[pageDetails.categoryId];
                if (productsList.length != 0) {
                    for (let i = 0; i < 9; i++) {
                        let product = productsList[i];
                        if (product)
                            sortedProducts.items.push(product)
                    }
                    dispatch('fetchSideBarDefaultProducts', pageDetails);
                    commit('setEmptyProductList', false);
                } else {
                    commit('setEmptyProductList', true);
                    commit('setSideBarFilterDetails', {
                        "items": [],
                        "aggregations": [],
                        "total_count": 0
                    });
                } 
                 commit('setProductsDetails', humps(sortedProducts));
                commit('inProgress', false);
            } else {
                commit('setEmptyProductList', true);
                commit('inProgress', false);
            }
        },
    },

}