package nz.ac.massey.javaecs.examples.Systems;

import java.util.BitSet;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.concurrent.Task;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import nz.ac.massey.javaecs.ECSSystem;
import nz.ac.massey.javaecs.Engine;
import nz.ac.massey.javaecs.Entity;
import nz.ac.massey.javaecs.examples.Components.BoxRender;
import nz.ac.massey.javaecs.examples.Components.Render;
import nz.ac.massey.javaecs.examples.Components.TextRender;
import nz.ac.massey.javaecs.examples.Components.Vec2D;

public class RenderSystem extends ECSSystem{
    JFXView jFXView;
    Engine gameEngine;
    Thread renderThread;
    double renderScale;
    int screenX = 1024;
    int screenY = 1024;

    public RenderSystem(Engine gameEngine, double renderScale){
        this.gameEngine = gameEngine;
        // Set registrations
        registrationSet = new BitSet();
        registrationSet.set(gameEngine.getComponentIndex(Render.class));
        registrationSet.set(gameEngine.getComponentIndex(Vec2D.class));
        this.renderScale = renderScale;
    }

    @Override
    public void init() {
        // Spawn a new thread
        renderThread = new Thread(new RenderStarter());
        renderThread.start(); // this thread is asynchronous, it isn't expected to rejoin
    }

    @Override
    public void update() {
        /* while (JFXView.root == null){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        } */
        Group renderScene = new Group();
        for (Entity entity : entities) {
            //Vec2D pos = (Vec2D)gameEngine.getComponentData(entity, Vec2D.class);
            if (((Render)gameEngine.getComponentData(entity, Render.class)).renderType == BoxRender.class){
                BoxRender box = (BoxRender)gameEngine.getComponentData(entity, BoxRender.class);
                Vec2D vec2d = (Vec2D)gameEngine.getComponentData(entity, Vec2D.class);

                Rectangle rect = new Rectangle(box.xSize* renderScale, box.ySize * renderScale);
                rect.setFill(new Color(box.r, box.g, box.b, box.a));
                rect.setX(12 + (vec2d.x * renderScale));
                rect.setY(1012 - (vec2d.y * renderScale) - rect.getHeight());
                renderScene.getChildren().add(rect);
            }
            else if (((Render)gameEngine.getComponentData(entity, Render.class)).renderType == TextRender.class){
                Vec2D vec2d = (Vec2D)gameEngine.getComponentData(entity, Vec2D.class);
                TextRender text = (TextRender)gameEngine.getComponentData(entity, TextRender.class);
                Text t = new Text(vec2d.x * renderScale, vec2d.y * renderScale, text.content);
                t.setFont(new Font(text.size));
                t.setFill(text.color);
                t.setStroke(text.color);
                renderScene.getChildren().add(t);
            }
        }
        Platform.runLater(new Runnable(){
            @Override
            public void run() {
                JFXView.root.getChildren().clear();
                JFXView.root.getChildren().add(renderScene);
            }
        });
    }

    // Add a renderer

    public class RenderStarter implements Runnable{

        @Override
        public void run() {
            JFXView.run();
        }
        void updateScene(Group newGroup){
            JFXView.root.getChildren().clear();
            JFXView.root.getChildren().add(newGroup); 
        }
    }

    public static class JFXView extends Application {
        private static Group root;

        @Override
        public void start(Stage primaryStage) throws Exception {
            // based on the colorful circles sample at https://docs.oracle.com/javase/8/javafx/get-started-tutorial/animation.htm 
            root = new Group();
            Scene scene = new Scene(root, 1024, 1024, Color.BLACK);
            primaryStage.setScene(scene);

            primaryStage.show();
        }
        
        public static void run(){
            launch(new String[0]);
        }
    }
}
