/* @flow */

import _ from 'lodash'
import type Game from '../routes/Game/DefaultGame'

// The types of actions that you can dispatch to modify the state of the store
export const types = {
  ADD: 'ADD',
  REMOVE: 'REMOVE',
  SET_TEAMS: 'SET_TEAMS',
  SET_NUMBER_OF_GAMES: 'SET_NUMBER_OF_GAMES',
  SET_SCORE_INCREASING: 'SET_SCORE_INCREASING',
  SET_PLAY_MODE: 'SET_PLAY_MODE',
  SET_GAMES: 'SET_GAMES',
  ADD_RESULT: 'ADD_RESULT',
}

// Helper functions to dispatch actions, optionally with payloads
export const actionCreators = {
  addPlayer: (item: string) => {
    return {type: types.ADD, payload: item}
  },
  removePlayer: (index: number) => {
    return {type: types.REMOVE, payload: index}
  },
  setTeams: (teams: [Array<number>,Array<number>]) => {
    return {type: types.SET_TEAMS, payload: teams}
  },
  setNumberOfGames: (numberOfGames: number) => {
    return {type: types.SET_NUMBER_OF_GAMES, payload: numberOfGames}
  },
  setScoreIncreasing: (increasing: boolean) => {
    return {type: types.SET_SCORE_INCREASING, payload: increasing}
  },
  setPlayMode: (mode: string) => {
    return {type: types.SET_SCORE_INCREASING, payload: mode}
  },
  setGames: (games: {[string]: Game}) => {
    return {type: types.SET_GAMES, payload: games}
  },
  addResult: (gameID: string, winnerTeam: -1|0|1) => {
    return {type: types.ADD_RESULT, payload: {gameID: gameID, winnerTeam: winnerTeam}}
  },
}

const PlayMode = {
  CLASSIC: "CLASSIC",
  CLUB: "CLUB",
}

// Initial state of the store
const initialState = {
  players: ['Hannah', 'Markus', 'Tobi', 'Lo', 'Moritz'],
  teams: [[0,1,2],[3,4]],
  games: {},
  playedGames: [],
  teamWin: [],
  playerWin: [],
  matchSettings: {
    playMode: PlayMode.CLASSIC,
    numberOfGames: 7,
    scoreIncreasing: true,
  },
}

function isValidTeams(teams: [Array<number>,Array<number>], numTeams: number): boolean{
  let allTeams = [...teams[0], ...teams[1]]
  if(allTeams.length !== numTeams){
    return false
  }
  for(let i=0; i<numTeams; i++){
    if(_.indexOf(allTeams, i) === -1){
      return false
    }
  }
  return true
}

function isValidPlayMode(playMode: string): boolean{
  return playMode === PlayMode.CLASSIC || playMode === PlayMode.CLUB
}

// Function to handle actions and update the state of the store.
// Notes:
// - The reducer must return a new state object. It must never modify
//   the state object. State objects should be treated as immutable.
// - We set \`state\` to our \`initialState\` by default. Redux will
//   call reducer() with no state on startup, and we are expected to
//   return the initial state of the app in this case.
export const reducer = (state: typeof initialState = initialState, action: {type: string, payload: any}) => {
  const {players, teams} = state
  const {type, payload} = action

  switch (type) {
    case types.ADD: {
      let p = [payload, ...players]
      let t = Array.from({length: p.length}, (value, key) => key)
      t = [t.slice(0, Math.ceil(t.length/2)),t.slice(Math.ceil(t.length/2))]
      return {
        ...state,
        players: p,
        teams: t
      }
    }
    case types.REMOVE: {
      let p = players.filter((todo, i) => i !== payload)
      let t = Array.from({length: p.length}, (value, key) => key)
      t = [t.slice(0, Math.ceil(t.length/2)),t.slice(Math.ceil(t.length/2))]
      return {
        ...state,
        players: p,
        teams: t
      }
    }
    case types.SET_TEAMS: {
      if( isValidTeams(payload, players.length)){
        return {
          ...state,
          teams: payload
        }
      } else {
        return state
      }
    }
    case types.SET_NUMBER_OF_GAMES: {
      return {
        ...state,
        matchSettings: {
          ...state.matchSettings,
          numberOfGames: payload,
        }
      }
    }
    case types.SET_SCORE_INCREASING: {
      return {
        ...state,
        matchSettings: {
          ...state.matchSettings,
          scoreIncreasing: payload,
        }
      }
    }
    case types.SET_PLAY_MODE: {
      if(isValidPlayMode(payload)){
        return {
          ...state,
          matchSettings: {
            ...state.matchSettings,
            playMode: payload,
          }
        }
      } else {
        return state
      }
    }
    case types.SET_GAMES: {
      return {
        ...state,
        games:  payload,
      }
    }
    case types.ADD_RESULT: {
      let teamWin = [...state.teamWin]
      let playerWin = [...state.playerWin]
      if(payload.winnerTeam != -1){
        teamWin.push(payload.winnerTeam)
        playerWin.push([...state.teams[payload.winnerTeam]])
      }
      return {
        ...state,
        playedGames: [...state.playedGames, payload.gameID],
        teamWin: teamWin,
        playerWin: playerWin,
      }
    }
  }

  return state
}

@@ -1117,32 +1117,25 @@
 pes.SET_
-SCORE_INCREASING
+PLAY_MODE
 , payloa
@@ -1397,16 +1397,23 @@
   %7D,%0A%7D%0A%0A
+export 
 const Pl

Fix typos