#include "Output.h"
namespace mdd {
    int Output::addConnection(std::shared_ptr<IInput> input)
    {
        _connections.push_back(input);
        return _connections.size();
    }
    int Output::removeConnection(std::shared_ptr<IInput> input)
    {
        for (auto it = _connections.begin(); it != _connections.end(); ++it) {
            if ((*it) == input)
            {
                _connections.erase(it);
            }
        }
        return _connections.size();
    }
    Output::Output( const std::string& type, int appendix, const std::vector<double>& initial) :
            _state(state::UNCHANGED)
    {
        _appendix = appendix;
        _prefix = "";
        _type = type;
        _value = initial;
    }

    const std::vector<double>& Output::getValue() { return _value; }

    std::vector<double>& Output::setValue() {
        _state = state::CHANGED;
        return _value;
    }
    state Output::setValue(const std::vector<double>& val) {
        if (val != _value)
        {
            _value = val;
            _state = state::CHANGED;
        }
        else {
            _state = state::UNCHANGED;
        }
        return _state;
    }
    bool Output::isOptimizable() {
        return _optimizable;
    }

    void Output::setOptimizability(bool state) {
        _optimizable = state;
    }
    std::string Output::getType() { return _type; }

    state Output::getState() { return _state; } 

    void Output::resetState() { _state = state::UNCHANGED; }

    std::string Output::setType(std::string type){
        _type = type;
        return getID();
    }

    std::string Output::getID(){
        return _prefix + "/" + _type + std::to_string(_appendix);
    }

    std::string Output::setPrefix(std::string prefix){
        _prefix = prefix;
        return getID();
    }

    std::string Output::setAppendix(int appendix){
        _appendix = appendix;
        return getID();
    }
    bool Output::connect(std::shared_ptr<IInput> input)
    {
        addConnection(input);
        input->addConnection(std::make_shared<Output>((*this)));
        return true;
    }
    std::vector<std::shared_ptr<IInput>> Output::getConnections()
    {
        return _connections;
    }
}