package plugin.bonus;

import plugin.IPlugin;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

public class Relief implements IPlugin
{

	@Override
	public BufferedImage perform(BufferedImage img)
	{
		BufferedImage res = new BufferedImage(img.getWidth(), img.getHeight(), img.getType());
		for (int i = 0; i < img.getWidth(); i++)
			for (int j = 0; j < img.getHeight(); j++) {
				Color c = new Color(img.getRGB(i, j));
				int tmp = (c.getBlue() + c.getRed() + c.getGreen()) / 3;
				c = new Color(tmp, tmp, tmp);
				res.setRGB(i, j, c.getRGB());
			}


		BufferedImage res2 = new BufferedImage(img.getWidth(), img.getHeight(), img.getType());
		for (int i = 1; i < img.getWidth() - 1; i++)
			for (int j = 1; j < img.getHeight() - 1; j++) {
				ArrayList<Color> pix = new ArrayList<>();
				pix.add(new Color(res.getRGB(i, j)));
				pix.add(new Color(res.getRGB(i - 1, j - 1)));
				pix.add(new Color(res.getRGB(i, j - 1)));
				pix.add(new Color(res.getRGB(i + 1, j - 1)));
				pix.add(new Color(res.getRGB(i - 1, j)));
				pix.add(new Color(res.getRGB(i + 1, j)));
				pix.add(new Color(res.getRGB(i - 1, j + 1)));
				pix.add(new Color(res.getRGB(i, j + 1)));
				pix.add(new Color(res.getRGB(i + 1, j + 1)));
				int red = 8 * pix.get(0).getRed();
				for (int tmp = 1; tmp < pix.size(); tmp++) {
					red = red - pix.get(tmp).getRed();
				}
				red = red / 1;
				red = red + 128;
				red = 255 - red;
				if (red < 0)
					red = 0;
				if (red > 255)
					red = 255;
				int green = red;
				int blue = red;
				res2.setRGB(i, j, new Color(red, blue, green).getRGB());
			}

		return res2;
	}

	@Override
	public String getName()	{
		return "Lines";
	}

}