package joshie.enchiridion.gui.book;

import joshie.enchiridion.EConfig;
import joshie.enchiridion.api.EnchiridionAPI;
import joshie.enchiridion.api.gui.IBookEditorOverlay;
import joshie.enchiridion.util.ITextEditable;
import joshie.enchiridion.util.TextEditor;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.IGuiEventListener;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.widget.TextFieldWidget;

import java.util.List;

public class GuiSimpleEditor extends AbstractGuiOverlay {
    public static final GuiSimpleEditor INSTANCE = new GuiSimpleEditor();
    private TextFieldWidget textField;
    private String text = "";
    private IBookEditorOverlay editor = null;

    private GuiSimpleEditor() {
    }

    public void setEditor(IBookEditorOverlay editor) {
        this.editor = editor;
        this.text = "";
    }

    @Override
    public void init() {
        Minecraft mc = Minecraft.getInstance();
        Screen currentScreen = mc.currentScreen;
        if (currentScreen != null) {
            this.textField = new TextFieldWidget(mc.fontRenderer, mc.currentScreen.width / 5, mc.currentScreen.height / 5, 84, 7, "enchiridion.simpleEditor.search");
            this.textField.setMaxStringLength(32);
            this.textField.changeFocus(true);
            this.textField.setEnableBackgroundDrawing(true); //TODO Set to false when done
            this.textField.setText(text != null && !text.isEmpty() ? text : "");
        }
    }

    @Override
    public void tick() {
        if (editor != null) {
            textField.tick();
        }
    }

    @Override
    public IGuiEventListener getFocused() {
        if (editor != null) {
            return this.textField;
        }
        return super.getFocused();
    }

    @Override
    public void draw(int mouseX, int mouseY) {
        if (editor != null) {
            /* Draw the Background */
            EnchiridionAPI.draw.drawImage(SIDEBAR, EConfig.SETTINGS.editorXPos - 3, EConfig.SETTINGS.toolbarYPos.get() - 7, EConfig.SETTINGS.editorXPos + 87, EConfig.SETTINGS.timelineYPos.get() + 13);EnchiridionAPI.draw.drawBorderedRectangle(EConfig.SETTINGS.editorXPos, EConfig.SETTINGS.toolbarYPos.get() + 7, EConfig.SETTINGS.editorXPos + 85, EConfig.SETTINGS.timelineYPos.get() + 11, 0xFF312921, 0xFF191511);
            EnchiridionAPI.draw.drawBorderedRectangle(EConfig.SETTINGS.editorXPos + 2, EConfig.SETTINGS.toolbarYPos.get() + 9, EConfig.SETTINGS.editorXPos + 83, EConfig.SETTINGS.timelineYPos.get() + 9, 0xFFE4D6AE, 0x5579725A);
            EnchiridionAPI.draw.drawBorderedRectangle(EConfig.SETTINGS.editorXPos, EConfig.SETTINGS.toolbarYPos.get() - 3, EConfig.SETTINGS.editorXPos + 84, EConfig.SETTINGS.toolbarYPos.get() + 7, 0xFF312921, 0xFF191511);
            editor.draw(mouseX, mouseY);
            textField.render(mouseX, mouseY, 0);
        }
    }

    @Override
    public void charTyped(char character, int key) {
        if (editor != null) {
            if (this.textField.charTyped(character, key)) {
                text = textField.getText().trim();
            }
        }
    }

    @Override
    public boolean mouseClicked(int mouseX, int mouseY) {
        if (editor != null) {
            if (mouseX >= EConfig.SETTINGS.editorXPos && mouseX <= EConfig.SETTINGS.editorXPos + 84 && mouseY >= EConfig.SETTINGS.toolbarYPos.get() - 3 && mouseY <= EConfig.SETTINGS.toolbarYPos.get() + 7) {
                this.textField.mouseClicked(mouseX, mouseY, 0);
                return true;
            } else {
                return editor.mouseClicked(mouseX, mouseY);
            }
        }
        return false;
    }

    @Override
    public void addToolTip(List<String> tooltip, int mouseX, int mouseY) {
        if (editor != null) {
            editor.addToolTip(tooltip, mouseX, mouseY);
        }
    }

    @Override
    public void scroll(boolean down, int mouseX, int mouseY) {
        if (editor != null) {
            editor.scroll(down, mouseX, mouseY);
        }
    }

    public String getText() {
        return textField.getText();
    }
}
@@ -170,97 +170,8 @@
 ay;%0A
-import joshie.enchiridion.util.ITextEditable;%0Aimport joshie.enchiridion.util.TextEditor;%0A
 impo
@@ -1026,19 +1026,16 @@
 nderer, 
-mc.
 currentS
@@ -1052,42 +1052,120 @@
 h / 
-5, mc.currentScreen.height / 5, 84
+4 + (EConfig.SETTINGS.editorXPos + 27), currentScreen.height / 4 + (EConfig.SETTINGS.toolbarYPos.get() + 17), 80
 , 7,
@@ -1256,54 +1256,8 @@
 2);%0A
-            this.textField.changeFocus(true);%0A
     
@@ -1306,19 +1306,20 @@
 Drawing(
-tru
+fals
 e); //TO
@@ -1537,24 +1537,78 @@
 eld.tick();%0A
+            this.editor.updateSearch(this.getText());%0A
         %7D%0A  
@@ -2116,16 +2116,29 @@
 ) + 13);
+%0A            
 Enchirid
@@ -2823,32 +2823,77 @@
 ouseX, mouseY);%0A
+            if (textField.isFocused()) %7B%0A    
             text
@@ -2904,16 +2904,22 @@
 d.render
+Button
 (mouseX,
@@ -2923,32 +2923,46 @@
 eX, mouseY, 0);%0A
+            %7D%0A
         %7D%0A    %7D%0A
@@ -3594,41 +3594,142 @@
     
-return true;%0A            %7D else %7B
+this.textField.setFocused2(true);%0A                return true;%0A            %7D else %7B%0A                this.textField.setFocused2(false);
 %0A   
@@ -3777,32 +3777,33 @@
 ouseX, mouseY);%0A
+%0A
             %7D%0A  
@@ -3792,32 +3792,33 @@
 %0A%0A            %7D%0A
+%0A
         %7D%0A      

Work on the search text field