package speiger.src.coreengine.rendering.guiOld.components.window.color;

import java.awt.Color;
import java.util.function.IntConsumer;

import speiger.src.coreengine.math.MathUtils;
import speiger.src.coreengine.math.color.ColorSpaces;
import speiger.src.coreengine.math.color.ColorUtils;
import speiger.src.coreengine.math.misc.Facing;
import speiger.src.coreengine.math.vector.floats.Vec2f;
import speiger.src.coreengine.rendering.guiOld.UITextures;
import speiger.src.coreengine.rendering.guiOld.components.ButtonComponent;
import speiger.src.coreengine.rendering.guiOld.components.GradientSliderComponent;
import speiger.src.coreengine.rendering.guiOld.components.SliderComponent;
import speiger.src.coreengine.rendering.guiOld.components.TextFieldComponent;
import speiger.src.coreengine.rendering.guiOld.components.WindowComponent;
import speiger.src.coreengine.rendering.guiOld.components.icon.IIcon;
import speiger.src.coreengine.rendering.guiOld.components.icon.TexturedIcon;
import speiger.src.coreengine.rendering.guiOld.helper.box.GuiBox;
import speiger.src.coreengine.rendering.guiOld.helper.box.IGuiBox;
import speiger.src.coreengine.rendering.guiOld.helper.constrains.Constrains;
import speiger.src.coreengine.rendering.guiOld.helper.constrains.ParentConstrain;
import speiger.src.coreengine.rendering.guiOld.helper.constrains.RelativeConstrain;
import speiger.src.coreengine.utils.functions.Functions;
import speiger.src.coreengine.utils.helpers.InternalThreadPools;

public class ColorPickerWindowComponent extends WindowComponent
{
	IntConsumer listener;
	IIcon wheelIcon = new TexturedIcon(UITextures.COLOR_WHEEL);
	IGuiBox wheelBox = new GuiBox(15, 10, 70, 70);
	IGuiBox selectedBox = new GuiBox(5F, 94F, 50F, 10F);
	int[] colors = new int[]{ColorUtils.rgb(0), ColorUtils.rgb(0), ColorUtils.rgb(0), ColorUtils.rgb(0), ColorUtils.rgb(0)};
	SliderComponent brightness = new GradientSliderComponent(95, 13, 17, 76, 0, 100, 0, ColorUtils.GRAY, colors[2], colors[3], Facing.NORTH, null).setScrollEffect(-1).setVertical(true);
	SliderComponent saturation = new GradientSliderComponent(110, 13, 17, 76, 0, 100, 0, ColorUtils.GRAY, colors[0], colors[1], Facing.NORTH, null).setScrollEffect(-1).setVertical(true);
	TextFieldComponent code = new TextFieldComponent(60, 94, 104, 20, ColorUtils.DARK_GRAY).setValidator(Functions.NUMBERS_ONLY).setMaxTextLength(8);
	float[] hsv = new float[3];
	IGuiBox[] colorBoxes;
	ColorPool pool;

	public ColorPickerWindowComponent(float x, float y, ColorPool pool, int defaultColor, IntConsumer listener, String name)
	{
		super(x, y, 125, 140, FIXED_SIZE_POPUP, name);
		this.pool = pool;
		this.listener = listener;
		hsv = ColorSpaces.ABGR.toHue(defaultColor);
		colorBoxes = new IGuiBox[Math.min(18, pool.size())];
		for(int i = 0,m=colorBoxes.length;i<m;i++)
		{
			addBox(colorBoxes[i] = new GuiBox(5 + ((i % 9) * 12), 108 + (i >= 9 ? 12 : 0), 10, 10));
		}
	}
	
	@Override
	public void init()
	{
		super.init();
		addBox(wheelBox);
		addBox(selectedBox);
		addChild(brightness.onChange(minimizedListener).onAction(T -> setColor(hsv[0], hsv[1], T.cast(SliderComponent.class).getValue() * 0.01F)));
		addChild(saturation.onChange(minimizedListener).onAction(T -> setColor(hsv[0], T.cast(SliderComponent.class).getValue() * 0.01F, hsv[2])));
		addChild(code.setScale(0.5F).onChange(minimizedListener).onAction(_ -> onTyped()));
		addChild(new ButtonComponent(0F, 0F, 0F, 20F, "Select", ColorUtils.GREEN).setScale(0.4F).onAction(_ -> apply()), new Constrains(null, new ParentConstrain(8F).invert(), new RelativeConstrain(0.5F / 0.4F), null));
		addChild(new ButtonComponent(0F, 0F, 0F, 20F, "Cancel", ColorUtils.RED).setScale(0.4F).onAction(T -> T.getGui().removeComponent(this)), new Constrains(new RelativeConstrain(0.5F), new ParentConstrain(8F).invert(), new RelativeConstrain(0.5F / 0.4F), null));
		setColor(hsv[0], hsv[1], hsv[2]);
	}
	
