package net.pietru.omni_power.ui;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.EventListener;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Stack;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.Widget;
import com.badlogic.gdx.scenes.scene2d.utils.Disableable;
import com.badlogic.gdx.scenes.scene2d.utils.Drawable;
import com.badlogic.gdx.scenes.scene2d.utils.NinePatchDrawable;
import com.badlogic.gdx.utils.viewport.Viewport;
import finalforeach.cosmicreach.gamestates.InGame;
import finalforeach.cosmicreach.items.ISlotContainer;
import finalforeach.cosmicreach.items.Item;
import finalforeach.cosmicreach.items.ItemSlot;
import finalforeach.cosmicreach.items.ItemStack;
import finalforeach.cosmicreach.rendering.items.ItemRenderer;
import finalforeach.cosmicreach.settings.ControlSettings;
import finalforeach.cosmicreach.ui.FontRenderer;
import finalforeach.cosmicreach.ui.GameStyles;
import finalforeach.cosmicreach.ui.UI;

public class TextButtonWidget extends Table implements Disableable {
    public Image buttonImage;
    public Drawable imageDrawable;
    public Drawable imageHoveredDrawable;
    public Drawable imageSelectedDrawable;

    public boolean canSpreadItemTo;
    public boolean hoveredOver;
    public boolean pressed;
    boolean isDisabled;
    private static final Vector2 tmpVec = new Vector2();
    Vector2 textDim;
    String text;

    public TextButtonWidget(String text) {
        this(text, false);
    }

    public TextButtonWidget(String text, boolean isSecondary) {
        this(text, new NinePatchDrawable(isSecondary ? GameStyles.containerOutput9Patch : GameStyles.container9Patch), new NinePatchDrawable(isSecondary ? GameStyles.containerOutput9PatchHovered : GameStyles.container9PatchHovered), new NinePatchDrawable(GameStyles.containerSelected9Patch));
    }

    public TextButtonWidget(String text, Drawable imageDrawable, Drawable imageHoveredDrawable, Drawable imageSelectedDrawable) {
        this.canSpreadItemTo = true;
        this.textDim = new Vector2();
        this.text = text;
        this.imageDrawable = imageDrawable;
        this.imageHoveredDrawable = imageHoveredDrawable;
        this.imageSelectedDrawable = imageSelectedDrawable;
        this.buttonImage = new Image(imageDrawable);

        this.addListener((event) -> {
            if (event instanceof InputEvent ie) {
                return this.onInputEvent(ie);
            } else {
                return false;
            }
        });

        this.add(this.buttonImage);
        this.buttonImage.setWidth(text.length()*6);
        this.setWidth(this.buttonImage.getWidth());
    }

    public void setText(String text) {
        this.text = text;
        this.buttonImage.setWidth(text.length()*6);
        this.setWidth(this.buttonImage.getWidth());
    }

    public String getText() {
        return text;
    }

    private boolean isHoveredOver() {
        return this.hoveredOver && !Gdx.input.isCursorCatched();
    }

    public boolean isPressed() {
        return pressed;
    }

    protected boolean onInputEvent(InputEvent inputEvent) {
        if (Gdx.input.isCursorCatched()) {
            this.pressed = false;
            return false;
        } else {
            InputEvent.Type inputType = inputEvent.getType();
            if (inputType == InputEvent.Type.enter) {
                this.hoveredOver = true;
            }

            if (Gdx.input.isButtonPressed(0) && inputEvent.getType() == InputEvent.Type.touchDown) {
                this.pressed=true;
                return true;
            } else if (Gdx.input.isButtonPressed(1) && inputType == InputEvent.Type.touchDown) {
                this.pressed=true;
                return true;
            }

            if (inputEvent.getType() == InputEvent.Type.touchUp)
                this.pressed = false;

            if (inputType == InputEvent.Type.exit) {
                this.canSpreadItemTo = true;
                this.hoveredOver = false;
                this.pressed = false;
            }

            return false;
        }
    }


    public void draw(Batch batch, float parentAlpha) {
        this.buttonImage.setDrawable(isHoveredOver() ? imageHoveredDrawable : imageDrawable);
        if (pressed)
            this.buttonImage.setDrawable(imageSelectedDrawable);
        this.buttonImage.setWidth(text.length()*10);

        super.draw(batch,parentAlpha);
        batch.setColor(Color.WHITE);
        drawLabelTextWithDropShadow(batch,4,10,Color.GRAY);

        Viewport viewport = this.getStage().getViewport();
        this.buttonImage.localToAscendantCoordinates(null, tmpVec.set(0.0F, 0.0F));
        viewport.project(tmpVec);
        float sx = tmpVec.x;
        float sy = tmpVec.y;
        this.buttonImage.localToAscendantCoordinates(null, tmpVec.set(this.buttonImage.getWidth(), this.buttonImage.getHeight()));
        viewport.project(tmpVec);
    }

    public void drawLabelTextWithDropShadow(Batch batch, float xOff, float yOff, Color dropShadowColor) {
        Color batchColor = batch.getColor();
        float oldR = batchColor.r;
        float oldG = batchColor.g;
        float oldB = batchColor.b;
        float oldA = batchColor.a;
        batch.setColor(dropShadowColor);
        this.drawLabelText(batch, 2.0F+xOff, -2.0F+yOff);
        batch.setColor(oldR, oldG, oldB, oldA);
        this.drawLabelText(batch, 0.0F+xOff, 0.0F+yOff);
    }

    public void drawLabelText(Batch batch, float xOff, float yOff) {
        Viewport viewport = this.getStage().getViewport();
        FontRenderer.getTextDimensions(viewport, this.text, this.textDim);
        float x = this.getX();//- this.textDim.x;
        float y = this.getY();
        FontRenderer.drawText(batch, viewport, this.text, x + xOff, y + yOff, true);
    }

    @Override
    public void setDisabled(boolean b) {
        isDisabled=b;
    }

    @Override
    public boolean isDisabled() {
        return isDisabled;
    }
}
