package at.fos.ermodel.gui;

import javafx.geometry.Insets;
import javafx.scene.image.Image;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Objects;
import java.util.zip.GZIPInputStream;

public class C2 {
    public static final Color MouseSelectedColor = Color.RED;
    public static final Color FocusOnComponentColor = Color.BLUE;
    public static final Color ButtonBackgroundColor = Color.rgb(179, 179, 179);

    public static final Color ERMDockablePointColor = Color.rgb(236, 234, 241);
    public static final Color ERMTextBorderColor = Color.GREY;

    public static Font ERMFont = Font.font(null, FontWeight.BOLD, 12.0D);
    public static Color ERMBackgroundColor = Color.rgb(27, 27, 30);
    public static Color ERMForegroundColor = Color.rgb(48, 48, 52);

    public static Color ERMTextColor = Color.rgb(236, 234, 241);

    public static Background AppBackground = new Background(new BackgroundFill(C2.ERMBackgroundColor, CornerRadii.EMPTY, Insets.EMPTY));
    public static Background AppForeground = new Background(new BackgroundFill(C2.ERMForegroundColor, CornerRadii.EMPTY, Insets.EMPTY));


    public static StackPane alerterror = new StackPane();
    public static StackPane alertconfirmation = new StackPane();
    public static StackPane alertinformation = new StackPane();


    public static Color ERMSelectedColor = Color.RED;

    public static Color ERMESColor = Color.rgb(0, 255, 0);

    public static Color ERMRSColor = Color.AQUA;
    public static Color ERMRSConnColor = Color.AQUA;

    public static Color ERMRSISAColor = Color.BLUEVIOLET;
    public static Color ERMISAConnGENERAL = Color.BLUEVIOLET;
    public static Color ERMISAConnSPEC = Color.AQUA;

    public static Color ERMATTRColor = Color.GREENYELLOW;

    public static Color ERMLINEColor = Color.CHARTREUSE;

    public static Color TEMPLINEColor = Color.PALEGREEN;


    public static Image applicationImageIconAsICO;


    public static int imageWidth = 55;
    public static int imageHeight = 32;

    public static Image treeCloseImage;

    public static Image treeOpenImage;

    public static Image attr_conn_attrImage;
    public static Image attr_conn_esImage;
    public static Image attr_conn_rsImage;
    public static Image attr_derived_multi_valueImage;
    public static Image attr_derived_one_valueImage;
    public static Image attr_key_valueImage;
    public static Image attr_multi_valueImage;
    public static Image attr_one_valueImage;
    public static Image attr_weak_key_valueImage;
    public static Image es_associativeImage;
    public static Image es_weakImage;
    public static Image esImage;
    public static Image rs_conn_esImage;
    public static Image rs_id_conn_es_weakImage;
    public static Image rs_id_conn_esImage;
    public static Image rs_idImage;
    public static Image rs_isa_conn_general_esImage;
    public static Image rs_isa_conn_special_esImage;
    public static Image rs_isaImage;
    public static Image rsImage;
    public static Image textImage;
    public static Image textWithoutBorderImage;
    public static HashMap<String, InputStream> randomDataFiles = new HashMap<>();
    public static HashMap<String, BufferedReader> randomDataFilesExtern = new HashMap<>();

    public C2() {

        alerterror.getStyleClass().add("alert.error.dialog-pane");
        alertinformation.getStyleClass().add("alert.error.dialog-pane");
        alertconfirmation.getStyleClass().add("alert.error.dialog-pane");

        randomDataFiles.put("Random Color", decompress("colors"));
        randomDataFiles.put("Random Company", decompress("companies"));
        randomDataFiles.put("Random Country", decompress("countries"));
        randomDataFiles.put("Random Departmentname", decompress("departmentnames"));
        randomDataFiles.put("Random Firstname", decompress("firstnames"));
        randomDataFiles.put("Random Jobname", decompress("jobnames"));
        randomDataFiles.put("Random Language", decompress("languages"));
        randomDataFiles.put("Random Lastname", decompress("lastnames"));
        randomDataFiles.put("Random Mediacategory", decompress("mediacategories"));
        randomDataFiles.put("Random Mediapublisher", decompress("mediapublishers"));
        randomDataFiles.put("Random Mediatitle", decompress("mediatitles"));
        randomDataFiles.put("Random Mediatype", decompress("mediatypes"));
        randomDataFiles.put("Random Place", decompress("places"));
        randomDataFiles.put("Random Profession", decompress("professions"));
        randomDataFiles.put("Random Rentalequipment", decompress("rentalequipment"));
        randomDataFiles.put("Random Street", decompress("streets"));

        applicationImageIconAsICO = loadImage("erdlogo");
        attr_conn_attrImage = loadImage("attr_conn_attr");
        attr_conn_esImage = loadImage("attr_conn_es");
        attr_conn_rsImage = loadImage("attr_conn_rs");
        attr_derived_multi_valueImage = loadImage("attr_derived_multi_value");
        attr_derived_one_valueImage = loadImage("attr_derived_one_value");
        attr_key_valueImage = loadImage("attr_key_value");
        attr_multi_valueImage = loadImage("attr_multi_value");
        attr_one_valueImage = loadImage("attr_one_value");
        attr_weak_key_valueImage = loadImage("attr_weak_key_value");
        es_associativeImage = loadImage("es_associative");
        es_weakImage = loadImage("es_weak");
        esImage = loadImage("es");
        rs_conn_esImage = loadImage("rs_conn_es");
        rs_id_conn_es_weakImage = loadImage("rs_id_conn_es_weak");
        rs_id_conn_esImage = loadImage("rs_id_conn_es");
        rs_idImage = loadImage("rs_id");
        rs_isa_conn_general_esImage = loadImage("rs_isa_conn_general_es");
        rs_isa_conn_special_esImage = loadImage("rs_isa_conn_special_es");
        rs_isaImage = loadImage("rs_isa");
        rsImage = loadImage("rs");
        textImage = loadImage("text");
        textWithoutBorderImage = loadImage("text_without_border");
        treeOpenImage = loadImage("tree_open");
        treeCloseImage = loadImage("tree_close");
    }

    private static InputStream decompress(String INPUT_FILE) {
        INPUT_FILE = "/compressed_data/" + INPUT_FILE + ".txt.gz";
        byte[] buffer = new byte[1024];
        try {
            GZIPInputStream is =
                    new GZIPInputStream(Objects.requireNonNull(C2.class.getResourceAsStream(INPUT_FILE)));

            StringBuilder out = new StringBuilder();

            while (is.read(buffer) > 0) {
                for (byte b : buffer) {
                    out.append((char) b);
                }
            }
            is.close();

            System.out.println("File Successfully decompressed");

            System.out.println(out);

            return new ByteArrayInputStream(out.toString().getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;

    }

    private static Image loadImage(String FILE_NAME) {
        return new Image(Objects.requireNonNull(C2.class.getResourceAsStream("/applicationimages/" + FILE_NAME + ".png")));
    }

    public static double computeTextWidth(String text, double wrappingWidth) {
        Text helper = new Text();
        helper.setFont(ERMFont);
        helper.setText(text);

        helper.setWrappingWidth(0.0D);
        helper.setLineSpacing(0.0D);
        double w = Math.min(helper.prefWidth(-1.0D), wrappingWidth);
        helper.setWrappingWidth(Math.ceil(w));
        return Math.ceil(helper.getLayoutBounds().getWidth());
    }

    public static double computeTextHeight() {
        return ERMFont.getSize();
    }
}