//Trans_MultiGraphPanelB.java
//This is for frequency plots
import java.net.*;
import java.awt.*;
import java.awt.event.*;

public class Trans_MultiGraphPanelB extends Panel implements MouseListener, ItemListener, Runnable, AdjustmentListener{
    //private static final Color bgcolor = new Color(216,216,191);
    //private  static final Color bgcolor = new Color(161,220,220);
    private static final Color tinta = new Color(236,236,221);
    private static final Color tinta2 = new Color(236,236,221);
    private  static final Color bgcolor = new Color(255,255,255);
    
    private Scrollbar slider;  
    
    private static final Font titlefont = new Font("SanSerif",Font.BOLD,16);
    private static final Font labfont = new Font("SanSerif",Font.PLAIN,11);
    private Label titlelabel, selectlabel;   
    private String titulo;
    public Choice c1;
    public Thread tron;
    private boolean IsTronRunning;
    public boolean ThreadStarted;
    public boolean  button_one_ON = true;
    private int SleepTime = 50;
    private Trans_State t_state;
    public TransGraphCanvas tgc1;
    public TransGraphCanvas2 tgc3;
    StartStopClock ssc;
    
    public int DynamicRunType = 1;
    public CheckboxGroup cbxgroup;
    public Checkbox cbx1, cbx2;
    private int SCROLLMIN=0, SCROLLMAX=251;
   
    public Trans_MultiGraphPanelB(String titulo){
	super();

	this.titulo=titulo;
	setLayout(null);
	setBackground(bgcolor);
        
	//t_state = new Trans_State();
	 
	c1 = new Choice();
        c1.addItem(" Select a Plot");
	c1.addItem(" Time Dependent Voltage");
	c1.addItem(" Time Dependent Current");
	c1.addItem(" Time Dependent Power");
	
	c1.setBackground(bgcolor);
	c1.setFont(new Font("SanSerif",Font.PLAIN,11));
	titlelabel = new Label(titulo,Label.LEFT);
	titlelabel.setFont(titlefont);
        titlelabel.setForeground(Color.black);
	
	selectlabel = new Label("click to select plot",Label.CENTER);
	selectlabel.setFont(labfont);
	selectlabel.setBackground(bgcolor);
	selectlabel.setForeground(Color.blue);
	
	//add(selectlabel);
	selectlabel.setBounds(200,30,100,25);
	
	tgc3 = new TransGraphCanvas2();
	ssc  = new StartStopClock();
	
	//Checkboxes
	cbxgroup = new CheckboxGroup();
	cbx1 = new Checkbox("Trace On",cbxgroup,false);
	cbx2 = new Checkbox("Trace Off",cbxgroup,true);
	cbx1.setBackground(tinta);
	cbx2.setBackground(tinta);
        
        slider = new Scrollbar(Scrollbar.HORIZONTAL,SCROLLMAX-SleepTime,1,SCROLLMIN,SCROLLMAX);
        add(slider);
        slider.setBackground(Color.white);
        //slider.setBounds(216,261,85,8);
	slider.setBounds(536,26,125,15);
        Panel ps = new Panel();
	    ps.setBackground(Color.black);
	    ps.setBounds(535,25,127,17);
            add(ps);
	    
        add(titlelabel);
	add(c1);
	add(tgc3);
	add(ssc);
	add(cbx1);
	add(cbx2);
	//titlelabel.setBounds(5,5,300,25);
	titlelabel.setBounds(5,5,200,25);
	c1.setBounds(5,30,195,25);
	tgc3.setBounds(5,66,795,314);
	ssc.setBounds(215,4,198,58);
	// CHANGE #1 9/25/2017 ===============================
        //cbx1.setBounds(435,17,75,15);
	//cbx2.setBounds(435,33,75,15);
        cbx1.setBounds(425,12,95,20);
	cbx2.setBounds(425,33,95,20);
        //=====================================================
	
	Panel pgraph1 = new Panel();
	    pgraph1.setBackground(tinta2);
	    //pgraph1.setBackground(bgcolor);
	    add(pgraph1);
	    pgraph1.setBounds(5,66,795,316);
	
	Panel pgraph2 = new Panel();
	    pgraph2.setBackground(Color.black);
	    add(pgraph2);
	    pgraph2.setBounds(4,65,797,318);
	
	Panel pgraph3 = new Panel();
	    pgraph3.setBackground(Color.black);
	    //add(pgraph3);
	    pgraph3.setBounds(4,220,200,60);
        
        Panel pgraph4a = new Panel();
	    pgraph4a.setBackground(tinta);
	    add(pgraph4a);
            // CHANGE #2 9/25/2017 ==============================
	    //pgraph4a.setBounds(430,16,85,32);
            pgraph4a.setBounds(425,11,95,42);
            //===================================================
            
	Panel pgraph4 = new Panel();
	    pgraph4.setBackground(Color.black);
	    add(pgraph4);
	    // CHANGE #3 9/25/2017 ==============================
            //pgraph4.setBounds(429,15,87,34);
	    pgraph4.setBounds(424,10,97,44);
            //===================================================
	IsTronRunning = false;
        ThreadStarted = false;
	
	//Listeners
	ssc.button1.addMouseListener(this);
	ssc.button2.addMouseListener(this);
	cbx1.addItemListener(this);
	cbx2.addItemListener(this);
        slider.addAdjustmentListener(this);	
    }			
    
