//NewGuideInputPanel.java
import java.awt.*;
import java.awt.event.*;

public class NewGuideInputPanel extends Panel implements ActionListener, AdjustmentListener, ItemListener{
    private NewGuide_State state;
    //private NewModeControlPanel pwmc;
    //private static final Color bgcolor = new Color(160,200,200);
    private static final Color bgcolor = new Color(236,236,221);
    private static final Font labfont=new Font("SanSerif",Font.PLAIN,12);
    private static final Font labfontB=new Font("SanSerif",Font.BOLD,12);
    private static final Font labfont0=new Font("SanSerif",Font.PLAIN,10);
    private static final Font labfont2=new Font("Serif",Font.PLAIN,14);
    private static final Font normalfont = new Font("SanSerif",Font.PLAIN,14);
    private static final Font labelfont = new Font("Serif",Font.ITALIC,14);
    private static final Font labelfont2 = new Font("Serif",Font.ITALIC,16);
    private static final Font labelfont0 = new Font("Serif",Font.ITALIC,12);
    private static final Font labelfontIT = new Font("Serif",Font.ITALIC,18);
    public int ScrollMax = 10000;
    public int ScrollMax2 = 751; 
    //public int ScrollMax2 = 76; 
    public int ScrollMax1 = 180; 
    public int ScrollMax3 = 1000;
    
    public Checkbox cN1, cN2, cN3, cN4, cN5, cN6, cAnt1, cAnt2;
    public CheckboxGroup cNumber, cAntenna;
    
    public int offset = 10;
    private Label titlelabel, titlelabel2;
    private Label lab1A, lab1, lab2, lab2_a, lab2_b, lab2_c, lab2A, lab3, 
                  lab3b, lab3c, lab4, lab4A, lab6a, lab6b, lab6c, labT;
    private Label ulab1, ulab3, ulab2, ulab4, ulabT, Click;
    public TextField text1, text2, text3, text4, text6, textT;
    public Scrollbar slider1, slider2, slider3, slider5;
    public Scrollbar sliderT, sliderT0;
    public  Button b1, b2, b3;
    public  Button bupdate;
    public Panel ps1, ps2;
    Choice ch1, ch2;
  
