#include "ModuleSwitch.h"
#include <iostream>

namespace mdd{
    ModuleSwitch::ModuleSwitch(){
        std::vector<double> default_val = { 42 };
        addInput("Switch", default_val);
        addInput("Value", default_val);
        addInput("Default", default_val);
        addOutput("Value", default_val);
        setType("Switch");
    }
    state ModuleSwitch::update(){
        std::vector<double> ret;
        size_t length = getInput(0)->getValue().size();
        for (size_t i = 0; i < length; i++)
        {
            int index = (int)getInput(0)->getValue()[i];
           
            if (index == 0) {
                index = 1;
            }

            if (index > getInputs().size() - 1) {
                index = getInputs().size() - 1;
            }
            for (size_t j = 0; j < getInput(index)->getValue().size(); j++)
            {
                ret.push_back(getInput(index)->getValue()[j]);
            }
            
        }
        return getOutput(0)->setValue(ret);

    }
}