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

public class RectWaveChooser extends Panel implements ActionListener, MouseListener,
    ItemListener{
    
    private static final Color bgcolor = new Color(236,236,236);
    private Label titlelabel, plot_style;
    public CheckboxGroup cgrp, field;
    public Checkbox c1, c2, c3, c4, f1, f2, f3;
    public Button bplot1, bplot2, bplot3;
    public Panel p_plot1, p_plot2, p_plot3;
    public int x, y;
    public RectWaveGuide_State state;
    public boolean IsTE, IsTot, IsS;
    
    public RectWaveChooser(RectWaveGuide_State state){
	super();
	setLayout(null);
        
        this.state = state;
	setBackground(bgcolor);
	
        titlelabel = new Label("Plot",Label.LEFT);
	titlelabel.setFont(new Font("SanSerif",Font.BOLD,16));
        titlelabel.setForeground(Color.black);
        titlelabel.setBounds(state.s10,state.s15,state.s50,state.s18);
        add(titlelabel);
        
        plot_style = new Label("Field Plot Style",Label.CENTER);
	plot_style.setFont(new Font("SanSerif",Font.BOLD,12));
        plot_style.setForeground(Color.black);
        plot_style.setBounds(state.s170,state.s15,state.s110,state.s16);
        add(plot_style);
        
        IsTE = true;
        IsTot = true;
        IsS = false;
	cgrp = new CheckboxGroup();
        field = new CheckboxGroup();
	c1 = new Checkbox("",cgrp,false); // Ex
	c2 = new Checkbox("",cgrp,false); // Ey
	c3 = new Checkbox("",cgrp,true); // Etot
	c4 = new Checkbox("",cgrp,false); // S
        
        c1.setBackground(bgcolor);
	c2.setBackground(bgcolor);
        c3.setBackground(bgcolor);
	c4.setBackground(bgcolor);
        
        f1 = new Checkbox("plot 1",field,true); // color map
	f2 = new Checkbox("plot 2",field,false); // colored arrows
	f3 = new Checkbox("plot 3",field,false); // only black arrows
	f1.setFont(new Font("SanSerif",Font.BOLD,state.s12));
        f2.setFont(new Font("SanSerif",Font.BOLD,state.s12));
        f3.setFont(new Font("SanSerif",Font.BOLD,state.s12));
        
        f1.setBackground(bgcolor);
	f2.setBackground(bgcolor);
        f3.setBackground(bgcolor);
        
        bplot1 = new Button("1");
        bplot2 = new Button("2");
        bplot3 = new Button("3");
        p_plot1 = new Panel();
	    p_plot1.setBackground(Color.black);
        p_plot2 = new Panel();
	    p_plot2.setBackground(Color.gray.brighter());
        p_plot3 = new Panel();
	    p_plot3.setBackground(Color.gray.brighter());
        
        bplot1.setFont(new Font("SanSerif",Font.BOLD,state.font12));
        bplot2.setFont(new Font("SanSerif",Font.BOLD,state.font12));
        bplot3.setFont(new Font("SanSerif",Font.BOLD,state.font12));
        bplot1.setBackground(bgcolor);
        bplot2.setBackground(bgcolor);
        bplot2.setBackground(bgcolor);
        bplot1.setForeground(Color.black);
        bplot2.setForeground(Color.gray.brighter());
        bplot3.setForeground(Color.gray.brighter());
        
	//add(c1);
	//add(c2);
	add(c3);
        add(c4);
        add(f1);
        add(f2);
        add(f3);
        add(bplot1);
        add(bplot2);
        add(bplot3);
        add(p_plot1);
        add(p_plot2);
        add(p_plot3);
        
        // CHANGE ============================================================== PUT OUT OF RANGE - NO LONGER USING
        c3.setBounds(state.s100000,state.s30,state.s25,state.s20);
        c4.setBounds(state.s100000,state.s30,state.s25,state.s20);
        //======================================================================
        
        // CHANGE ==============================================================
        //f1.setBounds(state.s180,state.s10,state.s100,state.s20);
        //f2.setBounds(state.s180,state.s30,state.s100,state.s20);
        //f3.setBounds(state.s180,state.s50,state.s100,state.s20);
        //======================================================================
        
        bplot1.setBounds(state.s170,state.s37,state.s30,state.s26);
        bplot2.setBounds(state.s200+state.s10,state.s37,state.s30,state.s26);
        bplot3.setBounds(state.s200+state.s50,state.s37,state.s30,state.s26);
        
        p_plot1.setBounds(state.s170-3,state.s37-3,state.s30+6,state.s26+6);
        p_plot2.setBounds(state.s200+state.s10-3,state.s37-3,state.s30+6,state.s26+6);
        p_plot3.setBounds(state.s200+state.s50-3,state.s37-3,state.s30+6,state.s26+6);
        
        //Listeners
	c3.addItemListener(this);
        c4.addItemListener(this);
        
        f1.addItemListener(this);
	f2.addItemListener(this);
	f3.addItemListener(this);
        
        bplot1.addActionListener(this);
	bplot2.addActionListener(this);
	bplot3.addActionListener(this);
        
        this.addMouseListener(this);
    }
    
    public void paint(Graphics g){
	g.clearRect(0,0,getSize().width,getSize().height);
        
        Graphics2D g2d = (Graphics2D)g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        
        int ylevel = state.s45;
        
        g.setColor(Color.magenta.darker());
        MaestroG.subscripterSerB("S","z","",g,state.font14,state.s30,ylevel + state.s5);
        
        if(IsTE){
            g.setColor(Color.red.darker());
            MaestroG.subscripterSerB("E(x,y)","","",g,state.font14,state.s115,ylevel + state.s5);
        }
        else{
            g.setColor(Color.blue.darker());
            MaestroG.subscripterSerB("H(x,y)","","",g,state.font14,state.s115,ylevel + state.s5);
        }
        
        // DRAW SCALABLE RADIO BUTTONS DONE BY HAND - BECAUSE OF WINDOWS 10 (WHICH SUCKS, TOO)
        if(IsS){
            g.setColor(Color.white);
            MaestroG.fillCircle(state.s15,ylevel, state.s13,g);
            g.setColor(Color.black);
            MaestroG.drawCircle(state.s15,ylevel, state.s13,g);
            g.setColor(Color.black);
            MaestroG.fillCircle(state.s15,ylevel, state.s6,g);
            
            g.setColor(Color.white);
            MaestroG.fillCircle(state.s100,ylevel, state.s13,g);
            g.setColor(Color.black);
            MaestroG.drawCircle(state.s100,ylevel, state.s13,g);
        }
        if(IsTot){
            g.setColor(Color.white);
            MaestroG.fillCircle(state.s15,ylevel, state.s13,g);
            g.setColor(Color.black);
            MaestroG.drawCircle(state.s15,ylevel, state.s13,g);
            g.setColor(Color.black);
            
            g.setColor(Color.white);
            MaestroG.fillCircle(state.s100,ylevel, state.s13,g);
            g.setColor(Color.black);
            MaestroG.drawCircle(state.s100,ylevel, state.s13,g);
            g.setColor(Color.black);
            MaestroG.fillCircle(state.s100,ylevel, state.s6,g);
        }
    }
    
    
    public void mouseClicked(MouseEvent evt){
        x = evt.getX();
	y = evt.getY();
        
        if(x < 80){
            IsTot = false;
            IsS = true;
            c4.setState(true);
	
            bplot1.setEnabled(false);
            bplot2.setEnabled(false);
            bplot3.setEnabled(false);
            state.ignition();
            repaint();
        }
        else if(x>100){
            IsTot = true;
            IsS = false;
            c4.setState(false);
	
            bplot1.setEnabled(true);
            bplot2.setEnabled(true);
            bplot3.setEnabled(true);
            state.ignition();
            repaint();
        }
    }
    public void mouseEntered(MouseEvent evt){ }
    public void mouseExited(MouseEvent evt) { }
    public void mousePressed(MouseEvent evt) {myEventHandler(evt);}
    public void mouseReleased(MouseEvent evt) { }
    
    
    public void actionPerformed(ActionEvent evt){
        
        if(evt.getSource() == bplot1){// plot type 1
            p_plot1.setBackground(Color.black);
            p_plot2.setBackground(Color.gray.brighter());
            p_plot3.setBackground(Color.gray.brighter());
        
            bplot1.setForeground(Color.black);
            bplot2.setForeground(Color.gray.brighter());
            bplot3.setForeground(Color.gray.brighter());
            repaint();
        }
        
        if(evt.getSource() == bplot2){// plot type 2
            p_plot1.setBackground(Color.gray.brighter());
            p_plot2.setBackground(Color.black);
            p_plot3.setBackground(Color.gray.brighter());
        
            bplot1.setForeground(Color.gray.brighter());
            bplot2.setForeground(Color.black);
            bplot3.setForeground(Color.gray.brighter());
            repaint();
        }
        if(evt.getSource() == bplot3){// plot type 3
            p_plot1.setBackground(Color.gray.brighter());
            p_plot2.setBackground(Color.gray.brighter());
            p_plot3.setBackground(Color.black);
        
            bplot1.setForeground(Color.gray.brighter());
            bplot2.setForeground(Color.gray.brighter());
            bplot3.setForeground(Color.black);
            repaint();
        }
    }
    
    private void myEventHandler(MouseEvent evt){
        
    }
    
    public void redoitE(){
        c1.setLabel("Ex");
        c2.setLabel("Ey");
        c3.setLabel("| Etot |");
    }
    
    public void redoitH(){
        c1.setLabel("Hx");
        c2.setLabel("Hy");
        c3.setLabel("| Htot |");
    }
    
    public void itemStateChanged(ItemEvent evt){
	
	if(evt.getSource()==c3){
	    f1.setEnabled(true);
            f2.setEnabled(true);
            f3.setEnabled(true);
            
            bplot1.setEnabled(true);
            bplot2.setEnabled(true);
            bplot3.setEnabled(true);
	}
	else if(evt.getSource()==c4){
	    f1.setEnabled(false);
            f2.setEnabled(false);
            f3.setEnabled(false);
            
            bplot1.setEnabled(false);
            bplot2.setEnabled(false);
            bplot3.setEnabled(false);
	}
	state.ignition();
    }
    
   
}