package view.panes;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import view.TurtleDraw;
import model.Model;
import model.Turtle;
import javafx.geometry.Point2D;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import view.Constants;
import view.controllers.ImagePalette;

/**
 * Responsible for drawing the graphical representation of the turtles movement
 * as its state changes using Observable. Calls on the Draw class to update the
 * graphical representation stored in View when the status of a turtle changes.
 *
 * @author Team 14
 *
 */
public class ViewPaneModule extends PaneModule {

    public boolean gridVisible = true;
    private Color myBackColor;
    private String RGB;
    private Pane myPane = new Pane();
    private TurtleDraw myDraw;
    private Point2D myCurrentPoint = new Point2D(0, 0);
    private List<Line> myGrid = new ArrayList<Line>();
    private Color defColor = Color.LIGHTGRAY;
    private Set<Turtle> myTurtles;
    private ImagePalette myImagePalette;
    private Map<Integer, ImageView> myIcons = new HashMap<Integer, ImageView>();
    private boolean mySelectorCheck = false;
    private Set<Turtle> myActiveTurtles;
    private Model myModel;
    private List<ImageView> myStamps = new ArrayList<ImageView>();

    /**
     * Constructor method called from UserInterface.java
     */
    public ViewPaneModule (ImagePalette ip) {
        myImagePalette = ip;
        myDraw = new TurtleDraw(myImagePalette);
        myBackColor = Constants.DEFAULT_BGCOLOR;
        RGB = toRGB(myBackColor);
        updateBGColor(RGB);
        myPane.setPrefSize(Constants.VIEW_DEFAULT_DIMENSION, Constants.VIEW_DEFAULT_DIMENSION);
        createGrid(defColor);
    }

    public void updateView (Model m) {
        myModel = m;
        myBackColor = m.getBackgroundColor();
        this.updateBGColor(this.toRGB(myBackColor));
        myActiveTurtles = m.getTurtleManager().getActiveTurtles();
        myTurtles = m.getTurtleManager().getAllTurtles();
        myTurtles.forEach(turtle -> updateTurtle(turtle));
    }

    @Override
    public BorderPane addPane (BorderPane p) {
        p.setCenter(myPane);
        return p;
    }

    public void showError (String displayMessage) {
        System.out.println(displayMessage);
    }

    private void createGrid (Color c) {
        int loc = 0;
        int cellSize = 10;
        while (loc < Constants.VIEW_DEFAULT_DIMENSION - 10) {
            Line horizontal = new Line(0, loc, Constants.VIEW_DEFAULT_DIMENSION - 10, loc);
            myGrid.add(horizontal);
            Line vertical = new Line(loc, 0, loc, Constants.VIEW_DEFAULT_DIMENSION);
            myGrid.add(vertical);
            loc += cellSize;
        }
        for (Line l : myGrid) {
            l.setStroke(c);
            l.toBack();
            myPane.getChildren().add(l);
        }
    }

    public void changeGridVisibility () {
        for (Line l : myGrid) {
            l.setVisible(!gridVisible);
            l.toBack();
        }
        gridVisible = !gridVisible;
    }

    private void updateTurtle (Turtle t) {
        int id = (int) t.getID();
        if (!myIcons.containsKey(id)) {
            myIcons.put(id, myDraw.drawTurtle(t, myActiveTurtles.contains(t) && mySelectorCheck));
            if (((int) t.getShowing()) == 1) {
                myDraw.showTurtle(myPane, myIcons.get(id));
            }
        } else {
            myDraw.hideTurtle(myPane, myIcons.get(id));
            myIcons.put(id, myDraw.drawTurtle(t, myActiveTurtles.contains(t) && mySelectorCheck));
            if (((int) t.getShowing()) == 1) {
                myDraw.showTurtle(myPane, myIcons.get(id));
            }
        }
        if (((int) t.getPenStatus()) == 1) {
            myDraw.drawLine(t.getPrevLocation(), t.getNewLocation());
            myDraw.path.setStroke(t.getPenColor());
            myPane.getChildren().add(myDraw.path);
        }

      
        System.out.println(t.getPenSize());
        myPane.getChildren().add(myDraw.path);
        myDraw.setAngle(myIcons.get(id), t.getHeading());
        myDraw.moveTurtle(myIcons.get(id), t.getNewLocation());
//        DisplayStats(Turtle t);


    }

    
    
    private void DisplayStats(Turtle t){
//    	t.get
    }
    
    private void updateBGColor (String RGB) {
        myPane.setStyle("-fx-background-color: rgb( " + RGB + ");");
    }

    private String toRGB (Color c) {
        double r = 255 * c.getRed();
        double g = 255 * c.getGreen();
        double b = 255 * c.getBlue();
        String R = "" + (int) r;
        String G = "" + (int) g;
        String B = "" + (int) b;
        return R + "," + G + "," + B;

    }

    public Set<Turtle> getTurtles () {
        return myTurtles;
    }

    public void toggleActive (boolean b) {
        mySelectorCheck = b;
        updateView(myModel);
    }
}

@@ -1419,75 +1419,8 @@
 del;
-%0A    private List%3CImageView%3E myStamps = new ArrayList%3CImageView%3E();
 %0A%0A  
@@ -3415,59 +3415,8 @@
 ));%0A
-            if (((int) t.getShowing()) == 1) %7B%0A    
     
@@ -3463,38 +3463,24 @@
 s.get(id));%0A
-            %7D%0A
         %7D el
@@ -3643,59 +3643,8 @@
 ));%0A
-            if (((int) t.getShowing()) == 1) %7B%0A    
     
@@ -3699,22 +3699,8 @@
 ));%0A
-            %7D%0A
     
@@ -3719,17 +3719,15 @@
   if
- 
 (((int)
- 
 t.ge
@@ -3743,15 +3743,12 @@
 s())
- 
 ==
- 
 1)
- 
 %7B%0A  
@@ -3931,107 +3931,8 @@
    %7D
-%0A%0A      %0A        System.out.println(t.getPenSize());%0A        myPane.getChildren().add(myDraw.path);
 %0A   
@@ -4054,125 +4054,15 @@
 ));%0A
-//        DisplayStats(Turtle t);%0A%0A%0A    %7D%0A%0A    %0A    %0A    private void DisplayStats(Turtle t)%7B%0A//    %09t.get%0A    %7D
 %0A    
+%7D%0A
 %0A   
@@ -4656,9 +4656,8 @@
 %0A    %7D%0A%7D
-%0A

fix error in viewpanemodule