//Trans_MultiGraphPanelC.java
//This is for time-dependent plots

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class Trans_MultiGraphPanelC extends Panel implements MouseListener, ItemListener, AdjustmentListener, Runnable{
    
    private static final Color tinta = new Color(236,236,221);
    private static final Color tinta2 = new Color(216,216,191);
    private static final Color bgcolor = new Color(240,240,240); 
    
    private Font titlefont = new Font("SanSerif",Font.BOLD,16);
    private Font titlefont2 = new Font("SanSerif",Font.PLAIN,16);
    private Font titlefont3 = new Font("SanSerif",Font.ITALIC,16);
    private Font stepfont1 = new Font("Serif",Font.BOLD,15);
    private Font stepfont2 = new Font("SanSerif",Font.PLAIN,13);
    private Font omegafont = new Font("SanSerif",Font.BOLD,16);
        
    private Font labfont = new Font("SanSerif",Font.PLAIN,11);
    private int ishift = 0;
    private Font labfont2 = new Font("SanSerif",Font.BOLD,11);
    private Font labfont3 = new Font("SanSerif",Font.PLAIN,16);
    
    public Button about;
   
    public Label titulolabel, fast, slow, text1, text2, text3, watch1, watch2, watch3, step1, step2;   
    private String titulo;
    //public Choice c1;
    private Thread tron;
    private boolean IsTronRunning; 
    private int ydown = 2;
    private double temp;
    private int SleepTime = 50;
    private int ScrollMax = 500;
    public int ScrollMaxZoom = 10;
    
    private int ScrollMax2 = 4057;
    public Scrollbar slider1, sliderzoom;
    public Trans_State t_state;
    private Trans_State state;
    private DrawCurrent drawC;
    public Panel ps3, ps4;
    
    StartStopClock ssc;
    private int DynamicRunType = 1;
    
        
    public Trans_MultiGraphPanelC(String titulo, Trans_State state, DrawCurrent drawC){
	super();
	this.titulo=titulo;
        this.state = state;
        this.drawC = drawC;
        
	setLayout(null);
	setBackground(bgcolor);
        
        titlefont = new Font("SanSerif",Font.BOLD,state.font16);
        titlefont2 = new Font("SanSerif",Font.PLAIN,state.font16);
        titlefont3 = new Font("SanSerif",Font.ITALIC,state.font16);
        stepfont1 = new Font("Serif",Font.BOLD,state.font15);
        stepfont2 = new Font("SanSerif",Font.PLAIN,state.font13);
        omegafont = new Font("SanSerif",Font.BOLD,state.font16);
        
        labfont = new Font("SanSerif",Font.PLAIN,state.font11);
        labfont2 = new Font("SanSerif",Font.BOLD,state.font11);
        labfont3 = new Font("SanSerif",Font.PLAIN,state.font16);
        
        // button to reach informational page
        about = new Button("Instructions");
        //about.setBackground(new Color(240,240,255));
	//add(about);
	about.setBounds(state.s300,getSize().height-state.s30,state.s80,state.s20);	
        
        Panel ps7 = new Panel();
	    ps7.setBackground(Color.lightGray);
	    //add(ps7);
	    ps7.setBounds(state.s800+state.s19,state.s24+ydown,state.s72,state.s25);
	    
	Panel ps8 = new Panel();
	    ps8.setBackground(Color.black);
	    //add(ps8);
	    ps8.setBounds(state.s800+state.s18,state.s23+ydown,state.s74,state.s27);
		 
	    //c1 = new Choice();
	    //c1.addItem("");
	
	titulolabel = new Label("Switching Transient",Label.CENTER);
	titulolabel.setFont(titlefont);
	
	fast = new Label("faster",Label.LEFT);
        fast.setForeground(Color.red.darker());
	slow = new Label("slower",Label.RIGHT);
	slow.setForeground(Color.blue.darker());
        fast.setFont(labfont);
	slow.setFont(labfont);
        
        step1 = new Label("\u0394\u03c9t",Label.LEFT);
        step1.setForeground(Color.blue.darker());
	step1.setFont(stepfont1);
        
        step2 = new Label("\u0394\u03c9t  step = ",Label.RIGHT);
        step2.setForeground(Color.blue.darker());
        step2.setFont(stepfont2);
        
        text1 = new Label(" ",Label.LEFT);
        text1.setForeground(Color.red.darker());
	text1.setFont(labfont);
	
        text2 = new Label(" ",Label.LEFT);
        text2.setForeground(Color.red.darker());
	text2.setFont(labfont);
	
        text3 = new Label(" ",Label.LEFT);
        text3.setForeground(Color.red.darker());
	text3.setFont(labfont);
	//add(text1);  add(text2);  add(text3);
        
        text1.setBounds(state.s10,state.s133,state.s200+state.s20,state.s16);
        text2.setBounds(state.s10,state.s149,state.s200+state.s20,state.s16);
        text3.setBounds(state.s10,state.s165,state.s200+state.s20,state.s16);
        
	watch1 = new Label("0",Label.LEFT);
	watch2 = new Label("\u03c9t = ",Label.RIGHT);
	watch1.setFont(titlefont);
	watch2.setFont(omegafont);
	
	watch1.setForeground(Color.red.darker());
	watch2.setForeground(Color.red.darker());
	
        watch1.setBackground(Color.white);
	watch2.setBackground(Color.white);
	
        ssc  = new StartStopClock(state);
	
	slider1 = new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,ScrollMax);//changes sleep time
	slider1.setValue(ScrollMax-SleepTime);
        slider1.setBackground(new Color(255,255,250));
	add(slider1);
	slider1.setBounds(state.s61,state.s9+ydown,state.s160,state.s15);

	Panel ps1 = new Panel();
	    ps1.setBackground(Color.cyan);
	    add(ps1);
	    ps1.setBounds(state.s61-1,state.s9+ydown-1,state.s160+2,state.s15+2);

        Panel ps2 = new Panel();
	    ps2.setBackground(Color.black);
	    add(ps2);
	    ps2.setBounds(state.s61-2,state.s9+ydown-2,state.s160+4,state.s15+4);
            
        sliderzoom = new Scrollbar(Scrollbar.HORIZONTAL,0,1,1,ScrollMaxZoom);//changes sleep time
	sliderzoom.setValue(2);
        sliderzoom.setBackground(new Color(255,255,250));
	add(sliderzoom);
	    
        add(fast);
	add(slow);
        add(step2);
	add(watch1);
	add(watch2);
	add(ssc);
	
        int shiftx = state.s10;
        slow.setBounds(state.s15,state.s9+ydown,state.s40,state.s12);
	fast.setBounds(state.s200+state.s27,state.s9+ydown,state.s40,state.s12);
        step1.setBounds(state.s155,state.s106,state.s70,state.s14);
        step2.setBounds(state.s145,state.s100,state.s80,state.s14);
        
	// CHANGE ==============================================================
	watch1.setBounds(state.s58+shiftx,state.s112+ydown,state.s45,state.s18);
        watch2.setBounds(state.s10+shiftx,state.s112+ydown,state.s48,state.s18);
        
        sliderzoom.setBounds(state.s125+shiftx,state.s117+ydown,state.s110,state.s15);
        Panel pszoom = new Panel();
	    pszoom.setBackground(Color.cyan);
	    add(pszoom);
	    pszoom.setBounds(state.s125+shiftx-1,state.s117+ydown-1,state.s110+2,state.s15+2);
	Panel ps2zoom = new Panel();
	    ps2zoom.setBackground(Color.black);
	    add(ps2zoom);
	    ps2zoom.setBounds(state.s125+shiftx-2,state.s117+ydown-2,state.s110+4,state.s15+4);	
	// frame of "wt =  "
        Panel pwatch2 = new Panel();
	    pwatch2.setBackground(Color.black);
	    add(pwatch2);
	    pwatch2.setBounds(state.s10+shiftx-1,state.s112+ydown-1,state.s93+2,state.s18+2);
	Panel pwatch1b = new Panel();
	    pwatch1b.setBackground(Color.white);
	    add(pwatch1b);
	    pwatch1b.setBounds(state.s10+shiftx-2,state.s112+ydown-2,state.s93+4,state.s18+4);
	Panel pwatch2b = new Panel();
	    pwatch2b.setBackground(Color.black);
	    add(pwatch2b);
	    pwatch2b.setBounds(state.s10+shiftx-3,state.s112+ydown-3,state.s93+6,state.s18+6);
	
        ssc.setBounds(state.s10,state.s35+ydown,state.s200+state.s54,state.s58);
        Panel pssc = new Panel();
	    pssc.setBackground(Color.black);
	    add(pssc);
	    pssc.setBounds(state.s10-1,state.s35+ydown-1,state.s200+state.s54+2,state.s58+2);
	Panel pssc2 = new Panel();
	    pssc2.setBackground(Color.cyan);
	    add(pssc2);
	    pssc2.setBounds(state.s10-2,state.s35+ydown-2,state.s200+state.s54+4,state.s58+4);	
        Panel pssc3 = new Panel();
	    pssc3.setBackground(Color.black);
	    add(pssc3);
	    pssc3.setBounds(state.s10-3,state.s35+ydown-3,state.s200+state.s54+6,state.s58+6);	
	
	IsTronRunning = false;
	
    //Listeners
	ssc.button1.addMouseListener(this);
	ssc.button2.addMouseListener(this);
	ssc.button3.addMouseListener(this);
	
	slider1.addAdjustmentListener(this);
        sliderzoom.addAdjustmentListener(this);
    }
    
    public void paint(Graphics g){
        //g.clearRect(0,0,getSize().width-1,getSize().height-1);
		
		//Draw the background	
		g.setColor(Color.black);
		g.drawLine(0,getSize().height-1,getSize().width-1,getSize().height-1);
		g.drawLine(getSize().width-1,0,getSize().width-1,getSize().height-1);
		
		g.drawLine(0,0,getSize().width-1,0);
		g.drawLine(0,0,0,getSize().height-1);
        
        FontMetrics fm;
	    
        int x, y, dx, dxx, dy, dyy;
        Font normalfont = new Font("SanSerif",Font.PLAIN,state.font12);
	Font symbolfont = new Font("Symbol",Font.PLAIN,state.font12);
	Font subfont = new Font("SanSerif",Font.PLAIN,state.font10);
	Font subfont2 = new Font("SanSerif",Font.PLAIN,state.font11);
	
        x = state.s10;
	y = state.s10;
	dx = state.s105;
	dxx = state.s15;
	            
	g.setFont(labfont);
        g.setColor(Color.blue.darker());
        
        g.setColor(Color.red.darker());
        if(sliderzoom.getValue()<7){
            MaestroG.Custom("","",""+sliderzoom.getValue()+"\u00b0","",g,state.font13,state.s200+state.s30,state.s112);
        }
        else{
            MaestroG.Custom("","",""+(sliderzoom.getValue()+1)+"\u00b0","",g,state.font13,state.s200+state.s30,state.s112);
        }
    }
    
    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){;}
    
    public synchronized void mouseHandler(MouseEvent evt){
	if(evt.getSource() == ssc.button1){
	    ssc.button2.setEnabled(true);
	    IsTronRunning = true;
	    if(tron == null){
		start();
	    }
		//t_state.ignition();
                //tgc3.cleanUp();
	}
	if(evt.getSource() == ssc.button2){
	    IsTronRunning = false;
	    //tgc3.repaint();
	    
	}
	if(evt.getSource() == ssc.button3){
	    IsTronRunning = false;
	    ssc.clockcanvas.setStatus(0,t_state.NTime,t_state.dtime);
	    ssc.clockcanvas.reset();
	    t_state.ctime=0;
	    //t_state.ignition();
	    //tgc3.plot(t_state.xnew,t_state.ycleannew,t_state.VPRINT);
	    //tgc3.repaint();
	    watch1.setText("0\u00b0");
	    
	    ssc.button2.setEnabled(false);   
    	}
    }
    
    public void itemStateChanged(ItemEvent evt){
	
    }
    
    public synchronized void plotTransientVoltage(Trans_State state){
	//if(tron != null) tron.suspend();
	t_state = state;
	
	//t_state.ignition();
	//t_state.reset();
	
	ssc.clockcanvas.setStatus(0,state.NTime,state.dtime);
	ssc.clockcanvas.reset();
	DynamicRunType = 1;
	//tgc3.reset();
	//tgc3.setDynamics(false);
	t_state.ScanTransientVoltage();
		
	
    } 
    
    public synchronized void plotTransientCurrent(Trans_State state){
	//if(tron != null) tron.suspend();
	t_state = state;
	
	//t_state.ignition();
	//t_state.reset();
	
	ssc.clockcanvas.setStatus(0,state.NTime,state.dtime);
	ssc.clockcanvas.reset();
	DynamicRunType = 1;
	//tgc3.reset();
	//tgc3.setDynamics(false);
	t_state.ScanTransientCurrent();
		
	
    }	
    
    public synchronized void plotTransientPower(Trans_State state){
	
    }
    
    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.decrement();
		t_state.increment();
	    }
	    try{
		Thread.sleep(SleepTime+50);
	    }
	    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.xnew,t_state.y1new,t_state.VPRINT);
	    break;
		
	    case 1:
		if(t_state.PlotPsiSquare){
                    t_state.ScanTransientVoltage();
                    //tgc3.setProbability(t_state.SUM_LEFT, t_state.SUM_BARRIER, t_state.SUM_RIGHT);
                    //tgc3.plot(t_state.xnew,t_state.y1new,t_state.VPRINT);
                }
                else{
                    t_state.ScanTransientCurrent();
                    //tgc3.setProbability(t_state.SUM_LEFT, t_state.SUM_BARRIER, t_state.SUM_RIGHT);
                    if(t_state.PlotPsiReal){
                        //tgc3.plot(t_state.xnew,t_state.PSInewReal,t_state.VPRINT);
                    }
                    else{
                        //tgc3.plot(t_state.xnew,t_state.PSInewImaginary,t_state.VPRINT);
                    }
                    
                }
                watch1.setText(""+(t_state.ctime)+"\u00b0");
		temp = (double)((t_state.ctime)*t_state.DT);
                drawC.repaint();
                repaint();
	    break;
	    
	}
    }
    
    public synchronized void setSleepTime(int SleepTime){
	this.SleepTime = SleepTime;
    }
    
    public void adjustmentValueChanged(AdjustmentEvent evt){//NEED HERE
	
	if(evt.getSource()==slider1){
	    SleepTime  = ScrollMax-slider1.getValue();  
	}
        if(evt.getSource()==sliderzoom){
	    repaint();
	}
    }
    
}
