import { Map, List, Set } from 'immutable';

const initialAnswers = Map({
  activities: Map(),
  moods: Set(),
  freeWord: Set(),
});

const initialState = Map({
  users: List(),
  currentUser: Map({
    id: null,
    token: '',
    name: '',
    image: null,
    answers: initialAnswers,
  }),
});

const CREATE_USER = 'UserState/CREATE_USER';
const EDIT_USER = 'UserState/EDIT_USER';
const REMOVE_USER = 'UserState/REMOVE_USER';
const RESET_CURRENT_USER = 'UserState/RESET_CURRENT_USER';
const SET_CURRENT_USER = 'UserState/SET_CURRENT_USER';
const ADD_ACTIVITY = 'UserState/ADD_ACTIVITY';
const DELETE_ACTIVITY = 'UserState/DELETE_ACTIVITY';
const ADD_MOOD = 'UserState/ADD_MOOD';
const DELETE_MOOD = 'UserState/DELETE_MOOD';
const ADD_FREEWORD = 'UserState/ADD_FREEWORD';

export function createUser(newUser) {
  return {
    type: CREATE_USER,
    payload: Map({
      id: newUser.get('id'),
      name: newUser.get('name'),
      token: newUser.get('token'),
      image: newUser.get('image'),
      answers: initialAnswers,
    }),
  };
}

export function editUser(user) {
  return {
    type: EDIT_USER,
    payload: {
      id: user.get('id'),
      values: Map({
        id: user.get('id'),
        name: user.get('name'),
        token: user.get('token'),
        image: user.get('image'),
        answers: initialAnswers,
      }),
    },
  };
}

export function removeUser(id) {
  return {
    type: REMOVE_USER,
    payload: id,
  };
}

export function resetCurrentUser() {
  return {
    type: RESET_CURRENT_USER,
  };
}

export function setCurrentUser(user) {
  console.log(user.get('image'));
  return {
    type: SET_CURRENT_USER,
    payload: user,
  };
}

export function addActivity(activity) {
  return {
    type: ADD_ACTIVITY,
    payload: activity,
  };
}

export function deleteActivity(activity) {
  return {
    type: DELETE_ACTIVITY,
    payload: activity,
  };
}

export function addMood(mood) {
  return {
    type: ADD_MOOD,
    payload: mood,
  };
}

export function deleteMood(mood) {
  return {
    type: DELETE_MOOD,
    payload: mood,
  };
}

export function addFreeWord(freeWord) {
  return {
    type: ADD_FREEWORD,
    payload: freeWord,
  };
}

function usersReducer(state = List(), action) {
  switch (action.type) {
    case CREATE_USER:
      return state.update(list => list.push(action.payload));

    case EDIT_USER:
      return state.set(action.payload.id, action.payload.values);

    case REMOVE_USER:
      return state.filter(user => user.get('id') !== action.payload);

    default:
      return state;
  }
}

function currentUserReducer(state = Map(), action, wholeState) {
  switch (action.type) {
    case RESET_CURRENT_USER:
      return initialState.get('currentUser');

    case SET_CURRENT_USER:
      return state
        .set('name', action.payload.get('name'))
        .set('image', action.payload.get('image'))
        .set('token', action.payload.get('token'))
        .set('id', action.payload.get('id'));

    case ADD_ACTIVITY:
      return state.setIn(
        ['answers', 'activities', action.payload.main, action.payload.sub],
        action.payload.thumb,
      );

    case DELETE_ACTIVITY:
      return state.deleteIn([
        'answers',
        'activities',
        action.payload.main,
        action.payload.sub,
      ]);

    case ADD_MOOD:
      return state.updateIn(['answers', 'moods'], moods =>
        moods.add(action.payload),
      );

    case DELETE_MOOD:
      return state.updateIn(['answers', 'moods'], moods =>
        moods.delete(action.payload),
      );

    case ADD_FREEWORD:
      return state.updateIn(['answers', 'freeWord'], freeWord =>
        freeWord.add(action.payload),
      );

    default:
      return state;
  }
}

export default function UserStateReducer(state = initialState, action = {}) {
  return state
    .set('users', usersReducer(state.get('users'), action))
    .set(
      'currentUser',
      currentUserReducer(state.get('currentUser'), action, state),
    );
}

@@ -3150,32 +3150,230 @@
 ELETE_ACTIVITY:%0A
+      const subs = state.getIn(%5B'answers', 'activities', action.payload.main%5D);%0A%0A      if (subs.size === 1) %7B%0A        return state.deleteIn(%5B'answers', 'activities', action.payload.main%5D);%0A      %7D%0A%0A
       return sta

Delete main activity when its last sub activity is deleted