	@Override
	public boolean isPopup()
	{
		return true;
	}
	
	@Override
	protected boolean renderSelf(int mouseX, int mouseY, float particalTicks)
	{
		super.renderSelf(mouseX, mouseY, particalTicks);
		wheelIcon.render(getRenderer().translate(0F, 0F, 0.01F).setBrightness(hsv[1]), wheelBox);
		getRenderer().setBrightness(1F);
		for(int i = 0,m=colorBoxes.length;i<m;i++)
		{
			IGuiBox box = colorBoxes[i];
			getRenderer().drawQuad(box, pool.getColor(i)).drawFrame(box, ColorUtils.DARK_GRAY);
		}
		getRenderer().drawQuad(selectedBox, colors[4]).drawFrame(selectedBox, ColorUtils.DARK_GRAY);
		float centerX = wheelBox.getCenterX();
		float centerY = wheelBox.getCenterY();
		Vec2f pos = InternalThreadPools.VEC2F.get().set(centerX, centerY).add(0F, -(hsv[2] * 33F)).rotate((float)Math.toRadians(-hsv[0] * 360F), centerX, centerY);
		getRenderer().drawQuad(pos.x() - 1F, pos.y() - 1F, pos.x() + 1F, pos.y() + 1, 0.1F, ColorUtils.BLACK);
		InternalThreadPools.VEC2F.accept(pos);
		getRenderer().translate(0F, 0F, -0.01F);
		return true;
	}
	
	private void onTyped()
	{
		try
		{
			int color = Integer.decode(code.getText());
			float[] hsv = Color.RGBtoHSB((color >> 16) & 0xFF, (color >> 8) & 0xFF, color & 0xFF, new float[3]);
			setColor(hsv[0], hsv[1], hsv[2]);
		}
		catch(Exception e){}
	}
	
	public void apply()
	{
		pool.addColor(colors[4]);
		listener.accept(colors[4]);
		getGui().removeComponent(this);
	}
	
	@Override
	public boolean onClick(int button, int mouseX, int mouseY)
	{
		return wheelBox.isColiding(mouseX, mouseY) ? onChanged(mouseX, mouseY) : super.onClick(button, mouseX, mouseY);
	}
	
	@Override
	public boolean onDrag(int mouseX, int mouseY)
	{
		return super.onDrag(mouseX, mouseY) || (wheelBox.isColiding(mouseX, mouseY) ? onChanged(mouseX, mouseY) : false);
	}
	
	private boolean onChanged(int mouseX, int mouseY)
	{
		Vec2f pos = InternalThreadPools.VEC2F.get().set(mouseX, mouseY);
		float radius = (float)pos.distanceTo(wheelBox.getCenterX(), wheelBox.getCenterY());
		if(radius > 38)
		{
			InternalThreadPools.VEC2F.accept(pos);
			return false;
		}
		float hue = (pos.directionAngle(wheelBox.getCenterX(), wheelBox.getCenterY()) + 180F) % 360F;
		setColor(hue / 360F, hsv[1], radius / 33F);
		InternalThreadPools.VEC2F.accept(pos);
		return true;
	}
	
	protected void setColor(float hue, float saturation, float brightness)
	{
		hsv[0] = MathUtils.clamp(0F, 1F, hue);
		hsv[1] = MathUtils.clamp(0F, 1F, saturation);
		hsv[2] = MathUtils.clamp(0F, 1F, brightness);
		colors[0] = ColorSpaces.ABGR.fromHue(hsv[0], 0F, 1F);
		colors[1] = ColorSpaces.ABGR.fromHue(hsv[0], 1F, 1F);
		colors[2] = ColorSpaces.ABGR.fromHue(hsv[0], 1F, 0F);
		colors[3] = ColorSpaces.ABGR.fromHue(hsv[0], 1F, 1F);
		colors[4] = ColorSpaces.ABGR.fromHue(hsv[0], hsv[1], hsv[2]);
		code.setText(ColorUtils.toHTML(colors[4], false));
		this.brightness.setValue((int)(hsv[2] * 100F));
		this.saturation.setValue((int)(hsv[1] * 100F));
	}
}
