package lt.ktu.gmj.ui;

import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import lt.ktu.gmj.*;
import lt.ktu.gmj.propertySheet.*;
import lt.ktu.gmj.analysis.*;

public class ResultSheet extends PropertySheet implements ResultObserver{
    
    protected ResultRepository set;
    protected JLabel iteration;
    protected JLabel value;
    protected JLabel dimensions[];
    //AD corrections : 
    protected JLabel profit[];		//Laukeliai pelnui
    static final String myProfit[]={"1st Profit", "2nd Profit", "3rd Profit"};        
	double[] X; 
	double[] Y; 
    //---    
    
    public ResultSheet(){
        	set = null;         	
        	//Ad corrections: 
        	//Masyvai einamiems duomenims: serveriu kainoms ir galingumams talpinti
        	X = new double[6]; //[kiek dimensiju][kiek tasku] 
		Y = new double[6]; 
    }    
    public void logStarted(ResultRepository resultrepository){
        set = resultrepository;
        SwingUtilities.invokeLater(new Runnable(){
        		public void run(){
            		setup();
        		}
        });
    }
    public void logDisposed(ResultRepository resultrepository){
        resultrepository.removeObserver(this);
        SwingUtilities.invokeLater(new Runnable(){
        		public void run(){
            		removeAll();
        		}
        });
    }
    public void newResult(ResultRepository resultrepository){
        final Result r = resultrepository.lastResult();
        SwingUtilities.invokeLater(new Runnable(){
       			public void run(){
            		iteration.setText(String.valueOf(r.iteration));            		
        		}
       	});
    }
    public void newProgressResult(ResultRepository resultrepository){
        final Result r = resultrepository.lastResult();
        //AD modifications:
        final Elevation wd=set.task().wienerDomain();	//Paruosiama klase rezultatu perdavimui iskilumo testui	      
        //---
        SwingUtilities.invokeLater(new Runnable(){
        		public void run(){
		            value.setText(format(r.value));
		            //Ad corrections
		            int k=0;		            
		            //---		            
		            for(int i = 0; i < r.point.x.length; i++){
		                dimensions[i].setText(format(r.point.x[i]));		                
		                //Ad corrections		                               
		                X[i]=r.point.x[i]; 	//Issaugomi kainos ir galingumai
		                Y[i]=set.task().p(k); 	//Issaugomi pelnai           
		                if(i==1 || i==3 || i==5){                		
		                		profit[k].setText(format(set.task().p(k)));	//Pelnai isvedami i ekrana
		                		k++;
		                }		                
		            }			       
        		}        		
        });
        //AD:
        wd.setCoords(X,Y);	//Duomenys perduodami iskilumo testui
    }
   
    private String format(double d){
        String s = String.valueOf((double)Math.round(d * 1000D) / 1000D);
        if(s.length() > 7)s = s.substring(0, 7);
        return s;
    }
    private String format(int i){
        return String.valueOf(i);
    }    
    public void finalResult(ResultRepository resultrepository){}
    protected void setup(){
        removeAll();
        add(new Header("Property", "Value"));
        iteration = new JLabel("", 0);
        add(new SimpleProperty("Iteration", new ComponentPresenter(iteration)));
        value = new JLabel("", 0);
        add(new SimpleProperty("F(x)", new ComponentPresenter(value)));
        String as[] = set.task().domain().dimensions();
        dimensions = new JLabel[as.length];        
        //Ad corrections
        profit = new JLabel[myProfit.length];
        int k=0;        
        //---
        for(int i = 0; i < as.length; i++){
            dimensions[i] = new JLabel("", 0);
            add(new SimpleProperty(as[i], new ComponentPresenter(dimensions[i])));
            //Ad corrections:
            if(i==1 || i==3 || i==5){	//Prideda pelnu laukelius:
            		profit[k] = new JLabel("", 0);	//Uzpildo pelnu laukelius 0-iais
            		add(new SimpleProperty(myProfit[k],new ComponentPresenter(profit[k])));            		
            		k++;            		
            }
            //---
        }
        complete();
    }        
}

