package jatek0002Demo;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import org.w3c.dom.Document;
import jatek.Jatek;


public class Jatek0002Demo  implements Jatek,ActionListener{

	private Jatek0002DemoGameData jgd = new Jatek0002DemoGameData();
	private Jatek0002DemoPanel jk0002 = new Jatek0002DemoPanel();
	
	public Jatek0002Demo (){
		jk0002.getStart().addActionListener(this);
	}
	
	@Override
	public void closeGame() {
		jk0002.setPanel("input");
		jk0002.getGr().setDrawLineA(null);
		jk0002.getGr().setDrawLineB(null);
		jk0002.getGr().setEquilibria(null);
		jk0002.getGameResults().setText("Eredmények");
		jk0002.getJspA11().setValue(0);
		jk0002.getJspA12().setValue(0);
		jk0002.getJspA21().setValue(0);
		jk0002.getJspA22().setValue(0);
		jk0002.getJspB11().setValue(0);
		jk0002.getJspB12().setValue(0);
		jk0002.getJspB21().setValue(0);
		jk0002.getJspB22().setValue(0);
		jk0002.getGr().repaint();
		
	}

	@Override
	public boolean createSaveDataFormat(File save) {
		return true;
	}
	
	@Override
	public void loadData(Document d) {
		
	}
	/**
	 * Speciális eset, nem a loadData(Document d) metódust használjuk. 
	 * Oka, hogy egy demoról beszélünk, ami nem .xml állományokkal dolgozik.
	 */
	public void loadDemo() {
		Double[][] aData=new Double[2][2];
		Double[][] bData=new Double[2][2];
		aData[0][0]=new Double((Integer)jk0002.getJspA11().getValue());
		aData[0][1]=new Double((Integer)jk0002.getJspA12().getValue());
		aData[1][0]=new Double((Integer)jk0002.getJspA21().getValue());
		aData[1][1]=new Double((Integer)jk0002.getJspA22().getValue());
		
		bData[0][0]=new Double((Integer)jk0002.getJspB11().getValue());
		bData[0][1]=new Double((Integer)jk0002.getJspB12().getValue());
		bData[1][0]=new Double((Integer)jk0002.getJspB21().getValue());
		bData[1][1]=new Double((Integer)jk0002.getJspB22().getValue());
		
		jgd.setAdata(aData);
		jgd.setBdata(bData);
		
	}
	/**
	 * Lejátszuk a játékot, meghatározzuk az egyensúlyipontokat, és kirajzoltatjuk
	 * az eredményt.
	 */
	@Override
	public void playGame() {
		Double[][] data=jgd.getAdata();
		Double a = data[1][1]-data[0][1];
		Double alfa = data[0][0]-data[0][1]-data[1][0]+data[1][1];
		Double lambda=a/alfa;

		data=jgd.getBdata();
		Double b = data[1][1]-data[1][0];
		Double beta = data[0][0]-data[0][1]-data[1][0]+data[1][1];
		Double mu=b/beta;
				
		
		//feltételek
		if(alfa.compareTo(0.0)==0 && a.compareTo(0.0)==0){
			double[][] drawLineA={{0.0,0.0},{0.0,1.0},{1.0,1.0},{1.0,1.0},{1.0,0.0},{1.0,0.0},{0.0,0.0}};
			jk0002.getGr().setDrawLineA(drawLineA);
		}
		if(beta.compareTo(0.0)==0 && b.compareTo(0.0)==0){
			double[][] drawLineB={{0.0,0.0},{0.0,1.0},{1.0,1.0},{1.0,1.0},{1.0,0.0},{1.0,0.0},{0.0,0.0}};
			jk0002.getGr().setDrawLineB(drawLineB);
		}
		if(alfa.compareTo(0.0)==0 && a.compareTo(0.0)>0){
			double[][] drawLineA={{0.0,0.0},{0.0,1.0}};
			jk0002.getGr().setDrawLineA(drawLineA);
		}
		if(beta.compareTo(0.0)==0 && b.compareTo(0.0)>0){
			double[][] drawLineB={{0.0,0.0},{1.0,0.0}};
			jk0002.getGr().setDrawLineB(drawLineB);
		}
		if(alfa.compareTo(0.0)==0 && a.compareTo(0.0)<0){
			double[][] drawLineA={{1.0,0.0},{1.0,1.0}};
			jk0002.getGr().setDrawLineA(drawLineA);
		}
		if(beta.compareTo(0.0)==0 && b.compareTo(0.0)<0){
			double[][] drawLineB={{0.0,1.0},{1.0,1.0}};
			jk0002.getGr().setDrawLineB(drawLineB);
		}
		if(alfa.compareTo(0.0)!=0 && lambda==0){
			double[][] drawLineA={{0.0,0.0},{1.0,0.0},{1.0,0.0},{1.0,1.0}};
			jk0002.getGr().setDrawLineA(drawLineA);	
		}
		if(beta.compareTo(0.0)!=0 && mu==0){
			double[][] drawLineB={{0.0,0.0},{0.0,1.0},{0.0,1.0},{1.0,1.0}};
			jk0002.getGr().setDrawLineB(drawLineB);	
		}
		if(alfa.compareTo(0.0)>0 && lambda>0 && lambda<1){
			double[][] drawLineA={{0.0,0.0},{0.0,lambda},{0.0,lambda},{1.0,lambda},{1.0,lambda},{1.0,1.0}};
			jk0002.getGr().setDrawLineA(drawLineA);
		}
		if(beta.compareTo(0.0)>0 && mu>0 && mu<1){
			double[][] drawLineB={{0.0,0.0},{mu,0.0},{mu,0.0},{mu,1.0},{mu,1.0},{1.0,1.0}};
			jk0002.getGr().setDrawLineB(drawLineB);
		}
		if(alfa.compareTo(0.0)<0 && lambda>0 && lambda<1){
			//System.out.println(a+","+alfa+","+lambda);
			double[][] drawLineA={{1.0,0.0},{1.0,lambda},{1.0,lambda},{0.0,lambda},{0.0,lambda},{0.0,1.0}};
			jk0002.getGr().setDrawLineA(drawLineA);
		}
		if(beta.compareTo(0.0)<0 && mu>0 && mu<1){
			double[][] drawLineB={{1.0,0.0},{mu,0.0},{mu,0.0},{mu,1.0},{mu,1.0},{0.0,1.0}};
			jk0002.getGr().setDrawLineB(drawLineB);
		}
		if(alfa.compareTo(0.0)<0 && lambda<0){
			double[][] drawLineA={{0.0,0.0},{0.0,1.0}};
			jk0002.getGr().setDrawLineA(drawLineA);			
		}
		if(beta.compareTo(0.0)<0 && mu<0){
			double[][] drawLineB={{0.0,0.0},{1.0,0.0}};
			jk0002.getGr().setDrawLineB(drawLineB);			
		}
		if(alfa.compareTo(0.0)>0 && lambda<0){
			double[][] drawLineA={{1.0,0.0},{1.0,1.0}};
			jk0002.getGr().setDrawLineA(drawLineA);			
		}
		if(beta.compareTo(0.0)>0 && mu<0){
			double[][] drawLineB={{0.0,1.0},{1.0,1.0}};
			jk0002.getGr().setDrawLineB(drawLineB);			
		}
		
		if(alfa.compareTo(0.0)<0 && lambda==0){
			double[][] drawLineA={{0.0,1.0},{0.0,0.0},{0.0,0.0},{1.0,0.0}};
			jk0002.getGr().setDrawLineA(drawLineA);			
		}
		if(beta.compareTo(0.0)<0 && mu==0){
			double[][] drawLineB={{0.0,1.0},{0.0,0.0},{0.0,0.0},{1.0,0.0}};
			jk0002.getGr().setDrawLineB(drawLineB);			
		}
		if(alfa.compareTo(0.0)>0 && lambda==1){
			double[][] drawLineA={{0.0,0.0},{0.0,1.0},{0.0,1.0},{1.0,1.0}};
			jk0002.getGr().setDrawLineA(drawLineA);			
		}
		if(beta.compareTo(0.0)>0 && mu==1){
			double[][] drawLineB={{0.0,0.0},{1.0,0.0},{1.0,0.0},{1.0,1.0}};
			jk0002.getGr().setDrawLineB(drawLineB);			
		}
		if(alfa.compareTo(0.0)<0 && lambda==1){
			double[][] drawLineA={{0.0,1.0},{1.0,1.0},{1.0,1.0},{1.0,0.0}};
			jk0002.getGr().setDrawLineA(drawLineA);			
		}
		if(beta.compareTo(0.0)<0 && mu==1){
			double[][] drawLineB={{1.0,0.0},{1.0,1.0},{1.0,1.0},{0.0,1.0}};
			jk0002.getGr().setDrawLineB(drawLineB);			
		}
		if(alfa.compareTo(0.0)>0 && lambda>1){
			double[][] drawLineA={{0.0,0.0},{0.0,1.0}};
			jk0002.getGr().setDrawLineA(drawLineA);			
		}
		if(beta.compareTo(0.0)>0 && mu>1){
			double[][] drawLineB={{0.0,0.0},{1.0,0.0}};
			jk0002.getGr().setDrawLineB(drawLineB);			
		}
		if(alfa.compareTo(0.0)<0 && lambda>1){
			double[][] drawLineA={{1.0,0.0},{1.0,1.0}};
			jk0002.getGr().setDrawLineA(drawLineA);			
		}
		if(beta.compareTo(0.0)<0 && mu>1){
			double[][] drawLineB={{0.0,1.0},{1.0,1.0}};
			jk0002.getGr().setDrawLineB(drawLineB);			
		}
		
		
		
		//egyensúlypontok
		int k=-1;
		double[][] drawLineA=jk0002.getGr().getDrawLineA();
		double[][] drawLineB=jk0002.getGr().getDrawLineB();
		double[][] equilibria=new double[4][2];
		
		for(int i=0;i<drawLineA.length;i++){
			for(int j=0;j<drawLineB.length;j++){
				if(drawLineA[i][0]==drawLineB[j][0] && drawLineA[i][1]==drawLineB[j][1]){
					boolean isA=false;
					for(int l=0;l<=k;l++){
						if(equilibria[l][0]==drawLineA[i][0] && equilibria[l][1]==drawLineA[i][1]){
							isA=true;
							break;
						}
					}
					if(!isA){
						k++;
						equilibria[k][0]=drawLineA[i][0];
						equilibria[k][1]=drawLineA[i][1];
					}
				}
			}
		}
		
		if(lambda>0 && lambda<1 && mu>=0 && mu<=1){
			k++;
			equilibria[k][0]=mu;
			equilibria[k][1]=lambda;
		}else if(lambda>=0 && lambda<=1 && mu>0 && mu<1){
			k++;
			equilibria[k][0]=mu;
			equilibria[k][1]=lambda;
		}
		String points="{";
		for(int i=0;i<=k;i++){
			points+="{"+equilibria[i][0]+","+equilibria[i][1]+"}";
		}
		points+="}";
		jk0002.getGr().setEquilibriaLength(k);
		jk0002.getGr().setEquilibria(equilibria);
		jk0002.getGameResults().setText("Egyensúlypont: "+points);
		jk0002.getGr().repaint();
	}
	
	public Jatek0002DemoPanel getJk0002Demo() {
		return jk0002;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String ac = e.getActionCommand();
	      if(ac.equals("Számol")) {
	    	  loadDemo();
	    	  playGame();
	      }
		
	}

	
	
}