    public void paint(Graphics g){
	    
	    g.setFont(labfont);
            g.setColor(Color.blue);
            g.drawString("animation speed",560,22);
	    g.setColor(Color.blue.darker());
            MaestroG.drawArrowtip(545,47,4,g);
	    MaestroG.drawArrowtip(553,47,4,g);
	    
	    g.setColor(Color.red.darker());
	    MaestroG.drawArrowtip(642,47,3,g);
	    MaestroG.drawArrowtip(650,47,3,g);            
	}

    
    public void mouseClicked(MouseEvent evt){mouseHandler(evt);}
    public void mouseEntered(MouseEvent evt){;}
    public void mouseExited(MouseEvent evt){;}
    public void mousePressed(MouseEvent evt){;}
    public void mouseReleased(MouseEvent evt){;}
    
    private synchronized void mouseHandler(MouseEvent evt){
	if(evt.getSource() == ssc.button1){
	    IsTronRunning = true;
	    
            if(!ThreadStarted){
                start();
                ThreadStarted = true;
                ssc.button1.setEnabled(false);
                ssc.button2.setEnabled(true);
                
            }
            else{
                ssc.button1.setEnabled(false);
                ssc.button2.setEnabled(true);
                
                notify();
            }
            //c1.setEnabled(false);
            button_one_ON = false;
	}
	if(evt.getSource() == ssc.button2){
	    IsTronRunning = false;
               
            ssc.button2.setEnabled(false);
            ssc.button1.setEnabled(true);
            button_one_ON = true;
	}
    }
    
    public void itemStateChanged(ItemEvent evt){
	if(evt.getSource() == cbx1){
	    tgc3.setTrace(true);
	}
	else if(evt.getSource() == cbx2){
	    tgc3.setTrace(false);
	}
    }
    
    public void adjustmentValueChanged(AdjustmentEvent evt){
	  if(evt.getSource()==slider){
		SCROLLMAX = slider.getMaximum();
		SCROLLMIN = slider.getMinimum();
                SleepTime = (SCROLLMAX-SCROLLMIN)-slider.getValue()-1;  
	  }
	}
	
	
    public synchronized void plotTimeDependentVoltage(Trans_State state){
	//if(tron != null) tron.suspend();
	t_state = state;
	cbx2.setState(true);
	t_state.reset();
	ssc.clockcanvas.setStatus(0,state.NTime,state.dtime);
	ssc.clockcanvas.reset();
	DynamicRunType = 1;
	tgc3.reset();
	tgc3.setDynamics(false);
	t_state.ScanTimeDependentVoltage();
	String labella;
	/*
        if(!state.stub[0].isEnable() && !state.stub[1].isEnable() && !state.stub[2].isEnable()){
	    labella = "2 |V+|";
	}
	else{
	    labella = "3 |V+|";
	}
         */
	//add(labella);
	//labella.setBounds(250,5,50,25);
        
        //labella =""+state.PlotTimeScale+" |V+|";
        //if(state.PlotTimeScale==1){labella ="  |V+|";};
	labella ="";
        
        
	if (state.getYRangeMax(1)>=1.0 && state.getYRangeMax(1)<1.0E3 || state.getYRangeMax(1)==0.0){
	    tgc3.setTitle("   V(d,t) [ V ]                                       "+labella);    
	}
	else if(state.getYRangeMax(1)<1.0E6 && state.getYRangeMax(1)>=1.0E3  ){
	    tgc3.setTitle("   V(d,t) [ k V ]                                     "+labella); 
	   
	}
	else if(state.getYRangeMax(1)>=1.0E6  ){
	    tgc3.setTitle("   V(d,t) [ M V ]                                     "+labella); 
	   
	}
	else if(state.getYRangeMax(1)<1.0 && state.getYRangeMax(1)>=1.0E-3  ){
	    tgc3.setTitle("   V(d,t) [ m V ]                                     "+labella); 
	   
	}
	else if(state.getYRangeMax(1)<1.0E-3 && state.getYRangeMax(1)>=1.0E-6  ){
	    tgc3.setTitle("   V(d,t) [ \u00b5 V ]                                     "+labella); 
	    
	}
	else if(state.getYRangeMax(1)<1.0E-6 && state.getYRangeMax(1)>=1.0E-9  ){
	    tgc3.setTitle("   V(d,t) [ n V ]                                     "+labella); 
	    
	}
	else if(state.getYRangeMax(1)<1.0E-9 && state.getYRangeMax(1)>=1.0E-12  ){
	    tgc3.setTitle("   V(d,t) [ p V ]                                     "+labella); 
	    
	}
	else if(state.getYRangeMax(1)<1.0E-12 && state.getYRangeMax(1)>=1.0E-15 ){
	    tgc3.setTitle("   V(d,t) [ f V ]                                     "+labella); 
	   
	}
	else if(state.getYRangeMax(1)<1.0E-15 ){
	    tgc3.setTitle("   V(d,t) [ a V ]                                     "+labella); 
	   
	}
	//tgc3.setTitle("   V (d,t)  [ V ]                                          2 |V+|");
	tgc3.setYRangeMax(t_state.getYRangeMax(1));
	tgc3.setYRangeMin(-t_state.getYRangeMax(1));
	tgc3.plot(t_state.x, t_state.y1, t_state.yV1, t_state.yV2);
	tgc3.cleanUp();
    } 
    
