package joshie.enchiridion.gui.book.features;

import joshie.enchiridion.api.book.IButtonAction;
import joshie.enchiridion.api.book.IFeatureProvider;
import joshie.enchiridion.gui.book.GuiSimpleEditor;
import joshie.enchiridion.gui.book.GuiSimpleEditorButton;

import java.util.List;

public class FeatureButton extends FeatureJump {
	public FeatureImage deflt;
	public FeatureImage hover;
	public IButtonAction action;
	public float size = 1F;
	public transient FeatureText textHover;
	public transient FeatureText textUnhover;
	public transient IFeatureProvider provider;
	private transient boolean isInit = false;
	
	public FeatureButton(){}
	public FeatureButton(String deflt, String hover, IButtonAction action) {
		this.deflt = new FeatureImage(deflt);
		this.hover = new FeatureImage(hover);
		this.action = action;
	}
	
	@Override
	public FeatureButton copy() {
	    FeatureButton button = new FeatureButton(deflt.path, hover.path, action.copy());
        button.size = size;
        button.textHover = textHover == null ? null : textHover.copy();
        button.textUnhover = textUnhover == null ? null : textUnhover.copy();
        return button;
	}
	
	@Override
	public void update(IFeatureProvider position) {
		if (deflt == null || hover == null) return;
		else {
		    isInit = false; //Reset the init when something changes
		    provider = position;
			deflt.update(position);
			hover.update(position);
			textHover = new FeatureText(action.getHoverText());
			textHover.size = this.size;
			textHover.update(position);
			textUnhover = new FeatureText(action.getUnhoverText());
			textUnhover.update(position);
			textHover.size = this.size;
		}
	}

    @Override
    public void keyTyped(char character, int key) {
        if (textHover != null) {
            textHover.keyTyped(character, key);
            this.size = textHover.size;
        }

        if (textUnhover != null){
            textUnhover.keyTyped(character, key);
            this.size = textHover.size;
        }
    }
	
	@Override
    public void draw(int xPos, int yPos, double width, double height, boolean isMouseHovering) {
		if (deflt == null || hover == null) return;
		if (!isInit && action != null) { //Called here because action needs everything to be loaded, where as update doesn't
		    action.initAction();
		    isInit = true;
		}
		
		if (isMouseHovering) {
		    hover.draw(xPos, yPos, width, height, isMouseHovering);
		    if (textHover != null) textHover.draw(xPos, yPos, width, height, isMouseHovering);
		} else {
			deflt.draw(xPos, yPos, width, height, isMouseHovering);
			if (textUnhover != null) textUnhover.draw(xPos, yPos, width, height, isMouseHovering);
		}
	}
	
	@Override
	public boolean getAndSetEditMode() {
		if (deflt == null || hover == null) return false;
		GuiSimpleEditor.INSTANCE.setEditor(GuiSimpleEditorButton.INSTANCE.setButton(this));		
		return false;
	}
	
	@Override
	public void performClick(int mouseX, int mouseY) {
		if (action != null) action.performAction();
	}
	
	@Override
	public void addTooltip(List<String> tooltip, int mouseX, int mouseY) {
		if (action != null) {
			String[] title = action.getTooltip().split("\n");
			if (title != null) {
				boolean first = false;
				for (String t: title) {
					if (first || !t.equals("")) {
						tooltip.add(t);
					} else first = true;
				}
			}
		}
	}
}

@@ -252,16 +252,66 @@
 rButton;
+%0Aimport joshie.enchiridion.helpers.MCClientHelper;
 %0A%0Aimport
@@ -1668,32 +1668,43 @@
 (position);%0A
-%09%09%09
+            
 text
-H
+Unh
 over.size = 
@@ -1804,77 +1804,248 @@
 if (
-textHover != null) %7B%0A            textHover.keyTyped(character, key);%0A
+MCClientHelper.isShiftPressed()) %7B%0A            boolean changed = false;%0A            if (key == 78) %7B%0A                size = Math.min(15F, Math.max(0.5F, size + 0.1F));%0A                changed = true;%0A            %7D else if (key == 74) %7B%0A    
     
@@ -2056,36 +2056,95 @@
     
-this.
 size = 
-textHover.size;%0A
+Math.min(15F, Math.max(0.5F, size - 0.1F));%0A                changed = true;%0A    
     
@@ -2162,32 +2162,25 @@
     
+ 
+   
 if (
-textUnhover != null)
+changed) 
 %7B%0A  
@@ -2193,43 +2193,37 @@
     
+    
 text
-Unh
+H
 over.
-keyTyped(character, key
+update(provider
 );%0A 
@@ -2237,35 +2237,55 @@
     
-this.size =
+   
  text
-H
+Unh
 over.
-size;
+update(provider);%0A            %7D
 %0A   
@@ -3166,28 +3166,27 @@
 %09%09%0A%09%09return 
-fals
+tru
 e;%0A%09%7D%0A%09%0A%09@Ov

Fix button resizing.