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

import { GetBlogFeaturedRequestModel } from '../../models/requests'
import { GetBlogFeaturedResponseModel } from '../../models/responses'
import { Get } from '../../restClient'

export interface BlogFeaturedState extends GetBlogFeaturedResponseModel {
  isLoading: boolean
}

interface RequestAction extends GetBlogFeaturedRequestModel {
  type: 'REQUEST_BLOG_FEATURED'
}

interface ReceiveAction extends GetBlogFeaturedResponseModel {
  type: 'RECEIVE_BLOG_FEATURED'
}

type KnownAction = RequestAction | ReceiveAction

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

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

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

const unloadedState: BlogFeaturedState = {
  items: [
    {
      id: "",
      slug: "demo-post",
      badges: [ "demo" ],
      image: {
        src: "https://dummyimage.com/850x350/dee2e6/6c757d.jpg",
        alt: "..."
      },
      title: "Lorem ipsum",
      shortText: "",
      author: {
        id: "",
        nickName: "Admin",
        image: {
          src: "https://dummyimage.com/40x40/ced4da/6c757d",
          alt: "..."
        }
      },
      created: new Date().toString(),
      tags: [],

      readTime: 10,
      likes: 0
    },
    {
      id: "",
      slug: "demo-post",
      badges: [ "demo" ],
      image: {
        src: "https://dummyimage.com/850x350/dee2e6/6c757d.jpg",
        alt: "..."
      },
      title: "Lorem ipsum",
      shortText: "",
      author: {
        id: "",
        nickName: "Admin",
        image: {
          src: "https://dummyimage.com/40x40/ced4da/6c757d",
          alt: "..."
        }
      },
      created: new Date().toString(),
      tags: [],

      readTime: 10,
      likes: 0
    },
    {
      id: "",
      slug: "demo-post",
      badges: [ "demo" ],
      image: {
        src: "https://dummyimage.com/850x350/dee2e6/6c757d.jpg",
        alt: "..."
      },
      title: "Lorem ipsum",
      shortText: "",
      author: {
        id: "",
        nickName: "Admin",
        image: {
          src: "https://dummyimage.com/40x40/ced4da/6c757d",
          alt: "..."
        }
      },
      created: new Date().toString(),
      tags: [],
      
      readTime: 10,
      likes: 0
    }
  ],
  isLoading: false
}

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

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

  return state
}
