/**
 * React Native Full App (https://store.enappd.com/product/react-native-full-app)
 *
 * Copyright © 2019-present Enappd. All rights reserved.
 *
 * This source code is licensed as per the terms found in the
 * LICENSE.md file in the root directory of this source tree.
 */
import env from '../../Database/grocery/data'
import {
    GET_SHORT_OFFERS,
    GET_ACCORDION_DATA,
    SET_CATEGORY_PRODUCTS,
    SET_CATEGORY_TITLE,
    GET_SLIDER_DATA,
    HomeActionTypes
} from "../types/HomePage";
import { ThunkAction } from "redux-thunk";
import { AppReduxState } from "../reducers";
import { Action } from "redux";

export function getShortOffers(): ThunkAction<void,AppReduxState,null,Action<string>> {
    return dispatch => new Promise((resolve,reject) => {
      fetch('http://5cbf130e06a6810014c6632a.mockapi.io/api/v1/shortOffers')
        .then(response => response.json())
        .then(productData => {
          dispatch(getShortOffersDispatch(productData))
          resolve();
        })
        .catch(e => {
          console.error("get error in fetch", e)
          resolve();
        })
    })
  }
  
  function getShortOffersDispatch(data: any):HomeActionTypes {
    return {
      type: GET_SHORT_OFFERS,
      payload: env.shortOffers
    };
  }
  
  export function getAccordionData(): ThunkAction<void,AppReduxState,null,Action<string>> {
    return (dispatch: any) => {
      fetch('http://5cbf130e06a6810014c6632a.mockapi.io/api/v1/accordionData')
        .then(response => response.json())
        .then(productData => {
          dispatch(getAccordionDataDispatch(productData))
        })
        .catch(e => console.error("get error in fetch", e))
    }
  }
  
  function getAccordionDataDispatch(data: any): HomeActionTypes {
    return {
      type: GET_ACCORDION_DATA,
      payload: env.items
    };
  }

  export function getSliderData(): ThunkAction<void,AppReduxState,null,Action<string>> {
    return dispatch => {
      fetch('http://5cbf130e06a6810014c6632a.mockapi.io/api/v1/sliderImages')
        .then(response => response.json())
        .then(productData => {
          dispatch(getSliderDataDispatch(productData));
        })
        .catch(e => console.error("get error in fetch", e))
    }
  }
  
  function getSliderDataDispatch(data: any): HomeActionTypes {
    return {
      type: GET_SLIDER_DATA,
      payload: env.images
    };
  }

  export function setCategoryProducts(product: any): ThunkAction<void,AppReduxState,null,Action<string>> {
    return (dispatch: any) => {
      dispatch(setCategoryProductsDispatch(product))
    }
  }
  
  function setCategoryProductsDispatch(data: any): HomeActionTypes {
    return {
      type: SET_CATEGORY_PRODUCTS,
      payload: data
    };
  }
  
  export function setCategoryTitle(param: string): ThunkAction<void,AppReduxState,null,Action<string>> {
    return (dispatch: any) => {
      dispatch(setCategoryTitleDispatch(param))
    }
  }
  
  function setCategoryTitleDispatch(payload: string): HomeActionTypes {
    return {
      type: SET_CATEGORY_TITLE,
      payload
    };
  }