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

import { GetShopRelatedRequestModel } from '../../models/requests'
import { GetShopRelatedResponseModel } from '../../models/responses'
import { Get } from '../../restClient'

export interface ShopRelatedState extends GetShopRelatedResponseModel {
  isLoading: boolean
}

interface RequestAction extends GetShopRelatedRequestModel {
  type: 'REQUEST_SHOP_RELATED'
}

interface ReceiveAction extends GetShopRelatedResponseModel {
  type: 'RECEIVE_SHOP_RELATED'
}

type KnownAction = RequestAction | ReceiveAction

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

    Get<Promise<GetShopRelatedResponseModel>>('https://localhost:7151/api/ShopRelated', props?.pathParams, props?.searchParams)
      .then(response => response)
      .then(data => {
        if(data)
          dispatch({ type: 'RECEIVE_SHOP_RELATED', ...data })
      })

    dispatch({ type: 'REQUEST_SHOP_RELATED' })
  }
}

const unloadedState: ShopRelatedState = {
  items: [
    {
      id: '',
      slug: "shop-catalog-item",
      sku: "SKU-0",
      image: { src: "https://dummyimage.com/450x300/dee2e6/6c757d.jpg", alt: "..." },
      badges: [ "sale", "best offer" ],
      title: "Shop item title",
      brandName: "Brand & Name",
      
      shortText: "Lorem ipsum, dolor sit amet consectetur adipisicing elit. Eaque fugit ratione dicta mollitia. Officiis ad...",
      text: "",
      author: {
        id: '',
        image: { src: "https://dummyimage.com/40x40/ced4da/6c757d", alt: "..." },
        nickName: "Admin"
      },
      created: (new Date).toString(),

      tags: [ "react", "redux", "webapi" ],

      rating: 4.5,
      price: 20,
      newPrice: 10
    },
    {
      id: '',
      slug: "shop-catalog-item",
      sku: "SKU-0",
      image: { src: "https://dummyimage.com/450x300/dee2e6/6c757d.jpg", alt: "..." },
      badges: [ "sale", "best offer" ],
      title: "Shop item title",
      brandName: "Brand & Name",
      
      shortText: "Lorem ipsum, dolor sit amet consectetur adipisicing elit. Eaque fugit ratione dicta mollitia. Officiis ad...",
      text: "",
      author: {
        id: '',
        image: { src: "https://dummyimage.com/40x40/ced4da/6c757d", alt: "..." },
        nickName: "Admin"
      },
      created: (new Date).toString(),

      tags: [ "react", "redux", "webapi" ],

      rating: 4.5,
      price: 20,
      newPrice: 10
    },
    {
      id: '',
      slug: "shop-catalog-item",
      sku: "SKU-0",
      image: { src: "https://dummyimage.com/450x300/dee2e6/6c757d.jpg", alt: "..." },
      badges: [ "sale", "best offer" ],
      title: "Shop item title",
      brandName: "Brand & Name",
      
      shortText: "Lorem ipsum, dolor sit amet consectetur adipisicing elit. Eaque fugit ratione dicta mollitia. Officiis ad...",
      text: "",
      author: {
        id: '',
        image: { src: "https://dummyimage.com/40x40/ced4da/6c757d", alt: "..." },
        nickName: "Admin"
      },
      created: (new Date).toString(),

      tags: [ "react", "redux", "webapi" ],

      rating: 4.5,
      price: 20,
      newPrice: 10
    },
    {
      id: '',
      slug: "shop-catalog-item",
      sku: "SKU-0",
      image: { src: "https://dummyimage.com/450x300/dee2e6/6c757d.jpg", alt: "..." },
      badges: [ "sale", "best offer" ],
      title: "Shop item title",
      brandName: "Brand & Name",
      
      shortText: "Lorem ipsum, dolor sit amet consectetur adipisicing elit. Eaque fugit ratione dicta mollitia. Officiis ad...",
      text: "",
      author: {
        id: '',
        image: { src: "https://dummyimage.com/40x40/ced4da/6c757d", alt: "..." },
        nickName: "Admin"
      },
      created: (new Date).toString(),

      tags: [ "react", "redux", "webapi" ],

      rating: 4.5,
      price: 20,
      newPrice: 10
    }
  ],
  isLoading: false
}

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

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

  return state
}