    public NewGuideInputPanel(NewGuide_State state){
	super();
	setLayout(null);
	setBackground(bgcolor);
	this.state = state;
        
        cNumber = new CheckboxGroup();
	cN1 = new Checkbox(" 1",false,cNumber);
        cN2 = new Checkbox(" 2",true,cNumber);
	cN3 = new Checkbox(" 3",false,cNumber);
	cN4 = new Checkbox(" 4",false,cNumber);
        cN5 = new Checkbox(" 5",false,cNumber);
	cN6 = new Checkbox(" 6",false,cNumber);
	
        cAntenna = new CheckboxGroup();
	cAnt1 = new Checkbox("  \u03bb / 2   dipole",true,cAntenna);
        cAnt2 = new Checkbox("  Parabolic   Reflector",false,cAntenna);
	
	titlelabel = new Label("Set Array",Label.LEFT);
	titlelabel.setFont(new Font("SanSerif",Font.BOLD,12));    
	titlelabel2 = new Label("Scan Angle",Label.LEFT);
	titlelabel2.setFont(new Font("SanSerif",Font.BOLD,12));    
        
	lab1A = new Label("Element Type",Label.LEFT);
	lab1A.setFont(labfont);
        lab1A.setForeground(Color.red);
	
        Click = new Label("Click Update",Label.LEFT);
	Click.setFont(labfontB);
        Click.setForeground(Color.red);
	
        lab1 = new Label("d  = ",Label.RIGHT);
	lab1.setFont(labelfont);
	
        lab2A = new Label("Elements Spacing",Label.LEFT);
	lab2A.setFont(labfont);
	lab2A.setForeground(Color.red);
        lab2 = new Label("d",Label.RIGHT);
        lab2.setFont(labelfont2);
        //lab2_a = new Label("E",Label.RIGHT);
	//lab2_a.setFont(labelfont);
	//lab2_b = new Label("0",Label.LEFT);
	//lab2_b.setFont(labelfont0);
        lab2_c = new Label(" = ",Label.RIGHT);
	lab2_c.setFont(labelfont);
        
        
        // labels for epsilon - disabled
        lab3 = new Label("\u03b5",Label.RIGHT);
	lab3.setFont(labelfont);
	lab3b = new Label("r",Label.LEFT);
	lab3b.setFont(labelfont);
	lab3.setForeground(Color.black);
	lab3b.setForeground(Color.black);
	lab3c = new Label("=",Label.LEFT);
	lab3c.setFont(labelfont);
	lab3c.setForeground(Color.black);
        
        
	lab4A = new Label("Number of Elements",Label.LEFT);
	lab4A.setFont(labfont);
        lab4A.setForeground(Color.red);
        lab4 = new Label("f  = ",Label.RIGHT);
	lab4.setFont(labelfont);
        
        // labels for wire - disabled
	lab6a = new Label("a",Label.RIGHT);
	lab6a.setFont(labelfontIT);
	lab6b = new Label("Radius",Label.CENTER);
	lab6b.setFont(labfont2);
	lab6c = new Label(" = ",Label.RIGHT);
	lab6c.setFont(labelfont);
			
	text1 = new TextField(""+state.a,8);
        text2 = new TextField(""+state.taper_parameter,8);
	text3 = new TextField(""+state.epsilon_r,8);
	text4 = new TextField(""+state.frequency,8);
	text6 = new TextField(""+state.wire_radius,8);
	
	ulab4 = new Label(" [Hz]",Label.LEFT);
	ulab2 = new Label(" [V/m]",Label.LEFT);
        ulab1 = new Label("\u03bb",Label.LEFT);
        ulab1.setFont(labfont2);
        ulab3 = new Label("\u03bb",Label.LEFT);
        ulab3.setFont(labfont2);
        
	slider1 = new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,ScrollMax+1);//changes a
	slider2 = new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,ScrollMax+1);//changes frequency
	slider3 = new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,ScrollMax2);//changes taper parameter
	slider1.setBackground(Color.white);
        slider2.setBackground(Color.white);
        slider3.setBackground(Color.white);
        
	slider1.setValue((int)((state.a-state.a_minimum)*ScrollMax/(state.a_maximum-state.a_minimum)));
	slider2.setValue((int)((state.frequency-state.frequency_minimum)*ScrollMax/(state.frequency_maximum-state.frequency_minimum)));
	slider3.setValue(750);
	//slider3.setValue(75);
        
        sliderT = new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,ScrollMax1+1);//changes theta
        sliderT0 = new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,ScrollMax3+1);//changes theta decimal
        sliderT.setValue((int)((state.theta_angle-state.theta_minimum)*ScrollMax1/(state.theta_maximum-state.theta_minimum)));
	sliderT0.setValue((int)((state.theta_angle-(int)state.theta_angle)*ScrollMax3));
        sliderT.setBackground(Color.white);
        sliderT0.setBackground(Color.white);
        
        labT = new Label(" \u03b8   = ",Label.RIGHT);
	labT.setFont(labelfont);
	textT = new TextField(""+(state.theta_angle-90.0),8);
	ulabT = new Label(" [deg]",Label.LEFT);
	
        
	b1 = new Button("Range");
	b2 = new Button("Range");
	b3 = new Button("Range");
	
	bupdate = new Button("Update");
	add(titlelabel);
	add(titlelabel2);
        
	add(lab1);
        add(lab1A);
        //add(Click);
        //Click.setVisible(false);
	//add(lab2_a);
        //add(lab2_b);
        add(lab2_c);
        add(lab2A);
	add(lab2);
        add(labT);
        
        //add(lab3);
	//add(lab3b);
	//add(lab3c);
	
        add(lab4);
        add(lab4A);
        
	//add(lab6a);
	//add(lab6b);
	//add(lab6c);
	
	add(text1);
	add(text2);
	add(text3);
	add(text4);
	add(text6);
        add(textT);
	
	add(ulab4);
	add(ulab2);
        add(ulab1);
        add(ulab3);
        add(ulabT);
        
	add(slider1);
	add(slider2);
	add(slider3);
	add(sliderT);
        add(sliderT0);
        
	add(b1);
	add(b2);
	add(b3);
	add(bupdate);
	add(cN1); add(cN2); add(cN3); add(cN4); add(cN5); add(cN6);
        add(cAnt1); add(cAnt2);
        
	titlelabel.setBounds(10,5,150,18);
	Click.setBounds(190,5,95,18);
        int vPosInit = 20+offset, vPos, dvPos = 7, vHeight = 18, vSlider = 15; 
	vPos = vPosInit;
	
	lab2A.setBounds(10,vPos-3,120,vHeight);
        lab2.setBounds(130,vPos-5,20,vHeight);
        lab2_c.setBounds(150,vPos-3,20,vHeight);
	text2.setBounds(170,vPos-5,50,vHeight+2);
	ulab3.setBounds(235,vPos-3,35,vHeight);   
	
	vPos += vHeight;
	slider3.setBounds(80,vPos+4,196,vSlider);
	Panel ps19 = new Panel();
	    ps19.setBackground(Color.cyan);
	    add(ps19);
	    ps19.setBounds(79,vPos+3,198,vSlider+2);
	    
	Panel ps110 = new Panel();
	    ps110.setBackground(Color.black);
	    add(ps110);
	    ps110.setBounds(78,vPos+2,200,vSlider+4);
	
	//b3.setBounds(15,vPos,55,20);
	
	//vPos += vHeight + 2*dvPos;
    		
	// update
	//bupdate.setBounds(10,vPos+2,60,vHeight+3);	
	//Panel ps7 = new Panel();
	//    ps7.setBackground(Color.cyan);
	//    add(ps7);
	//    ps7.setBounds(9,vPos+1,62,vHeight+5);
	    
	//Panel ps8 = new Panel();
	//    ps8.setBackground(Color.black);
	//    add(ps8);
	//    ps8.setBounds(8,vPos,64,vHeight+7);
    
        vPos += vHeight + 2*dvPos;
        lab4A.setBounds(10,vPos-3,120,vHeight);
        vPos += vHeight + dvPos/2-5;
        cN1.setBounds(20,vPos,45,vHeight);
        cN2.setBounds(65,vPos,45,vHeight);
        cN3.setBounds(110,vPos,45,vHeight);
        cN4.setBounds(155,vPos,45,vHeight);
        cN5.setBounds(200,vPos,45,vHeight);
        cN6.setBounds(245,vPos,45,vHeight);
        
        vPos += vHeight + 15*dvPos/10;
        lab1A.setBounds(10,vPos-3,120,vHeight);
        vPos += vHeight + dvPos/2-5;
        cAnt1.setBounds(20,vPos,200,vHeight);
        vPos += vHeight + dvPos/2;
        cAnt2.setBounds(20,vPos,200,vHeight);
        
	//Listeners
	bupdate.addActionListener(this);
	slider1.addAdjustmentListener(this);
	slider2.addAdjustmentListener(this);
	slider3.addAdjustmentListener(this);
        sliderT0.addAdjustmentListener(this);
	sliderT.addAdjustmentListener(this);
        
        cN1.addItemListener(this);
        cN2.addItemListener(this);
        cN3.addItemListener(this);
        cN4.addItemListener(this);
        cN5.addItemListener(this);
        cN6.addItemListener(this);
        cAnt1.addItemListener(this);
        cAnt2.addItemListener(this);
    }
    
    @Override
    public void paint(Graphics g){
	
	g.setColor(bgcolor.darker());
	g.fillRect(0,getSize().height-2,getSize().width,2);
	g.fillRect(getSize().width-2,0,2,getSize().height);
	g.setColor(bgcolor.brighter());
	g.fillRect(0,0,2,getSize().height-1);
	g.fillRect(0,0,getSize().width-2,2);
	int vPosInit = 30, vPos, dvPos = 2, vHeight = 20; 
	vPos = vPosInit;
	g.setColor(Color.black);
	g.drawLine(10,218,getSize().width-10,218);
	
	g.setColor(Color.red);    
	g.setFont(new Font("SanSerif",Font.PLAIN,12));
	
	g.setColor(Color.black);    
	g.setFont(new Font("Serif",Font.PLAIN,14));
	//g.drawString("\u03bb",255,vPos+53);
	//g.drawString("\u03bb",255,getSize().height-52);
	//g.drawString("\u03c1",142,getSize().height-52);
	g.setFont(new Font("SanSerif",Font.PLAIN,12));
    }
    
    public void actionPerformed(ActionEvent evt){
     //***********
     if(evt.getSource()==bupdate){
	//*********
	    
	    try{
                if(Double.valueOf(text2.getText()).doubleValue() > 1.0){
                    state.taper_parameter = 1.0;
		    slider3.setValue(ScrollMax2-1);
                    text2.setText("1.0");
                }
                else if(Double.valueOf(text2.getText()).doubleValue()<0.25){
                    state.taper_parameter = 0.25;
		    slider3.setValue(250);
                    //slider3.setValue(25);
                    text2.setText("0.25");
                }
                else{
                    state.taper_parameter = Double.valueOf(text2.getText()).doubleValue();
		    slider3.setValue((int)((1000.0)*(state.taper_parameter))-250);
                    //slider3.setValue((int)((100.0)*(state.taper_parameter))-25);
                }
	    }
	    catch(NumberFormatException e){
		  state.taper_parameter = 0.0; 
	    }
           
	  }
            //Click.setVisible(false);
            repaint();
	    state.ignition();
            state.scanField();
            state.scan_coefficients();
            
    }//End of actionPerformed()
    
    public void adjustmentValueChanged(AdjustmentEvent evt){
	if(evt.getSource()==slider3){
	    state.taper_parameter  = 0.25 + (double)slider3.getValue()/1000.0;
            //state.taper_parameter  = 0.25 + (double)slider3.getValue()/100.0;
	    text2.setText(""+MaestroA.rounder(state.taper_parameter,7));
            
            //Click.setVisible(true);
            repaint();
            
            state.ignition();
            //state.scanField();
            //state.scan_coefficients();
	}
        
        
        
    }
    public void itemStateChanged(ItemEvent evt){
	if(evt.getSource() == cN1){
            state.N_elements = 1;
            state.ignition();
            state.scanField();
	}
        else if(evt.getSource() == cN2){
            state.N_elements = 2;
            state.ignition();
            state.scanField();
	}
	else if(evt.getSource() == cN3){
            state.N_elements = 3;
            state.ignition();
            state.scanField();
	}
        else if(evt.getSource() == cN4){
            state.N_elements = 4;
            state.ignition();
            state.scanField();
	}
        else if(evt.getSource() == cN5){
            state.N_elements = 5;
            state.ignition();
            state.scanField();
	}
        else if(evt.getSource() == cN6){
            state.N_elements = 6;
            state.ignition();
            state.scanField();
	}
        
        else if(evt.getSource() == cAnt1){
            state.IsParabolic = false;
            state.ignition();
            state.scanField();
	}
        else if(evt.getSource() == cAnt2){
            state.IsParabolic = true;
            state.ignition();
            state.scanField();
	}
    }
    
}


