import org.lwjgl.glfw.*;
import org.lwjgl.opengl.GL;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.system.MemoryUtil.*;

public class uloha02 {
    int width = 500;
    int height = 450;
    double[] zacFarba;
    double[] konFarba;
    double[][] zacUtvar;
    double[][] konUtvar;
    double t = 0;
    String[] nazvySuborov = new String[]{"trojuholnik.txt", "obdlznik.txt", "4hviezda.txt", "6hviezda.txt", "srdce.txt"};

    long window;
    GLFWErrorCallback errorCallback;
    GLFWKeyCallback keyCallback;

    public double[][] precitajSubor(File subor) {
        double[][] body;
        try (Scanner sc = new Scanner(subor)) {
            body = new double[sc.nextInt()][2];
            for (int i = 0; i < body.length; i++) {
                sc.nextLine();
                body[i] = new double[]{sc.nextDouble(), sc.nextDouble()};
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
        return body;
    }

    public void spusti() {
        try {
            init();
            GL.createCapabilities();
            loop();

            glfwDestroyWindow(window);
            keyCallback.free();
        } finally {
            glfwTerminate();
            errorCallback.free();
        }
    }

    public void init() {
        glfwSetErrorCallback(errorCallback = GLFWErrorCallback.createPrint(System.err));
        if (!glfwInit())
            throw new IllegalStateException("Chyba pri inicializacii GLFW!!!");

        window = glfwCreateWindow(width, height, "morphing", NULL, NULL);
        if (window == NULL)
            throw new RuntimeException("Chyba pri vytvoreni GLFW okna!!!");

        glfwSetKeyCallback(window, keyCallback = new GLFWKeyCallback() {
            @Override
            public void invoke(long window, int key, int scancode, int action, int mods) {
                if (key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE)
                    glfwSetWindowShouldClose(window, true);
            }
        });

        GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
        glfwSetWindowPos(window, (vidmode.width() - width) / 2, (vidmode.height() - height) / 2);

        glfwMakeContextCurrent(window);
        glfwSwapInterval(0);
        glfwShowWindow(window);

    }

    public void vykresliGL() {
        glBegin(GL_TRIANGLE_FAN);
        glVertex2d(250, 225);
        //nacitajte utvar a farbu
        glEnd();
    }

    public void loop() {
        glViewport(0, 0, width, height);

        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(-0.5, width - 0.5, height - 0.5, -0.5, 0, 1);

        glShadeModel(GL_FLAT);

        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();

        glClearColor(1.f, 1.f, 1.f, 1.f);// Initialize clear color


        zacUtvar = precitajSubor(new File(nazvySuborov[(int)(Math.random()*nazvySuborov.length)]));
        konUtvar = precitajSubor(new File(nazvySuborov[(int)(Math.random()*nazvySuborov.length)]));
        zacFarba = new double[]{Math.random(), Math.random(), Math.random()};
        konFarba = new double[]{Math.random(), Math.random(), Math.random()};

        while (!glfwWindowShouldClose(window)) {
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

            vykresliGL();

            glfwSwapBuffers(window);
            glfwPollEvents();
            try {
                Thread.sleep(16);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public static void main(String[] args) {
        new uloha02().spusti();
    }
}