import { handleActions } from 'redux-actions';
import actions from "./actions";

const inital = {
 me: {
    id: "",
    name: "",
    authState: ""
  },
  room: {
    id: "",
    name: "",
    videos: [],
    owners: [],
    searchResults: [],
    queryVideo: "",
    submitstate: ""
  },
  user: {
    id: "",
    name: "",
    playList: []
  }
};

export default {
  me: handleActions({
    [actions.requestAuth](state) {
      return { ...state, authState: "pending" };
    },
    [actions.failureAuth](state, action) {
      return {...state, authState: action.payload}
    },
    [actions.successAuth](state, action) {
      const { id, name } = action.payload;
      return { id, name, authState: "completed" };
    }
  }),
  room: {
    [actions.successFetchRoomInfo](state, action) {
      return {...state, ...action.payload};
    },
    [actions.requestSerchVideo](state) {
      return {...state, submitstate: "pending"}
    },
    [actions.failureSearchVideo](state) {
      return {...state, submitstate: "error"}
    },
    [actions.sucessSearchVideo](state, action) {
      const { page, videos } = action.payload;
      const searchResults = [
        ...(page === 0 ? [] : state.searchResults),
        ...videos
      ];

      return {
        ...state,
         searchResults,
         submitstate: "complated"
      }
    },
    [actions.onPostedVideo](state, action) {
      const videos = [action.payload, ...state.videos];
      return {...state, videos};
    },
    [actions.onPlayedVideo](state, action) {
      const { index } = action.payload;
      const videos = [
        ...state.videos
          .slice(0, index)
          .map(value => ({...value, state: "unplayed"})),
        {...state.videos[index], state: "playing"},
        ...state.videos
          .slice(index)
          .map(value => ({...value, state: "played"}))
      ];
      return {...state, videos};
    },
    [actions.onDeletedVideo](state, action) {
      return {
        ...state,
        videos: state.videos.filter((value, index) =>
          index !== action.payload.index)}
    }
  },
  user: {
    [actions.successFetchUserInfo](state, action){
      return {...state, ...action.payload};
    }
  }
}

@@ -861,13 +861,12 @@
 uest
-Serch
+Post
 Vide
@@ -891,32 +891,85 @@
 return %7B
-...state, submit
+%0A        ...state,%0A        submitting: %7Bindex: action.payload.index, 
 state: %22
@@ -978,16 +978,24 @@
 nding%22%7D%0A
+      %7D%0A
     %7D,%0A 
@@ -1013,22 +1013,167 @@
 .failure
-Search
+PostVideo%5D(state) %7B%0A      return %7B%0A        ...state,%0A        submitting: %7B...state.submitting, state: %22pending%22%7D%0A      %7D%0A    %7D,%0A    %5Bactions.sucessPost
 Video%5D(s
@@ -1168,32 +1168,40 @@
 PostVideo%5D(state
+, action
 ) %7B%0A      return
@@ -1206,38 +1206,95 @@
 rn %7B
-...state, submitstate: %22error%22
+%0A        ...state,%0A        submitting: %7B...state.submitting, state: %22completed%22%7D%0A      
 %7D%0A  
@@ -1563,43 +1563,8 @@
 ults
-,%0A         submitstate: %22complated%22
 %0A   

Fix post/search video reducers