/*
This Source Code Form is subject to the
terms of the Mozilla Public License, v.
2.0. If a copy of the MPL was not
distributed with this file, You can
obtain one at
http://mozilla.org/MPL/2.0/.
*/

#include "stdafx.h"
#include "command.h"

#include "Globals.h"
#include "Logs.h"
#include "Timer.h"
#include "mctools.h"

namespace simulation {

command_queue Commands;
commanddescription_sequence Commands_descriptions = {

    { "aidriverenable", command_target::vehicle },
    { "aidriverdisable", command_target::vehicle },
    { "mastercontrollerincrease", command_target::vehicle },
    { "mastercontrollerincreasefast", command_target::vehicle },
    { "mastercontrollerdecrease", command_target::vehicle },
    { "mastercontrollerdecreasefast", command_target::vehicle },
    { "secondcontrollerincrease", command_target::vehicle },
    { "secondcontrollerincreasefast", command_target::vehicle },
    { "secondcontrollerdecrease", command_target::vehicle },
    { "secondcontrollerdecreasefast", command_target::vehicle },
    { "mucurrentindicatorothersourceactivate", command_target::vehicle },
    { "independentbrakeincrease", command_target::vehicle },
    { "independentbrakeincreasefast", command_target::vehicle },
    { "independentbrakedecrease", command_target::vehicle },
    { "independentbrakedecreasefast", command_target::vehicle },
    { "independentbrakebailoff", command_target::vehicle },
    { "trainbrakeincrease", command_target::vehicle, command_mode::continuous },
    { "trainbrakedecrease", command_target::vehicle, command_mode::continuous },
    { "trainbrakecharging", command_target::vehicle },
    { "trainbrakerelease", command_target::vehicle },
    { "trainbrakefirstservice", command_target::vehicle },
    { "trainbrakeservice", command_target::vehicle },
    { "trainbrakefullservice", command_target::vehicle },
    { "trainbrakehandleoff", command_target::vehicle },
    { "trainbrakeemergency", command_target::vehicle },
    { "trainbrakebasepressureincrease", command_target::vehicle },
    { "trainbrakebasepressuredecrease", command_target::vehicle },
    { "trainbrakebasepressurereset", command_target::vehicle },
    { "trainbrakeoperationtoggle", command_target::vehicle },
    { "manualbrakeincrease", command_target::vehicle },
    { "manualbrakedecrease", command_target::vehicle },
    { "alarmchaintoggle", command_target::vehicle },
    { "wheelspinbrakeactivate", command_target::vehicle },
    { "sandboxactivate", command_target::vehicle },
    { "reverserincrease", command_target::vehicle },
    { "reverserdecrease", command_target::vehicle },
    { "linebreakertoggle", command_target::vehicle },
    { "convertertoggle", command_target::vehicle },
    { "convertertogglelocal", command_target::vehicle },
    { "converteroverloadrelayreset", command_target::vehicle },
    { "compressortoggle", command_target::vehicle },
    { "compressortogglelocal", command_target::vehicle },
    { "motoroverloadrelaythresholdtoggle", command_target::vehicle },
    { "motoroverloadrelayreset", command_target::vehicle },
    { "notchingrelaytoggle", command_target::vehicle },
    { "epbrakecontroltoggle", command_target::vehicle },
    { "brakeactingspeedincrease", command_target::vehicle },
    { "brakeactingspeeddecrease", command_target::vehicle },
    { "brakeloadcompensationincrease", command_target::vehicle },
    { "brakeloadcompensationdecrease", command_target::vehicle },
    { "mubrakingindicatortoggle", command_target::vehicle },
    { "alerteracknowledge", command_target::vehicle },
    { "hornlowactivate", command_target::vehicle },
    { "hornhighactivate", command_target::vehicle },
    { "radiotoggle", command_target::vehicle },
    { "radiochannelincrease", command_target::vehicle },
    { "radiochanneldecrease", command_target::vehicle },
    { "radiostoptest", command_target::vehicle },
    // TBD, TODO: make cab change controls entity-centric
    { "cabchangeforward", command_target::vehicle },
    { "cabchangebackward", command_target::vehicle },

    { "viewturn", command_target::entity },
    { "movehorizontal", command_target::entity },
    { "movehorizontalfast", command_target::entity },
    { "movevertical", command_target::entity },
    { "moveverticalfast", command_target::entity },
    { "moveleft", command_target::entity },
    { "moveright", command_target::entity },
    { "moveforward", command_target::entity },
    { "moveback", command_target::entity },
    { "moveup", command_target::entity },
    { "movedown", command_target::entity },
    // TBD, TODO: make coupling controls entity-centric
    { "carcouplingincrease", command_target::vehicle },
    { "carcouplingdisconnect", command_target::vehicle },
    { "doortoggleleft", command_target::vehicle },
    { "doortoggleright", command_target::vehicle },
    { "departureannounce", command_target::vehicle },
    { "doorlocktoggle", command_target::vehicle },
    { "pantographcompressorvalvetoggle", command_target::vehicle },
    { "pantographcompressoractivate", command_target::vehicle },
    { "pantographtogglefront", command_target::vehicle },
    { "pantographtogglerear", command_target::vehicle },
    { "pantographlowerall", command_target::vehicle },
    { "heatingtoggle", command_target::vehicle },
    { "lightspresetactivatenext", command_target::vehicle },
    { "lightspresetactivateprevious", command_target::vehicle },
    { "headlighttoggleleft", command_target::vehicle },
    { "headlighttoggleright", command_target::vehicle },
    { "headlighttoggleupper", command_target::vehicle },
    { "redmarkertoggleleft", command_target::vehicle },
    { "redmarkertoggleright", command_target::vehicle },
    { "headlighttogglerearleft", command_target::vehicle },
    { "headlighttogglerearright", command_target::vehicle },
    { "headlighttogglerearupper", command_target::vehicle },
    { "redmarkertogglerearleft", command_target::vehicle },
    { "redmarkertogglerearright", command_target::vehicle },
    { "redmarkerstoggle", command_target::vehicle },
    { "endsignalstoggle", command_target::vehicle },
    { "headlightsdimtoggle", command_target::vehicle },
    { "motorconnectorsopen", command_target::vehicle },
    { "motordisconnect", command_target::vehicle },
    { "interiorlighttoggle", command_target::vehicle },
    { "interiorlightdimtoggle", command_target::vehicle },
    { "instrumentlighttoggle", command_target::vehicle },
    { "generictoggle0", command_target::vehicle },
    { "generictoggle1", command_target::vehicle },
    { "generictoggle2", command_target::vehicle },
    { "generictoggle3", command_target::vehicle },
    { "generictoggle4", command_target::vehicle },
    { "generictoggle5", command_target::vehicle },
    { "generictoggle6", command_target::vehicle },
    { "generictoggle7", command_target::vehicle },
    { "generictoggle8", command_target::vehicle },
    { "generictoggle9", command_target::vehicle },
    { "batterytoggle", command_target::vehicle }
};

}

