// Copyright 2020 Thomas Hintz
//
// This file is part of the Alpha Centauri Farming project.
//
// The Alpha Centauri Farming project is free software: you can
// redistribute it and/or modify it under the terms of the GNU General
// Public License as published by the Free Software Foundation, either
// version 3 of the License, or (at your option) any later version.
//
// The Alpha Centauri Farming project is distributed in the hope that
// it will be useful, but WITHOUT ANY WARRANTY; without even the
// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE.  See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with the Alpha Centauri Farming project.  If not, see
// <https://www.gnu.org/licenses/>.

import { UPDATE_GAME, UPDATE_PLAYER, GAME_STATE, SET_SELECTED_CARD, SET_CARDS,
         SPACE_PUSH_PLAYER, SPACE_CLEAR_PLAYERS, SET_OLD_MESSAGES, MESSAGE_PANEL_SPACE,
         MP_MOUSE, SET_MP_DIMS, MARK_ACTION_CHANGE_HANDLED, SET_NEXT_ACTION,
         MOVE_PLAYER, NEXT_UI_ACTION, NEXT_UI_ACTION_SILENT, ALERT, ALERT_HANDLED,
         AUTO_SKIP, MESSAGE, SET_HARVEST_TABLE, SET_CARD_ERROR,
         SET_MOVING_SKIP, SERVER_ERROR, REMOVE_PLAYER } from './actionTypes.js'

export { updateGame, updatePlayer, gameState, setSelectedCard, setCards,
         spacePushPlayer, spaceClearPlayers, setOldMessages, setMessagePanelSpace,
         mpMouse, setMPDims, movePlayer, setNextAction, nextUIAction,
         markActionChangeHandled, nextUIActionSilent, alert, alertHandled,
         autoSkip, message, setHarvestTable, setCardError, setMovingSkip,
         serverError, removePlayer }

function updateGame(update) {
    return { type: UPDATE_GAME,
             update };
}

function updatePlayer(update) {
    return { type: UPDATE_PLAYER,
             update };
}

function gameState(state) {
    return { type: GAME_STATE,
             state };
}

function setSelectedCard(card) {
    return { type: SET_SELECTED_CARD,
             // TODO share with initialState ui.card
             card: card ? card : { type: 'no-card', contents: '', total: 0 } }
}

function setCards(cards) {
    return { type: SET_CARDS,
             cards };
}

function setCardError(error) {
    return { type: SET_CARD_ERROR, error };
}

function spacePushPlayer(id, player) {
    return { type: SPACE_PUSH_PLAYER,
             id,
             player };
}

function spaceClearPlayers(id) {
    return { type: SPACE_CLEAR_PLAYERS,
             id };
}

function setOldMessages(messages) {
    return { type: SET_OLD_MESSAGES,
             messages };
}

function setMessagePanelSpace(space) {
    return { type: MESSAGE_PANEL_SPACE,
             space };
}

function mpMouse(mouseX, mouseY) {
    return { type: MP_MOUSE,
             mouseX, mouseY };
}

function setMPDims(minWidth, minHeight, maxWidth, maxHeight) {
    return { type: SET_MP_DIMS,
             minWidth, minHeight, maxWidth, maxHeight };
}

function movePlayer(newSpace, oldSpace, player) {
    return { type: MOVE_PLAYER,
             newSpace, oldSpace, player };
}

function removePlayer(color) {
    return { type: REMOVE_PLAYER,
             color };
}

function nextUIAction() {
    return { type: NEXT_UI_ACTION };
}

function nextUIActionSilent() {
    return { type: NEXT_UI_ACTION_SILENT };
}

function setNextAction(action, value) {
    return { type: SET_NEXT_ACTION,
             action, value };
}

function markActionChangeHandled() {
    return { type: MARK_ACTION_CHANGE_HANDLED };
}

function alert(value, contents, id) {
    return { type: ALERT, value, contents, id };
}

function alertHandled(id) {
    return { type: ALERT_HANDLED, id };
}

function autoSkip(component) {
    return { type: AUTO_SKIP, component };
}

function message(message) {
    return { type: MESSAGE, message };
}

function setHarvestTable(table) {
    return { type: SET_HARVEST_TABLE, table };
}

function setMovingSkip(skip) {
    return { type: SET_MOVING_SKIP, skip };
}

function serverError(exn) {
    return { type: SERVER_ERROR, exn };
}
