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

import { GetBlogItemRequestModel } from '../../models/requests'
import { GetBlogItemResponseModel } from '../../models/responses'
import { Get } from '../../restClient'

export interface BlogItemState extends GetBlogItemResponseModel {
  isLoading: boolean
}

interface RequestAction extends GetBlogItemRequestModel {
  type: 'REQUEST_BLOG_ITEM'
}

interface ReceiveAction extends GetBlogItemResponseModel {
  type: 'RECEIVE_BLOG_ITEM'
}

type KnownAction = RequestAction | ReceiveAction

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

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

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

const unloadedState: BlogItemState = {
  id: "",
  slug: "demo-post",
  image: {
    src: "https://dummyimage.com/900x400/ced4da/6c757d.jpg",
    alt: "..."
  },
  badges: [
    "Web Design",
    "Freebies"
  ],

  title: "Welcome to Blog Post!",
  text: `<p className="fs-5 mb-4">Science is an enterprise that should be cherished as an activity of the free human mind. Because it transforms who we are, how we live, and it gives us an understanding of our place in the universe.</p>
  <p className="fs-5 mb-4">The universe is large and old, and the ingredients for life as we know it are everywhere, so there's no reason to think that Earth would be unique in that regard. Whether of not the life became intelligent is a different question, and we'll see if we find that.</p>
  <p className="fs-5 mb-4">If you get asteroids about a kilometer in size, those are large enough and carry enough energy into our system to disrupt transportation, communication, the food chains, and that can be a really bad day on Earth.</p>
  <h2 className="fw-bolder mb-4 mt-5">I have odd cosmic thoughts every day</h2>
  <p className="fs-5 mb-4">For me, the most fascinating interface is Twitter. I have odd cosmic thoughts every day and I realized I could hold them to myself or share them with people who might be interested.</p>
  <p className="fs-5 mb-4">Venus has a runaway greenhouse effect. I kind of want to know what happened there because we're twirling knobs here on Earth without knowing the consequences of it. Mars once had running water. It's bone dry today. Something bad happened there as well.</p>`,
  author: {
    id: "",
    nickName: "Admin",
    image: {
      src: "https://dummyimage.com/40x40/ced4da/6c757d",
      alt: "..."
    }
  },
  created:  new Date().toString(),
  tags: [ "react", "redux", "webapi" ],
  comments: [
    {
      author: {
        id: "",
        nickName: "Commenter Name 1",
        image: {
          src: "https://dummyimage.com/50x50/ced4da/6c757d.jpg",
          alt: "..."
        }
      },
      comment: "If you're going to lead a space frontier, it has to be government; it'll never be private enterprise. Because the space frontier is dangerous, and it's expensive, and it has unquantified risks.",
  
      responses: [
        {
          author: {
            id: "",
            nickName: "Commenter Name 4",
            image: {
              src: "https://dummyimage.com/50x50/ced4da/6c757d.jpg",
              alt: "..."
            }
          },
          comment: "And under those conditions, you cannot establish a capital-market evaluation of that enterprise. You can't get investors."
        },
        {
          author: {
            id: "",
            nickName: "Commenter Name 3",
            image: {
              src: "https://dummyimage.com/50x50/ced4da/6c757d.jpg",
              alt: "..."
            }
          },
          comment: "When you put money directly to a problem, it makes a good headline."
        }
      ]
    },
    {
      author: {
        id: "",
        nickName: "Commenter Name 2",
        image: {
          src: "https://dummyimage.com/50x50/ced4da/6c757d.jpg",
          alt: "..."
        }
      },
      comment: "When I look at the universe and all the ways the universe wants to kill us, I find it hard to reconcile that with statements of beneficence."
    }
  
  ],

  isLoading: false
}

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

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

  return state
}
