import shuffle from 'lodash/shuffle'
import { TurnPlan } from './basic'

export interface BaseAction {
  type: string
}

export interface AddPlayerAction extends BaseAction {
  type: 'add player'
  name: string
}
export function addPlayerAction(name: string): AddPlayerAction {
  return {
    type: 'add player',
    name,
  }
}

export interface RemovePlayerAction extends BaseAction {
  type: 'remove player'
  name: string
}
export function removePlayerAction(name: string): RemovePlayerAction {
  return {
    type: 'remove player',
    name,
  }
}

export interface ChooseWonderAction extends BaseAction {
  type: 'choose wonder'
  player: string
  wonder: string
}
export function chooseWonderAction(player: string, wonder: string): ChooseWonderAction {
  return {
    type: 'choose wonder',
    player,
    wonder,
  }
}

export interface StartGameAction extends BaseAction {
  type: 'start game'
  shuffle: number[]
}
export function startGameAction(numPlayers: number): StartGameAction {
  return {
    type: 'start game',
    shuffle: shuffle([...Array(numPlayers).keys()]),
  }
}

export interface PlanTurnAction extends BaseAction {
  type: 'plan turn'
  player: string
  turnPlan?: TurnPlan
}
export function planTurnAction(player: string, turnPlan?: TurnPlan): PlanTurnAction {
  return {
    type: 'plan turn',
    player,
    turnPlan,
  }
}

export interface ResetAction extends BaseAction {
  type: 'reset'
}
export function resetAction(): ResetAction {
  return { type: 'reset' }
}

export interface LoadSampleAction extends BaseAction {
  type: 'load sample'
}
export function loadSampleAction(): LoadSampleAction {
  return { type: 'load sample' }
}

export type Action = AddPlayerAction | RemovePlayerAction | ChooseWonderAction | StartGameAction
  | PlanTurnAction
  | ResetAction | LoadSampleAction
