package jatek0002;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;


import javax.swing.JPanel;

public class Grafika extends JPanel {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private int range=30;
    private int corr=7;
    private int thickness = 3;
    private double[][] drawLineA=null,drawLineB=null,equilibria=null;;
    private boolean allBlueIsRed=false,allGreenIsRed=false;
    private int equilibriaLength;
    
	public Grafika(){
		super();
		setPreferredSize(new Dimension(400,400));
        setBackground(Color.WHITE);
	}
	/**
	 * paint metódus felülírása. A kapott eredmények alapján grafikusan megjelenítjük
	 * az eredményt.
	 */
	@Override
	public void paint(Graphics g2) {
		Graphics2D g = (Graphics2D)g2;
        super.paint(g);    // paints background
        //init
        // do your drawing here
        // get width of panel.
        int h = getHeight();    // get its height.
        int w = getWidth();    // get its height.
        g.drawLine(0, h-range, w, h-range);
        g.drawLine(range, 0, range, h); 
        g.drawString("koordináta-rendszer A: Kék B: Zöld", 340, 20);
        g.drawString("0", range-corr, h-2*corr);
     
        for(int i=0;i<10;i++){
        	Double d = new Double(((double)i+1)/10);
        	g.drawLine(range+(i+1)*range, h-range, range+(i+1)*range, h-range-corr);
        	g.drawString(d.toString(), range-corr+(i+1)*range, h-2*corr);
        	g.drawLine(range, h-range-(i+1)*range, range+corr, h-range-(i+1)*range);
        	g.drawString(d.toString(), 10, h-range-(i+1)*range+corr);
		}
        
        //results
        allBlueIsRed=false;
        allGreenIsRed=false;
        
        g.setStroke(new BasicStroke(thickness));
        if(drawLineA!=null && drawLineB!=null){
        	g.setColor(Color.BLUE);
        	if(drawLineA[0][0]==drawLineA[drawLineA.length-1][0] && drawLineA[0][1]==drawLineA[drawLineA.length-1][1]){
        		g.fillRect(transform(0.0,"x"), transform(1.0,"y"), range*10, range*10);
        		allGreenIsRed=true;
        	}
        	g.setColor(Color.GREEN);
        	if(drawLineB[0][0]==drawLineB[drawLineB.length-1][0] && drawLineB[0][1]==drawLineB[drawLineB.length-1][1]){
        		g.fillRect(transform(0.0,"x"), transform(1.0,"y"), range*10, range*10);
        		allBlueIsRed=true;
        	}
        }
        if(drawLineA!=null){
        	if(allBlueIsRed){
        		g.setColor(Color.RED);
        	}else{
        		g.setColor(Color.BLUE);
        	}
        	for(int i=0;i<drawLineA.length-1;i++){        		
        		g.drawLine(transform(drawLineA[i][0],"x"), transform(drawLineA[i][1],"y"), transform(drawLineA[i+1][0],"x"), transform(drawLineA[i+1][1],"y"));
        	}
        }
        g.setStroke(new BasicStroke(thickness-1));
        if(drawLineB!=null){
        	if(allGreenIsRed){
        		g.setColor(Color.RED);
        	}else{
        		g.setColor(Color.GREEN);
        	}
        	for(int i=0;i<drawLineB.length-1;i++){
        		g.drawLine(transform(drawLineB[i][0],"x"), transform(drawLineB[i][1],"y"), transform(drawLineB[i+1][0],"x"), transform(drawLineB[i+1][1],"y"));
        	}
        }
        g.setStroke(new BasicStroke(thickness-2));
        g.setColor(Color.RED);

        if(equilibria!=null){
        	for(int i=0;i<=equilibriaLength;i++){
        		g.fillOval(transform(equilibria[i][0],"x")-corr/2, transform(equilibria[i][1],"y")-corr/2, corr,corr);
        	}
        	if(equilibriaLength==3){
        		g.fillRect(transform(0.0,"x"), transform(1.0,"y"), range*10, range*10);
        	}
        	if(equilibriaLength==2){
        		if(equilibria[0][0]==equilibria[1][0] || equilibria[0][1]==equilibria[1][1]){
        			g.drawLine(transform(equilibria[0][0],"x"), transform(equilibria[0][1],"y"), transform(equilibria[1][0],"x"), transform(equilibria[1][1],"y"));
        		}
        		if(equilibria[0][0]==equilibria[2][0] || equilibria[0][1]==equilibria[2][1]){
        			g.drawLine(transform(equilibria[0][0],"x"), transform(equilibria[0][1],"y"), transform(equilibria[2][0],"x"), transform(equilibria[2][1],"y"));
        		}
        		if(equilibria[1][0]==equilibria[2][0] || equilibria[1][1]==equilibria[2][1]){
        			g.drawLine(transform(equilibria[1][0],"x"), transform(equilibria[1][1],"y"), transform(equilibria[2][0],"x"), transform(equilibria[2][1],"y"));
        		}
        	}
        	if(equilibriaLength==1){
        		if(equilibria[0][0]==equilibria[1][0] || equilibria[0][1]==equilibria[1][1]){
        			g.drawLine(transform(equilibria[0][0],"x"), transform(equilibria[0][1],"y"), transform(equilibria[1][0],"x"), transform(equilibria[1][1],"y"));
        		}
        	}
        }
    }	
	
	/**
	 * Saját koordináta rendszerre transzformáljuk az eredménykkordinátákat.
	 * @param coord double A transzformálandó koord.
	 * @param s String "x" vagy "y" koord.
	 * @return newCoord int új koord
	 */
	private int transform(double coord,String s){
		int nullaX=range;
		int nullaY=getHeight()-range;
		
		int newCoord=0;
		if(s.endsWith("x")){
			newCoord= (int) ((int)nullaX+coord*10*range);
		}
		if(s.endsWith("y")){
			newCoord= (int) ((int)nullaY-coord*10*range);
		}
		return newCoord;
	}
	
	public void setRange(int range) {
		this.range = range;
	}

	public void setCorr(int corr) {
		this.corr = corr;
	}

	public void setDrawLineA(double[][] drawLineA) {
		this.drawLineA = drawLineA;
	}

	public void setDrawLineB(double[][] drawLineB) {
		this.drawLineB = drawLineB;
	}
	public double[][] getDrawLineA() {
		return drawLineA;
	}

	public double[][] getDrawLineB() {
		return drawLineB;
	}
	public double[][] getEquilibria() {
		return equilibria;
	}
	public void setEquilibria(double[][] equilibria) {
		this.equilibria = equilibria;
	}
	public int getEquilibriaLength() {
		return equilibriaLength;
	}
	public void setEquilibriaLength(int equilibriaLength) {
		this.equilibriaLength = equilibriaLength;
	}

	
	
}

