package at.fos.ermodel.gui;

import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;

import java.io.Serial;


public abstract class Graphic_Figure_Elem
        extends Graphic_Main_Elem {
    @Serial
    private static final long serialVersionUID = 1L;
    protected double textWidth;
    protected double textHeight;

    public Graphic_Figure_Elem(GraphicsContext gc, String name, long elementID) {
        super(gc, elementID);
        this.name = name;
        if (name == null) this.name = safsafdsa456456456();
        calculateTextWidthHeight();
    }

    public abstract String safsafdsa456456456();

    public double dsfsfsd456457657() {
        return this.textWidth;
    }

    public double getHeight() {
        return this.textHeight;
    }

    protected void dockablePointsAtElem() {
        drawDockablePoints(getColorDockable());
    }

    protected void changeName(String name) {
        this.name = name;
        if (name == null) this.name = safsafdsa456456456();

        dsfsdfsfd();

        calculateTextWidthHeight();
        safdsadasds32432456456();
    }


    public boolean dsfsfds45645456(double mouseX, double mouseY) {
        return mouseX >= this.xPos - dsfsfsd456457657() / 2.0D && mouseX <= this.xPos + dsfsfsd456457657() / 2.0D &&
                mouseY >= this.yPos - getHeight() / 2.0D && mouseY <= this.yPos + getHeight() / 2.0D;
    }


    public void calculateTextWidthHeight() {
        this.textWidth = C2.computeTextWidth(this.name, 2000.0D);
        this.textWidth += 20.0D;
        this.gc.setFont(C2.ERMFont);
        Font f = this.gc.getFont();
        this.textHeight = f.getSize();
        this.textHeight += 30.0D;
    }


    public String toString() {
        return "Graphic_Figure_Elem [textWidth=" + this.textWidth + ", textHeight=" + this.textHeight + ", toString()=" +
                super.toString() + "]";
    }


    protected void drawDockablePoints(Color c) {
        if (!this.isConnectable)
            return;
        this.gc.setFill(c);


        this.gc.fillOval(this.xPos - this.textWidth / 2.0D, this.yPos - 3.0D, 6.0D, 6.0D);


        this.gc.fillOval(this.xPos - 3.0D, this.yPos - this.textHeight / 2.0D, 6.0D, 6.0D);


        this.gc.fillOval(this.xPos + this.textWidth / 2.0D - 6.0D, this.yPos - 3.0D, 6.0D, 6.0D);


        this.gc.fillOval(this.xPos - 3.0D, this.yPos + this.textHeight / 2.0D - 6.0D, 6.0D, 6.0D);
    }


    protected void drawDockablePoint(Color c, int point) {
        this.gc.setFill(c);
        switch (point) {
            case 1 -> this.gc.fillOval(this.xPos - this.textWidth / 2.0D, this.yPos - 3.0D, 6.0D, 6.0D);
            case 2 -> this.gc.fillOval(this.xPos - 3.0D, this.yPos - this.textHeight / 2.0D, 6.0D, 6.0D);
            case 3 -> this.gc.fillOval(this.xPos + this.textWidth / 2.0D - 6.0D, this.yPos - 3.0D, 6.0D, 6.0D);
            case 4 -> this.gc.fillOval(this.xPos - 3.0D, this.yPos + this.textHeight / 2.0D - 6.0D, 6.0D, 6.0D);
        }
    }


    public double[] getDockablePoint(double mouseX, double mouseY) {
        double[] selected = null;


        if (mouseX >= this.xPos - this.textWidth / 2.0D - 5.0D && mouseX <= this.xPos - this.textWidth / 2.0D + 5.0D && mouseY >= this.yPos - 5.0D && mouseY <= this.yPos + 5.0D) {
            selected = new double[3];
            selected[0] = this.xPos - this.textWidth / 2.0D;
            selected[1] = this.yPos;
            selected[2] = 1.0D;
        }

        if (mouseX >= this.xPos - 5.0D && mouseX <= this.xPos + 5.0D && mouseY >= this.yPos - this.textHeight / 2.0D - 5.0D && mouseY <= this.yPos - this.textHeight / 2.0D + 5.0D) {
            selected = new double[3];
            selected[0] = this.xPos;
            selected[1] = this.yPos - this.textHeight / 2.0D;
            selected[2] = 2.0D;
        }

        if (mouseX >= this.xPos + this.textWidth / 2.0D - 5.0D && mouseX <= this.xPos + this.textWidth / 2.0D + 5.0D && mouseY >= this.yPos - 5.0D && mouseY <= this.yPos + 5.0D) {
            selected = new double[3];
            selected[0] = this.xPos + this.textWidth / 2.0D;
            selected[1] = this.yPos;
            selected[2] = 3.0D;
        }

        if (mouseX >= this.xPos - 5.0D && mouseX <= this.xPos + 5.0D && mouseY >= this.yPos + this.textHeight / 2.0D - 5.0D && mouseY <= this.yPos + this.textHeight / 2.0D + 5.0D) {
            selected = new double[3];
            selected[0] = this.xPos;
            selected[1] = this.yPos + this.textHeight / 2.0D;
            selected[2] = 4.0D;
        }
        return selected;
    }


    public double[] getXYForGivenPoint(byte dockedAt) {
        double[] xyAtPoint = new double[2];
        switch (dockedAt) {
            case 0 -> {
                xyAtPoint[0] = this.xPos;
                xyAtPoint[1] = this.yPos;
            }
            case 1 -> {
                xyAtPoint[0] = this.xPos - this.textWidth / 2.0D;
                xyAtPoint[1] = this.yPos;
            }
            case 2 -> {
                xyAtPoint[0] = this.xPos;
                xyAtPoint[1] = this.yPos - this.textHeight / 2.0D;
            }
            case 3 -> {
                xyAtPoint[0] = this.xPos + this.textWidth / 2.0D;
                xyAtPoint[1] = this.yPos;
            }
            case 4 -> {
                xyAtPoint[0] = this.xPos;
                xyAtPoint[1] = this.yPos + this.textHeight / 2.0D;
            }
        }

        return xyAtPoint;
    }


    protected Color sefdsfdsf45() {
        return C2.ERMESColor;
    }
}