    public synchronized void plotTimeDependentCurrent(Trans_State state){
	//if(tron != null) tron.suspend();
	t_state = state;
	cbx2.setState(true);
	t_state.reset();
	ssc.clockcanvas.setStatus(0,t_state.NTime,t_state.dtime);
	ssc.clockcanvas.reset();
	DynamicRunType = 2;
	tgc3.reset();
	tgc3.setDynamics(false);
	t_state.ScanTimeDependentCurrent();
	
	String labella;
	/*
        if(!state.stub[0].isEnable() && !state.stub[1].isEnable() && !state.stub[2].isEnable()){
	    labella = "2 |V+|/ Zo";
	}
	else{
	    labella = "3 |V+|/ Zo";
	}
        */
        //labella =""+state.PlotTimeScale+" |V+|/ Zo";
        //if(state.PlotTimeScale==1){labella ="  |V+|/ Zo";};
	labella ="";
        
	if (state.getYRangeMax(2)>=1.0 && state.getYRangeMax(2)<1.0E3 || state.getYRangeMax(2)==0.0){
	    tgc3.setTitle("   I (d,t) [ A ]                                   "+labella);    
	}
	else if(state.getYRangeMax(2)<1.0E6 && state.getYRangeMax(2)>=1.0E3  ){
	    tgc3.setTitle("   I (d,t) [ k A ]                                 "+labella); 
	   
	}
	else if(state.getYRangeMax(2)>=1.0E6 ){
	    tgc3.setTitle("   I (d,t) [ M A ]                                 "+labella); 
	   
	}
	else if(state.getYRangeMax(2)<1.0 && state.getYRangeMax(2)>=1.0E-3  ){
	    tgc3.setTitle("   I (d,t) [ m A ]                                 "+labella); 
	   
	}
	else if(state.getYRangeMax(2)<1.0E-3 && state.getYRangeMax(2)>=1.0E-6  ){
	    tgc3.setTitle("   I (d,t) [ \u00b5 A ]                                 "+labella); 
	    
	}
	else if(state.getYRangeMax(2)<1.0E-6 && state.getYRangeMax(2)>=1.0E-9  ){
	    tgc3.setTitle("   I (d,t) [ n A ]                                 "+labella); 
	    
	}
	else if(state.getYRangeMax(2)<1.0E-9 && state.getYRangeMax(2)>=1.0E-12  ){
	    tgc3.setTitle("   I (d,t) [ p A ]                                 "+labella); 
	    
	}
	else if(state.getYRangeMax(2)<1.0E-12 && state.getYRangeMax(2)>=1.0E-15  ){
	    tgc3.setTitle("   I (d,t) [ f A ]                                 "+labella); 
	   
	}
	else if(state.getYRangeMax(2)<1.0E-15  ){
	    tgc3.setTitle("   I (d,t) [ a A ]                                 "+labella); 
	   
	}
	//tgc3.setTitle("   I(d,t)  [ A ]                                        2 |V+| / Zo");
	tgc3.setYRangeMax(t_state.getYRangeMax(2));
	tgc3.setYRangeMin(-t_state.getYRangeMax(2));
	tgc3.plot(t_state.x,t_state.y1, t_state.yV1, t_state.yV2);
	tgc3.cleanUp();
    }	
    
