'use strict';
import { socketService } from '../../../main';

/**
 * Description of game state model for a player
 *
 * @returns {object} Player state in vuex format
 */
export default function AnyPlayer() {
  const initPlayerState = {
    // Username of this player
    name: '',
    // Color of this player (blue or red)
    color: '',
    // Faction chosen (will stay empty if not faction mode)
    faction: '',
    // Will contain the IDs of the heroes selectable for draft mode
    draftHeroesIds: [],
    // the 12 heroes the player is playing with
    // Player will fill it himself if mode is draft or tournament
    // contains object : {id:heroId , position:'pile' , possibleActions :[]}
    twelveHeroes: [],
    foodInCamp: 0,
    // Can see already we might have issues with left/right depending from where we look :)
    'foodInBattle/left': 0,
    'foodInBattle/center': 0,
    'foodInBattle/right': 0,
    // During military phase it will contain actions made by player to be replayed by other
    actionsPerformed: []
  };

  const state = Object.assign({}, initPlayerState);

  const getters = {
    color: state => {
      return state.color;
    },
    draftIds: state => {
      return state.draftHeroesIds;
    },
    twelveHeroes: state => {
      return state.twelveHeroes;
    }
  };

  const mutations = {
    SET_FULL_PLAYER_STATE: (state, payload) => {
      Object.assign(state, payload);
    },
    INIT_PLAYER_STATE: state => {
      Object.assign(state, initPlayerState);
    }
  };

  const actions = {
    submitFaction: ({ commit, state }, payload) => {
      commit(
        'game/SET_WAITING_FOR',
        { color: state.color, value: false },
        { root: true }
      );
      socketService
        .endTurn({ color: state.color, faction: payload })
        .catch(err => {
          console.log('Error sending data : ', err);
          //Unvalid submit
          commit(
            'game/SET_WAITING_FOR',
            { color: state.color, value: true },
            { root: true }
          );
        });
    }
  };
  return {
    namespace: true,
    state,
    getters,
    mutations,
    actions
  };
}