void command_queue::update()
{
	double delta = Timer::GetDeltaTime();
	for (user_command c : m_active_continuous)
	{
	    auto const &desc = simulation::Commands_descriptions[ static_cast<std::size_t>( c ) ];
		command_data data { c, GLFW_REPEAT, 0, 0, command_data::desired_state_t::TOGGLE, delta };
	    auto lookup = m_commands.emplace((size_t)desc.target, commanddata_sequence() );
	    lookup.first->second.emplace( data );
	}
}

// posts specified command for specified recipient
void
command_queue::push( command_data const &Command, std::size_t const Recipient ) {
    auto const &desc = simulation::Commands_descriptions[ static_cast<std::size_t>( Command.command ) ];
	if (desc.mode == command_mode::continuous)
	{
		if (Command.action == GLFW_PRESS)
			m_active_continuous.emplace(Command.command);
		else if (Command.action == GLFW_RELEASE)
			m_active_continuous.erase(Command.command);
	}

    auto lookup = m_commands.emplace( Recipient, commanddata_sequence() );
    // recipient stack was either located or created, so we can add to it quite safely
    lookup.first->second.emplace( Command );
}

// retrieves oldest posted command for specified recipient, if any. returns: true on retrieval, false if there's nothing to retrieve
bool
command_queue::pop( command_data &Command, std::size_t const Recipient ) {

    auto lookup = m_commands.find( Recipient );
    if( lookup == m_commands.end() ) {
        // no command stack for this recipient, so no commands
        return false;
    }
    auto &commands = lookup->second;
    if( true == commands.empty() ) {

        return false;
    }
    // we have command stack with command(s) on it, retrieve and pop the first one
    Command = commands.front();
    commands.pop();

    return true;
}

void
command_relay::post( user_command const Command, std::uint64_t const Param1, std::uint64_t const Param2,
                     int const Action, std::uint16_t const Recipient,
                     command_data::desired_state_t state) const {

    auto const &command = simulation::Commands_descriptions[ static_cast<std::size_t>( Command ) ];
    if( ( command.target == command_target::vehicle )
     && ( true == FreeFlyModeFlag )
     && ( ( false == DebugModeFlag )
       && ( true == Global::RealisticControlMode ) ) ) {
        // in realistic control mode don't pass vehicle commands if the user isn't in one, unless we're in debug mode
        return;
    }

    simulation::Commands.push(
        command_data{
            Command,
            Action,
            Param1,
            Param2,
            state, Timer::GetDeltaTime()},
        static_cast<std::size_t>( command.target ) | Recipient );
/*
#ifdef _DEBUG
    if( Action != GLFW_RELEASE ) {
    // key was pressed or is still held
        if( false == command.name.empty() ) {
            if( false == (
                ( Command == user_command::moveleft )
             || ( Command == user_command::moveleftfast )
             || ( Command == user_command::moveright )
             || ( Command == user_command::moverightfast )
             || ( Command == user_command::moveforward )
             || ( Command == user_command::moveforwardfast )
             || ( Command == user_command::moveback )
             || ( Command == user_command::movebackfast )
             || ( Command == user_command::moveup )
             || ( Command == user_command::moveupfast )
             || ( Command == user_command::movedown )
             || ( Command == user_command::movedownfast )
             || ( Command == user_command::movevector )
             || ( Command == user_command::viewturn ) ) ) {
                WriteLog( "Command issued: " + command.name );
            }
        }
    }
    else {
    // key was released (but we don't log this)
        WriteLog( "Key released: " + command.name );
    }
#endif
*/
}

//---------------------------------------------------------------------------
