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

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
//import java.lang.*;  
import java.awt.image.*;
import java.net.*;
import java.awt.geom.*;



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 static final Font titlefont = new Font("SanSerif",Font.BOLD,16);
    private static final Font titlefont2 = new Font("SanSerif",Font.PLAIN,16);
    private static final Font titlefont3 = new Font("SanSerif",Font.ITALIC,16);
    private static final Font stepfont1 = new Font("Serif",Font.BOLD,15);
    private static final Font stepfont2 = new Font("SanSerif",Font.PLAIN,13);
    private static final Font omegafont = new Font("SanSerif",Font.BOLD,16);
        
    private static final Font labfont = new Font("SanSerif",Font.PLAIN,11);
    private int ishift = 0;
    private static final Font labfont2 = new Font("SanSerif",Font.BOLD,11);
    private static final 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);
        
        
        // button to reach informational page
        
        about = new Button("Instructions");
        //about.setBackground(new Color(240,240,255));
	//add(about);
	about.setBounds(300,getSize().height-30,80,20);	
        
        Panel ps7 = new Panel();
	    ps7.setBackground(Color.lightGray);
	    //add(ps7);
	    ps7.setBounds(819,24+ydown,72,25);
	    
	Panel ps8 = new Panel();
	    ps8.setBackground(Color.black);
	    //add(ps8);
	    ps8.setBounds(818,23+ydown,74,27);
	
		 
	c1 = new Choice();
	c1.addItem("  Wave function probability | \u03c8 | \u00b2");
	c1.addItem("  Wave function amplitude Re{ \u03c8 }");
	c1.addItem("  Wave function amplitude Im{ \u03c8 }");
        
        //c1.addItem("Transient Power");
	
	c1.setBackground(Color.white);
	c1.setFont(new Font("SanSerif",Font.PLAIN,14));
	
	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("To maximize resolution, the  2-D  plots  are",Label.LEFT);
        text1.setForeground(Color.red.darker());
	text1.setFont(labfont);
	
        text2 = new Label("scaled using the maximum running value in",Label.LEFT);
        text2.setForeground(Color.red.darker());
	text2.setFont(labfont);
	
        text3 = new Label("the domain at each iteration.",Label.LEFT);
        text3.setForeground(Color.red.darker());
	text3.setFont(labfont);
	//add(text1);  add(text2);  add(text3);
        
        text1.setBounds(10,133,220,16);
        text2.setBounds(10,149,220,16);
        text3.setBounds(10,165,220,16);
        
	watch1 = new Label("0",Label.LEFT);
	watch2 = new Label("\u03c9t = ",Label.RIGHT);
	watch1.setFont(titlefont);
	watch2.setFont(omegafont);
	
	//watch3 = new Label("0.0",Label.LEFT);
	//watch4 = new Label("s",Label.LEFT);
	
	//watch3.setFont(titlefont);
	//watch4.setFont(titlefont2);
	watch1.setForeground(Color.red.darker());
	watch2.setForeground(Color.red.darker());
	//watch3.setForeground(Color.blue.darker());
	//watch4.setForeground(Color.blue.darker());
        
        watch1.setBackground(Color.white);
	watch2.setBackground(Color.white);
	//watch3.setBackground(Color.white);
	//watch4.setBackground(Color.white);
        
	//tgc3 = new TransGraphCanvas2();
	//tgc2 = new Draw2DCanvas(t_state);
	
        ssc  = new StartStopClock();
	
	slider1 = new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,ScrollMax);//changes sleep time
	slider1.setValue(ScrollMax-SleepTime);
        //slider1.setBackground(new Color(255,255,150));
        slider1.setBackground(new Color(255,255,250));
	add(slider1);
	slider1.setBounds(61,9+ydown,160,15);

	Panel ps1 = new Panel();
	    ps1.setBackground(Color.cyan);
	    add(ps1);
	    ps1.setBounds(60,8+ydown,162,17);

        Panel ps2 = new Panel();
	    ps2.setBackground(Color.black);
	    add(ps2);
	    ps2.setBounds(59,7+ydown,164,19);
            
        sliderzoom = new Scrollbar(Scrollbar.HORIZONTAL,0,1,1,ScrollMaxZoom);//changes sleep time
	sliderzoom.setValue(2);
        //sliderzoom.setBackground(new Color(255,255,150));
        sliderzoom.setBackground(new Color(255,255,250));
	add(sliderzoom);
	    
        //add(titulolabel);
	add(fast);
	add(slow);
        //add(step1);
        add(step2);
	add(watch1);
	add(watch2);
	//add(watch3);
	//add(watch4);
	//add(c1);
	add(ssc);
	
        int shiftx = 10;
        slow.setBounds(15,9+ydown,40,12);
	fast.setBounds(227,9+ydown,40,12);
        step1.setBounds(155,106,70,14);
        step2.setBounds(145,100,80,14);
        
	// CHANGE ==============================================================
	watch1.setBounds(58+shiftx,112+ydown,45,18);
        watch2.setBounds(10+shiftx,112+ydown,48,18);
        //======================================================================
	//watch3.setBounds(137+shiftx,98+ydown,60,16);
	//watch4.setBounds(205+shiftx,98+ydown,20,16);
	
        sliderzoom.setBounds(115+shiftx,117+ydown,110,15);
        Panel pszoom = new Panel();
	    pszoom.setBackground(Color.cyan);
	    add(pszoom);
	    pszoom.setBounds(114+shiftx,116+ydown,112,17);
	Panel ps2zoom = new Panel();
	    ps2zoom.setBackground(Color.black);
	    add(ps2zoom);
	    ps2zoom.setBounds(113+shiftx,115+ydown,114,19);	
	
        Panel pwatch1 = new Panel();
	    pwatch1.setBackground(Color.white);
	    //add(pwatch1);
	    pwatch1.setBounds(10+shiftx,112+ydown,87,18);
	Panel pwatch2 = new Panel();
	    pwatch2.setBackground(Color.black);
	    add(pwatch2);
	    pwatch2.setBounds(9+shiftx,111+ydown,95,20);
	Panel pwatch1b = new Panel();
	    pwatch1b.setBackground(Color.white);
	    add(pwatch1b);
	    pwatch1b.setBounds(7+shiftx,109+ydown,99,24);
	Panel pwatch2b = new Panel();
	    pwatch2b.setBackground(Color.black);
	    add(pwatch2b);
	    pwatch2b.setBounds(6+shiftx,108+ydown,101,26);
	
        Panel pwatch3 = new Panel();
	    pwatch3.setBackground(Color.white);
	    //add(pwatch3);
	    pwatch3.setBounds(126+shiftx,97+ydown,103,18);
	Panel pwatch4 = new Panel();
	    pwatch4.setBackground(Color.black);
	    //add(pwatch4);
	    pwatch4.setBounds(125+shiftx,96+ydown,105,20);
	Panel pwatch3b = new Panel();
	    pwatch3b.setBackground(Color.white);
	    //add(pwatch3b);
	    pwatch3b.setBounds(124+shiftx,95+ydown,107,22);
	Panel pwatch4b = new Panel();
	    pwatch4b.setBackground(Color.black);
	    //add(pwatch4b);
	    pwatch4b.setBounds(123+shiftx,94+ydown,109,24);
	    
	titulolabel.setBounds(15,5+ydown,150,25);
	c1.setBounds(36,342+176,270,35);
	
	ssc.setBounds(22,35+ydown,230,58);
        Panel pssc = new Panel();
	    pssc.setBackground(Color.black);
	    add(pssc);
	    pssc.setBounds(21,34+ydown,232,60);
	Panel pssc2 = new Panel();
	    pssc2.setBackground(Color.cyan);
	    add(pssc2);
	    pssc2.setBounds(20,33+ydown,234,62);	
        Panel pssc3 = new Panel();
	    pssc3.setBackground(Color.black);
	    add(pssc3);
	    pssc3.setBounds(19,32+ydown,236,64);	
	
	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.setColor(Color.white);
		
		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,12);
	Font symbolfont = new Font("Symbol",Font.PLAIN,12);
	Font subfont    = new Font("SanSerif",Font.PLAIN,10);
	Font subfont2    = new Font("SanSerif",Font.PLAIN,11);
	
        x = 10;
	y = 10;
	dx = 105;
	dxx = 15;
	            
	g.setFont(labfont);
        g.setColor(Color.blue.darker());
        
        g.setColor(Color.red.darker());
        if(sliderzoom.getValue()<7){
            MaestroG.Custom("","",""+sliderzoom.getValue()+"\u00b0","",g,13,230,112);
        }
        else{
            MaestroG.Custom("","",""+(sliderzoom.getValue()+1)+"\u00b0","",g,13,230,112);
        }
        
        g.setFont(normalfont);
	fm = g.getFontMetrics();
	dy = fm.getHeight()+2;
	dyy = dy+8;
        
        double temp;
        
        g.setColor(Color.blue.darker());
        
          
    }
    
    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();
	}
    }
    
}
