import { Action, Reducer } from 'redux'
import { AppThunkAction } from '..'

import { GetShopCartItemsRequestModel } from '../../models/requests'
import { GetShopCartItemResponseModel } from '../../models/responses'

import { Get } from '../../restClient'

export interface ShopCartState {
  items: GetShopCartItemResponseModel [],
  isLoading: boolean
}

export interface RequestAction {
  type: 'REQUEST_CART_ITEMS'
}

export interface ReceiveAction {
  items: GetShopCartItemResponseModel [],
  type: 'RECEIVE_CART_ITEMS'
}

// export interface IncrementItemQuantityAction extends GetShopCartResponseModel {
//   type: 'INCREMENT_ITEM_QUANTITY'
// }

// export interface DecrementItmeQuantityAction extends GetShopCartResponseModel {
//   type: 'DECREMENT_ITEM_QUANTITY'
// }

export type KnownAction = RequestAction | ReceiveAction //| IncrementItemQuantityAction | DecrementItmeQuantityAction

export const actionCreators = {
  requestCart: (props?: GetShopCartItemsRequestModel): AppThunkAction<KnownAction> => (dispatch, getState) => {

 
    Get<Promise<GetShopCartItemResponseModel []>>('https://localhost:7151/api/ShopCartItems/404c8232-9048-4519-bfba-6e78dc7005ca', props?.pathParams, props?.searchParams)
      .then(response => response)
      .then(data => {
        if(data)
          dispatch({ type: 'RECEIVE_CART_ITEMS', items: [...data] })
      })

    dispatch({ type: 'REQUEST_CART_ITEMS' })
  },

  addToCart: (): AppThunkAction<KnownAction> => (dispatch, getState) => {

    // Get<Promise<GetBlogItemResponseModel>>('https://localhost:7151/api/BlogItem', props)
    //   .then(response => response)
    //   .then(data => {
    //     if(data)
    //       dispatch({ type: 'RECEIVE_BLOG_ITEM', ...data })
    //   })

    // dispatch({ type: 'REQUEST_BLOG_ITEM', slug: props.slug })
  },

  // increment: (startDateIndex: number): AppThunkAction<KnownAction> => (dispatch, getState) => {

  //   const items: ShopItemModel [] = []
  //   dispatch({ type: 'INCREMENT_ITEM_QUANTITY', items })


  // },
  // decrement: (): AppThunkAction<KnownAction> => (dispatch, getState) => {

  //   const items: ShopItemModel [] = []
  //   dispatch({ type: 'DECREMENT_ITEM_QUANTITY', items })
  // },

  // remFromCart: (): AppThunkAction<KnownAction> => (dispatch, getState) => {

  // }
}

const unloadedState: ShopCartState = {
  items: [
    {
      slug: "shop-catalog-item",
      sku: "SKU-0",
      image: { src: "https://dummyimage.com/450x300/dee2e6/6c757d.jpg", alt: "..." },
      title: "Shop item title",
      brandName: "Brand & Name",
      shortText: "Lorem ipsum, dolor sit amet consectetur adipisicing elit. Eaque fugit ratione dicta mollitia. Officiis ad...",
      created: (new Date).toString(),
      price: 20,
      newPrice: 10,
      quantity: 1
    },
    {
      slug: "shop-catalog-item",
      sku: "SKU-0",
      image: { src: "https://dummyimage.com/450x300/dee2e6/6c757d.jpg", alt: "..." },
      title: "Shop item title",
      brandName: "Brand & Name",
      shortText: "Lorem ipsum, dolor sit amet consectetur adipisicing elit. Eaque fugit ratione dicta mollitia. Officiis ad...",
      created: (new Date).toString(),
      price: 20,
      newPrice: 10,
      quantity: 1
    },
    {
      slug: "shop-catalog-item",
      sku: "SKU-0",
      image: { src: "https://dummyimage.com/450x300/dee2e6/6c757d.jpg", alt: "..." },
      title: "Shop item title",
      brandName: "Brand & Name",
      shortText: "Lorem ipsum, dolor sit amet consectetur adipisicing elit. Eaque fugit ratione dicta mollitia. Officiis ad...",
      created: (new Date).toString(),
      price: 20,
      newPrice: 10,
      quantity: 1
    }
  ],

  isLoading: false
}

export const reducer: Reducer<ShopCartState> = (state: ShopCartState | undefined, incomingAction: Action): ShopCartState => {
  if (state === undefined) {
    return unloadedState
  }
  
  const action = incomingAction as KnownAction
  switch (action.type) {
    case 'REQUEST_CART_ITEMS':
      return {
        ...state,
        isLoading: true
      }

    case 'RECEIVE_CART_ITEMS':
      return {
        ...action,
        isLoading: false
      }

    // case 'INCREMENT_ITEM_QUANTITY':
    //   return {
    //     ...action,
    //     isLoading: false
    //   }

    // case 'DECREMENT_ITEM_QUANTITY':
    //   return {
    //     ...action,
    //     isLoading: false
    //   }
  }

  return state
}