/*
    ------------------------------------------------------------------

    This file is part of the Open Ephys GUI
    Copyright (C) 2024 Open Ephys

    ------------------------------------------------------------------

    This program 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.

    This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.

*/

#include "RecordControl.h"

RecordControl::RecordControl()
    : GenericProcessor ("Record Control")
{
}

void RecordControl::registerParameters()
{
    addCategoricalParameter (Parameter::PROCESSOR_SCOPE,
                             "trigger_type",
                             "Trigger type",
                             "Determines whether recording state is set or toggled by an incoming event",
                             { "Edge set", "Edge toggle" },
                             0);

    addCategoricalParameter (Parameter::PROCESSOR_SCOPE,
                             "edge",
                             "Edge",
                             "Determines whether recording state is changed by rising or falling events",
                             { "Rising", "Falling" },
                             0);

    addTtlLineParameter (Parameter::STREAM_SCOPE,
                         "trigger_line",
                         "Trigger line",
                         "The TTL line that triggers a change in recording state",
                         16);
}

AudioProcessorEditor* RecordControl::createEditor()
{
    editor = std::make_unique<RecordControlEditor> (this);
    return editor.get();
}

void RecordControl::process (AudioBuffer<float>& buffer)
{
    checkForEvents();
}

void RecordControl::handleTTLEvent (TTLEventPtr event)
{
    DataStream* stream = getDataStream (event->getStreamId());

    if (event->getLine() == (int ((*stream)["trigger_line"])))
    {
        if (int (getParameter ("trigger_type")->getValue()) == 0) // edge set
        {
            if (event->getState() == bool (getParameter ("edge")->getValue()))
            {
                CoreServices::setRecordingStatus (false);
            }
            else
            {
                CoreServices::setRecordingStatus (true);
            }
        }
        else // edge toggle
        {
            if (event->getState() != bool (getParameter ("edge")->getValue()))
            {
                CoreServices::setRecordingStatus (! CoreServices::getRecordingStatus());
            }
        }
    }
}
