package org.example;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Objects;
import java.util.Set;

public class Pracownik implements Comparable<Pracownik> {
    private final String imie;
    private final String nazwisko;
    private final int lata_pracy;
    private final double pensja;
    private final String stanowisko;
    private final Set<Pracownik> children;
    private final String imagePath;

    public Pracownik(String imie, String nazwisko, int lata_pracy, double pensja, String stanowisko, Set<Pracownik> children, String imagePath) {
        this.imie = imie;
        this.nazwisko = nazwisko;
        this.lata_pracy = lata_pracy;
        this.pensja = pensja;
        this.stanowisko = stanowisko;
        this.children = children;
        this.imagePath = imagePath;
    }

    public String getImie() {
        return imie;
    }

    public String getNazwisko() {return nazwisko;}

    public int getLataPracy() {
        return lata_pracy;
    }

    public double getPensja() {
        return pensja;
    }

    public String getStanowisko() { return stanowisko; }

    public Set<Pracownik> getChildren() {
        return children;
    }

    public String getImage() { return imagePath; }

    @Override
    public int compareTo(Pracownik other) {
        // Naturalny porządek
        int bySurname = this.nazwisko.compareTo(other.nazwisko);
        if (bySurname != 0) return bySurname;

        int byName = this.imie.compareTo(other.imie);
        if (byName != 0) return byName;

        int byStanowisko = this.stanowisko.compareTo(other.stanowisko);
        if (byStanowisko != 0) return byStanowisko;

        int byLataPracy = Integer.compare(this.lata_pracy, other.lata_pracy);
        if (byLataPracy != 0) return byLataPracy;

        return Double.compare(this.pensja, other.pensja);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Pracownik)) return false;
        Pracownik pracownik = (Pracownik) o;
        return lata_pracy == pracownik.lata_pracy && pensja == pracownik.pensja && Objects.equals(imie, pracownik.imie);
    }

    @Override
    public int hashCode() {
        return Objects.hash(imie, lata_pracy, pensja);
    }

    @Override
    public String toString() {
        return String.format("Pracownik{%s, %s, %s, %d, %.2f}", stanowisko, nazwisko, imie, lata_pracy, pensja);
    }

    public void makeImageBlackAndWhite() {
        System.out.println(imagePath);
        BufferedImage image = null;
        try {
            File inputFile = new File(
                    imagePath);

            image = ImageIO.read(inputFile);
            System.out.println("Image read successfully.");
        }
        catch (IOException e) {
            System.out.println("Error while reading image: " + e.getMessage());
        }
        BufferedImage img = image;
        if (img == null) {
            System.out.println("Error while reading image: ");
            return;
        }
        int width = img.getWidth();
        int height = img.getHeight();
        int[] pixels = img.getRGB(0, 0, width, height, null, 0, width);
        // convert to grayscale
        for (int i = 0; i < pixels.length; i++) {

            // Here i denotes the index of array of pixels
            // for modifying the pixel value.
            int p = pixels[i];

            int a = (p >> 24) & 0xff;
            int r = (p >> 16) & 0xff;
            int g = (p >> 8) & 0xff;
            int b = p & 0xff;

            // calculate average
            int avg = (r + g + b) / 3;

            // replace RGB value with avg
            p = (a << 24) | (avg << 16) | (avg << 8) | avg;

            pixels[i] = p;
        }
        img.setRGB(0, 0, width, height, pixels, 0, width);
        try {
            File outputFile = new File(
                    imagePath + ".png");

            ImageIO.write(img, "png", outputFile);
            System.out.println("Image written successfully.");
        }
        catch (IOException e) {
            System.out.println("Error while writing image: " + e.getMessage());
        }
    }
}
