package io.gitlab.jfronny.ImgJava.imageProcessing;

import io.gitlab.jfronny.ImgJava.util.MColor;
import io.gitlab.jfronny.ImgJava.util.Picture;

import java.awt.*;

/**
 * Algorithmen zur Änderung der Pixelpositionen eines Pictures
 * z.B. drehen, spiegeln usw.
 *
 * @author Thomas Schaller
 * @version 1.1 (28.11.2019)
 */
public class ImageUtil {
    public enum MirrorMode {
        Vertical,
        Horizontal
    }

    public enum RotateMode {
        Left,
        Right
    }

    /**
     * Horizontally mirrors an image (mutates original instance)
     *
     * @param picture Picture to mirror
     * @return The mirrored image
     */
    public static Picture mirror(Picture picture, MirrorMode mirrorMode) {
        int w = picture.getWidth();
        int h = picture.getHeight();

        Color[][] pixel = picture.getPixelArray();
        Color[][] pixelNeu = new Color[w][h];

        for (int x = 0; x < w; x++) {
            for (int y = 0; y < h; y++) {
                pixelNeu[x][y] = switch (mirrorMode) {
                    case Vertical -> pixel[x][h - y - 1];
                    case Horizontal -> pixel[w - x - 1][y];
                };
            }
        }

        picture.setPixelArray(pixelNeu);
        return picture;
    }

    /**
     * Rotates an image by 90° (mutates original instance)
     *
     * @param picture Picture to mirror
     * @return The mirrored image
     */
    public static Picture rotate(Picture picture, RotateMode rotateMode) {
        int w = picture.getWidth();
        int h = picture.getHeight();

        Color[][] pixel = picture.getPixelArray();
        Color[][] pixelNeu = new Color[h][w];

        for (int x = 0; x < w; x++) {
            for (int y = 0; y < h; y++) {
                pixelNeu[y][x] = switch (rotateMode) {
                    case Left -> pixel[w - x - 1][y];
                    case Right -> pixel[x][h - y - 1];
                };
            }
        }

        picture.setPixelArray(pixelNeu);
        return picture;
    }


    public static Picture blurBox(Picture picture, int amount) {
        return applyMatrix(picture, new double[][]{
                new double[] {1, 1, 1},
                new double[] {1, 1, 1},
                new double[] {1, 1, 1}
        }, amount);
    }

    public static Picture blurGauss(Picture picture, int radius, int amount) {
        double sigma = radius / 2d;
        int bl = radius * 2 + 1;
        double[][] matrix = new double[bl][bl];
        
        for (int x = 0; x < bl; x++) {
            for (int y = 0; y < bl; y++) {
                matrix[x][y] = gauss(x - radius, y - radius, sigma);
            }
        }

        return applyMatrix(picture, matrix, amount);
    }

    private static double gauss(double dX, double dY, double stDev) {
        return Math.exp(-((dX * dX + dY * dY) / (2 * stDev * stDev))) / (2 * Math.PI * stDev * stDev);
    }

    public static Picture sharpen(Picture picture, int amount) {
        return applyMatrix(picture, new double[][]{
                new double[] {0, -1, 0},
                new double[] {-1, 5, -1},
                new double[] {0, -1, 0}
        }, amount);
    }

    public static Picture applyMatrix(Picture picture, double[][] matrix, int amount) {
        int w = picture.getWidth();
        int h = picture.getHeight();

        int mW = matrix.length;
        int mH = matrix[0].length;
        if ((mW % 2 != 1) || (mH % 2 != 1))
            throw new IndexOutOfBoundsException("matrix must not have round length");
        
        mW /= 2;
        mH /= 2;

        Color[][] pixel;
        Color[][] pixelNeu = picture.getPixelArray();
        
        for (int i = 0; i < amount; i++) {
            pixel = pixelNeu;
            pixelNeu = new Color[w][h];

            for (int x = 0; x < w; x++) {
                for (int y = 0; y < h; y++) {

                    pixelNeu[x][y] = pixel[x][y];

                    MColor sum = new MColor();

                    double gSum = 0;

                    for (int dx = -mW; dx <= mW; dx++) {
                        for (int dy = -mH; dy <= mH; dy++) {
                            if (x + dx >= 0 && y + dy >= 0 && x + dx < w && y + dy < h) {
                                double g = matrix[mW + dx][mH + dy];
                                gSum += g;
                                sum.addM(pixel[x + dx][y + dy], g);
                            }
                        }
                    }

                    pixelNeu[x][y] = sum.div(gSum).get();
                }
            }
        }

        picture.setPixelArray(pixelNeu);
        return picture;
    }
}
