
#include "Bool.hpp"

void Bool::processAll(const ProcessArgs& args) {
	for (int i = 0, cn = std::max(1, inputs[A_INPUT].getChannels()); i < cn; ++i) {
		bool a = inputs[A_INPUT].getPolyVoltage(i) > 1.0f;
		bool b = inputs[B_INPUT].getPolyVoltage(i) > 1.0f;
		outputs[AND_OUTPUT].setChannels(cn);
		outputs[AND_OUTPUT].setVoltage(5.0f * (a && b), i);
		outputs[OR_OUTPUT].setChannels(cn);
		outputs[OR_OUTPUT].setVoltage(5.0f * (a || b), i);
		outputs[XOR_OUTPUT].setChannels(cn);
		outputs[XOR_OUTPUT].setVoltage(5.0f * (a ^ b), i);
	}

	int cn = std::max(1, inputs[NOT_INPUT].getChannels());
	outputs[NOT_OUTPUT].setChannels(cn);
	for (int i = 0; i < cn; ++i) {
		outputs[NOT_OUTPUT].setVoltage(5.0f * (inputs[NOT_INPUT].isConnected() && inputs[NOT_INPUT].getPolyVoltage(i) < 1.0f), i);
	}
}

struct BoolWidget : BGModuleWidget {
	static constexpr int hp = 3;

	BoolWidget(Bool* module) {
		setModule(module);
		box.size = Vec(RACK_GRID_WIDTH * hp, RACK_GRID_HEIGHT);
		setPanel(box.size, "Bool");
		createScrews();

		// generated by svg_widgets.rb
		auto aInputPosition = Vec(10.5, 23.0);
		auto bInputPosition = Vec(10.5, 53.0);
		auto notInputPosition = Vec(10.5, 221.0);

		auto andOutputPosition = Vec(10.5, 86.0);
		auto orOutputPosition = Vec(10.5, 126.0);
		auto xorOutputPosition = Vec(10.5, 166.0);
		auto notOutputPosition = Vec(10.5, 254.0);
		// end generated by svg_widgets.rb

		addInput(createInput<Port24>(aInputPosition, module, Bool::A_INPUT));
		addInput(createInput<Port24>(bInputPosition, module, Bool::B_INPUT));
		addInput(createInput<Port24>(notInputPosition, module, Bool::NOT_INPUT));

		addOutput(createOutput<Port24>(andOutputPosition, module, Bool::AND_OUTPUT));
		addOutput(createOutput<Port24>(orOutputPosition, module, Bool::OR_OUTPUT));
		addOutput(createOutput<Port24>(xorOutputPosition, module, Bool::XOR_OUTPUT));
		addOutput(createOutput<Port24>(notOutputPosition, module, Bool::NOT_OUTPUT));
	}
};

Model* modelBool = bogaudio::createModel<Bool, BoolWidget>("Bogaudio-Bool", "BOOL", "Boolean logic", "Logic", "Polyphonic");
