package mnm.mods.tabbychat.gui;

import java.awt.Point;
import java.awt.Rectangle;

import org.lwjgl.input.Mouse;

import mnm.mods.tabbychat.TabbyChat;
import mnm.mods.tabbychat.core.GuiNewChatTC;
import mnm.mods.tabbychat.settings.ColorSettings;
import mnm.mods.tabbychat.settings.TabbySettings;
import mnm.mods.util.gui.BorderLayout;
import mnm.mods.util.gui.GuiPanel;
import mnm.mods.util.gui.events.GuiMouseAdapter;
import mnm.mods.util.gui.events.GuiMouseEvent;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.gui.ScaledResolution;
import net.minecraft.util.MathHelper;

public class ChatBox extends GuiPanel implements GuiMouseAdapter {

    private static ColorSettings colors = TabbyChat.getInstance().settings.colors;

    private ChatArea chatArea;
    private ChatTray pnlTray;
    private TextBox txtChatInput;

    private boolean dragMode;
    private Point drag;
    private Rectangle tempbox;

    public ChatBox(Rectangle rect) {
        super();
        this.setLayout(new BorderLayout());
        this.addComponent(pnlTray = new ChatTray(), BorderLayout.Position.NORTH);
        this.addComponent(chatArea = new ChatArea(), BorderLayout.Position.CENTER);
        this.addComponent(txtChatInput = new TextBox(), BorderLayout.Position.SOUTH);
        this.addComponent(new Scrollbar(chatArea), BorderLayout.Position.EAST);
        this.setBounds(rect);
    }

    @Override
    public void accept(GuiMouseEvent event) {
        Rectangle bounds = getBounds();
        float scale = getActualScale();

        // divide by scale because smaller scales make the point movement larger
        int x = (int) (event.position.x / scale);
        int y = (int) (event.position.y / scale);

        if (event.event == GuiMouseEvent.PRESSED) {
            if (Mouse.isButtonDown(0) && (pnlTray.isHovered() || (GuiScreen.isAltKeyDown() && isHovered()))) {
                dragMode = !pnlTray.isHandleHovered();
                drag = new Point(x, y);
                tempbox = new Rectangle(bounds);
            }
        }

        if (drag != null) {
            if (event.event == GuiMouseEvent.RELEASED) {
                // save bounds
                TabbySettings sett = TabbyChat.getInstance().settings;
                sett.advanced.chatX.setValue(bounds.x);
                sett.advanced.chatY.setValue(bounds.y);

                sett.saveSettingsFile();
                drag = null;
                tempbox = null;
            } else if (event.event == GuiMouseEvent.DRAGGED) {
                if (!dragMode) {
                    bounds.setSize(tempbox.width + x - drag.x, tempbox.height - y + drag.y);
                    bounds.setLocation(tempbox.x, tempbox.y + y - drag.y);
                } else {
                    bounds.setLocation(tempbox.x + x - drag.x, tempbox.y + y - drag.y);
                }
            }
        }
    }

    @Override
    public int getForeColor() {
        return colors.chatTextColor.getValue().getColor();
    }

    @Override
    public int getBackColor() {
        return colors.chatBoxColor.getValue().getColor();
    }

    @Override
    public float getScale() {
        return GuiNewChatTC.getInstance().getChatScale();
    }

    @Override
    public void updateComponent() {
        Rectangle bounds = getBounds();
        Point point = getActualPosition();
        float scale = getActualScale();
        ScaledResolution sr = new ScaledResolution(mc, mc.displayWidth, mc.displayHeight);

        int x = point.x;
        int y = point.y;
        int w = (int) (bounds.width * scale);
        int h = (int) (bounds.height * scale);

        int w1 = w;
        int h1 = h;
        int x1 = x;
        int y1 = y;

        w1 = Math.min(sr.getScaledWidth(), w);
        h1 = Math.min(sr.getScaledHeight(), h);
        w1 = Math.max(50, w1);
        h1 = Math.max(50, h1);

        x1 = Math.max(0, x1);
        x1 = Math.min(x1, sr.getScaledWidth() - w1);
        y1 = Math.max(0, y1);
        y1 = Math.min(y1, sr.getScaledHeight() - h1);

        if (x1 != x || y1 != y || w1 != w || h1 != h) {
            bounds.x = MathHelper.ceiling_double_int(x1 / scale);
            bounds.y = MathHelper.ceiling_double_int(y1 / scale);
            bounds.width = MathHelper.ceiling_double_int(w1 / scale);
            bounds.height = MathHelper.ceiling_double_int(h1 / scale);
        }
        super.updateComponent();
    }

    @Override
    public void onClosed() {
        super.onClosed();
        updateComponent();
    }

    public int getWidth() {
        return getBounds().width;
    }

    public ChatArea getChatArea() {
        return this.chatArea;
    }

    public ChatTray getTray() {
        return this.pnlTray;
    }

    public TextBox getChatInput() {
        return this.txtChatInput;
    }

}

@@ -2346,16 +2346,137 @@
 unds.y);
+%0A                sett.advanced.chatW.setValue(bounds.width);%0A                sett.advanced.chatH.setValue(bounds.height);
 %0A%0A      

Save chat dims when resizing