    public synchronized void plotTimeDependentPower(Trans_State state){
	//if(tron != null) tron.suspend();
	t_state = state;
	cbx2.setState(true);
	t_state.reset();
	ssc.clockcanvas.setStatus(0,t_state.NTime,t_state.dtime);
	ssc.clockcanvas.reset();
	DynamicRunType = 3;
	tgc3.reset();
	tgc3.setDynamics(false);
	t_state.ScanTimeDependentPower();
	
	String labella;
	/*
        if(!state.stub[0].isEnable() && !state.stub[1].isEnable() && !state.stub[2].isEnable()){
	    labella = "2 |V+|\u00b2/ Zo";
	}
	else{
	    labella = "3 |V+|\u00b2/ Zo";
	}
	*/
        
        //labella =""+state.PlotTimeScale+" |V+|\u00b2/ Zo";
        //if(state.PlotTimeScale==1){labella ="  |V+|\u00b2/ Zo";};
        labella ="";
                
	if (state.getYRangeMax(3)>=1.0 && state.getYRangeMax(3)<1.0E3 || state.getYRangeMax(3)==0.0){
	    tgc3.setTitle("   P(d,t)  [ W ]                                "+labella);    
	}
	else if(state.getYRangeMax(3)<1.0E6 && state.getYRangeMax(3)>=1.0E3  ){
	    tgc3.setTitle("   P(d,t)  [ k W ]                                "+labella); 
	   
	}
	else if(state.getYRangeMax(3)>=1.0E6  ){
	    tgc3.setTitle("   P(d,t)  [ M W ]                                "+labella); 
	   
	}
	else if(state.getYRangeMax(3)<1.0 && state.getYRangeMax(3)>=1.0E-3  ){
	    tgc3.setTitle("   P(d,t)  [ m W ]                                "+labella); 
	   
	}
	else if(state.getYRangeMax(3)<1.0E-3 && state.getYRangeMax(3)>=1.0E-6  ){
	    tgc3.setTitle("   P(d,t)  [ \u00b5 W ]                                "+labella); 
	    
	}
	else if(state.getYRangeMax(3)<1.0E-6 && state.getYRangeMax(3)>=1.0E-9  ){
	    tgc3.setTitle("   P(d,t)  [ n W ]                                "+labella); 
	    
	}
	else if(state.getYRangeMax(3)<1.0E-9 && state.getYRangeMax(3)>=1.0E-12  ){
	    tgc3.setTitle("   P(d,t)  [ p W ]                                "+labella); 
	    
	}
	else if(state.getYRangeMax(3)<1.0E-12 && state.getYRangeMax(3)>=1.0E-15){
	    tgc3.setTitle("   P(d,t)  [ f W ]                                "+labella); 
	   
	}
	else if(state.getYRangeMax(3)<1.0E-15){
	    tgc3.setTitle("   P(d,t)  [ a W ]                                "+labella); 
	   
	}
	//tgc3.setTitle("   P(d,t)  [ W ]                                     2 |V+|\u00b2/ Zo");
	tgc3.setYRangeMax(t_state.getYRangeMax(3));
	tgc3.setYRangeMin(-t_state.getYRangeMax(3));
	
	tgc3.plot(t_state.x,t_state.y1, t_state.yV1, t_state.yV2);
	tgc3.cleanUp();
    }
    
    public void start(){
	if(tron == null){
	    tron = new Thread(this);
	    tron.start();
	}
    }
    
    //public void stop(){
	//if(tron != null) {
	  //  tron.stop();
	  //  tron = null;
	//}
    //}
    
    public void run(){
	while(true){
	    if(IsTronRunning){
		plotDynamicRun();
		ssc.clockcanvas.increment();
		t_state.increment();
	    }
	    try{
		Thread.sleep(SleepTime);
	    }
	    catch(InterruptedException e){e.printStackTrace();}
	}
    }
    
    public synchronized void plotDynamicRun(){
	switch(DynamicRunType){
	    case 5:
		t_state.ScanRandom();
		tgc3.setTitle("Test");
		tgc3.reset();
		tgc3.setDynamics(true);
		tgc3.plot(t_state.x,t_state.y1, t_state.yV1, t_state.yV2);
	    break;
		
	    case 1:
		t_state.ScanTimeDependentVoltage();
		tgc3.plot(t_state.x,t_state.y1, t_state.yV1, t_state.yV2);
		tgc3.setDynamics(true);
	    break;
	    
	    case 2:
		t_state.ScanTimeDependentCurrent();
		tgc3.setDynamics(true);
		tgc3.plot(t_state.x,t_state.y1, t_state.yV1, t_state.yV2);
	    break;
	    
	    case 3:
		t_state.ScanTimeDependentPower();
		tgc3.setDynamics(true);
		tgc3.plot(t_state.x,t_state.y1, t_state.yV1, t_state.yV2);
	    break; 
	}
    }
}

