
#include <GL/glew.h>
#include <GLFW/glfw3.h>

#include "primary_loop.hpp"
#include "../locations.hpp"
#include "../../system.hpp"
#include "../../coolant/valve.hpp"
#include "../input/focus.hpp"
#include "../../util/streams.hpp"

#include <glm/ext/matrix_transform.hpp>
#include <iostream>

using namespace Sim::Graphics;
using namespace Sim::Graphics::Monitor;
using namespace Sim::Util::Streams;

struct ValveJoystick : public Focus::FocusType
{
	Sim::Coolant::Valve* active;
	
	ValveJoystick(Sim::Coolant::Valve* v) : active(v)
	{
		
	}

	virtual ~ValveJoystick()
	{
		active->clear_open_speed();
	}
	
	virtual void on_cursor_pos(double x, double y)
	{
		active->add_open_speed(-y * 1e-6);
	}

	virtual void on_mouse_button(int button, int action, int mods)
	{
		if(button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_RELEASE)
		{
			Focus::clear_focus();
		}
	}

	virtual bool cursor_is_visible()
	{
		return false;
	}
};


PrimaryLoop::PrimaryLoop()
{

}

void PrimaryLoop::init()
{
	mesh1.model_matrix = Locations::monitors[3];
	mesh2.model_matrix = glm::translate(mesh1.model_matrix, glm::vec3(0.5, 0, 0));

	mesh1.colour_matrix = mesh2.colour_matrix = {
		1, 1, 1, 1,
		0, 0, 0, 0,
		0, 0, 0, 0,
		0, 0, 0, 0
	};
	
	std::stringstream ss;
	Sim::Graphics::Mesh rmesh;

	ss << "Turbine Bypass Valve\n\n";
	ss << "Opened\nFlow\nSetpoint\n\n";
	ss << "Turbine Inlet Valve\n\n";
	ss << "Opened\nFlow\nSetpoint\n\n";
	ss << "Primary Pump\n\n";
	ss << "Power\nSpeed\nFlow\n\n";
	ss << "Condenser\n\n";
	ss << "Heat\n";
	ss << "Steam\n";
	ss << "Pressure\n";
	ss << "Level\n";

	rmesh.load_text(ss.str().c_str(), 0.04);
	mesh1.bind();
	mesh1.set(rmesh, GL_STATIC_DRAW);

	rmesh.load_model("../assets/model", "pump_switch_1.fbx");
	gm_switch_pump.bind();
	gm_switch_pump.set(rmesh, GL_STATIC_DRAW);
	
	rmesh.load_model("../assets/model", "turbine_valve_bypass_switch.fbx");
	gm_switch_bypass.bind();
	gm_switch_bypass.set(rmesh, GL_STATIC_DRAW);

	rmesh.load_model("../assets/model", "turbine_valve_inlet_switch.fbx");
	gm_switch_inlet.bind();
	gm_switch_inlet.set(rmesh, GL_STATIC_DRAW);

	m_joystick_turbine_bypass.load_model("../assets/model", "turbine_valve_bypass_joystick.stl");
	m_joystick_turbine_inlet.load_model("../assets/model", "turbine_valve_inlet_joystick.stl");
	m_switch_pump.load_model("../assets/model", "pump_switch_click_1.stl");
	m_switch_bypass.load_model("../assets/model", "turbine_valve_bypass_switch_click.stl");
	m_switch_inlet.load_model("../assets/model", "turbine_valve_inlet_switch_click.stl");
}

void PrimaryLoop::update(double dt)
{
	System& sys = *System::active;
	clock_now += dt;

	if(clock_at + 1.0/30.0 < clock_now)
	{
		std::stringstream ss;
		Sim::Graphics::Mesh rmesh;
		clock_at += 1.0/30.0;

		ss << "\n\n";
		ss << show( sys.loop.turbine_bypass_valve.get_state() * 100 ) << " %\n";
		show_units( ss, sys.loop.turbine_bypass_valve.get_flow() ) << "g/s\n";

		if(sys.loop.turbine_bypass_valve.get_auto())
		{
			ss << show( sys.loop.turbine_bypass_valve.get_setpoint() ) << " C\n";
		}

		else
		{
			ss << "-\n";
		}

		ss << "\n\n\n";
		ss << show( sys.loop.turbine_inlet_valve.get_state() * 100 ) << " %\n";
		show_units( ss, sys.loop.turbine_inlet_valve.get_flow() ) << "g/s\n";

		if(sys.loop.turbine_inlet_valve.get_auto())
		{
			ss << show( sys.loop.turbine_inlet_valve.get_setpoint() ) << " C\n";
		}

		else
		{
			ss << "-\n";
		}

		ss << "\n\n\n";
		ss << show( sys.loop.primary_pump.get_power() * 100 ) << " %\n";
		ss << show( sys.loop.primary_pump.get_rpm() ) << " r/min\n";
		show_units( ss, sys.loop.primary_pump.get_flow_mass() ) << "g/s\n";
		ss << "\n\n\n";
		ss << show( sys.loop.condenser.get_heat() ) << " C\n";
		show_units( ss, sys.loop.condenser.get_steam() ) << "g\n";
		show_units( ss, sys.loop.condenser.get_pressure() ) << "Pa\n";
		ss << show( sys.loop.condenser.get_level() / 1000 ) << " / " << show( sys.loop.condenser.get_volume() / 1000 ) << " kL\n";

		rmesh.load_text(ss.str().c_str(), 0.04);
		mesh2.bind();
		mesh2.set(rmesh, GL_DYNAMIC_DRAW);
	}

	if(m_joystick_turbine_bypass.check_focus())
		Focus::set(std::make_unique<ValveJoystick>(&sys.loop.turbine_bypass_valve));
	if(m_joystick_turbine_inlet.check_focus())
		Focus::set(std::make_unique<ValveJoystick>(&sys.loop.turbine_inlet_valve));
	if(m_switch_pump.check_focus())
		sys.loop.primary_pump.powered = !sys.loop.primary_pump.powered;
	if(m_switch_inlet.check_focus())
		sys.loop.turbine_inlet_valve.toggle_auto();
	if(m_switch_bypass.check_focus())
		sys.loop.turbine_bypass_valve.toggle_auto();
	
	gm_switch_inlet.model_matrix = glm::translate(glm::mat4(1), glm::vec3(0, sys.loop.turbine_inlet_valve.get_auto() ? 0.07 : 0, 0));
	gm_switch_bypass.model_matrix = glm::translate(glm::mat4(1), glm::vec3(0, sys.loop.turbine_bypass_valve.get_auto() ? 0.07 : 0, 0));
	gm_switch_pump.model_matrix = glm::translate(glm::mat4(1), glm::vec3(0, sys.loop.primary_pump.powered ? 0.07 : 0, 0));
}

void PrimaryLoop::render()
{
	mesh1.bind();
	mesh1.uniform();
	mesh1.render();

	mesh2.bind();
	mesh2.uniform();
	mesh2.render();
	
	gm_switch_pump.bind();
	gm_switch_pump.uniform();
	gm_switch_pump.render();
	
	gm_switch_inlet.bind();
	gm_switch_inlet.uniform();
	gm_switch_inlet.render();

	gm_switch_bypass.bind();
	gm_switch_bypass.uniform();
	gm_switch_bypass.render();
}

