//Mod1Input.java
/*
 * Panel with all input operations for Module1.1
 * Using null layout - positions of elements hard-coded
 * authors: Umberto Ravaioli, Janice Richards
 * version 1.0 - Copyright: Amanogawa.com - All Rights Reserved
 */

import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.text.*;
import java.util.Map;
import java.util.Hashtable;
import java.util.jar.Attributes;
import javax.swing.*;
import javax.swing.event.*;

public class Mod1Input extends Panel implements ChangeListener {
    private Mod1State state;
    private static final Color bgcolor = new Color(236,236,236);
    private Label titlelabel;
    public Checkbox ch2, ch3, ch4, ch5, ch6;
    public  Button reset;
    //private Label ulabc, labc, ulabv, labv, ulabe, labe;
    //private TextField textv, texte;
    private Label ulabc, labc, ulabb, labb;
    private TextField textb;
    public TextField textc;
    public JSlider sliderc;
    public ShowAngle sa;
    public int ScrollMax = 10000;
    int  w2,w3,w4,w5,w6, wb1, wb2;   // wv1,wv2,we1,we2;
    int yLine;
    public boolean bfieldAtCursorIsValid = false;  // initially
    DecimalFormat dec = new DecimalFormat("#.##");
    DecimalFormat dec2 = new DecimalFormat("#.###");
    DecimalFormat dec3 = new DecimalFormat("#.#####E0");
    //DecimalFormat decDEBUG = new DecimalFormat("#.######");
    private static final String emptyString = "                        ";
    //private boolean ignoreNextTextEvent = false;
    private boolean ignoreSliderAdjustment = false;
    private Font labelfont, titlefont;

    public Mod1Input(Mod1State state){
	super();
	setLayout(null);
	setBackground(bgcolor);
	this.state = state;
    }


    public void initialize() {
        labelfont = state.ttfFont.deriveFont(Font.BOLD,12f);
        
        labelfont = new Font("SanSerif",Font.BOLD,state.font12);
        titlefont = new Font("SanSerif",Font.BOLD,state.font16);

	titlelabel = new Label("Input",Label.CENTER);
	//titlelabel.setFont(TheFonts.bold16);
	titlelabel.setFont(titlefont);
        
	reset = new Button("Clear");
        //reset.setFont(TheFonts.sanSerif12);
        reset.setFont(labelfont);
        add(reset);

	CheckboxGroup chgroup1 = new CheckboxGroup();
	//ch1 = new Checkbox("  place -ve charge",true,chgroup1);
        ch2 = new Checkbox("  add line source",true,chgroup1);
        ch3 = new Checkbox("  edit current value",false,chgroup1);
        ch4 = new Checkbox("  delete line source",false,chgroup1);
        ch5 = new Checkbox("  drag line source",false,chgroup1);
        ch6 = new Checkbox("  display magnetic field",
                           false,chgroup1);
        Label labch6 = new Label("       at cursor:");
        
        getLabelWidths();
	
        //ch1.setForeground(Color.black);
        //ch1.setFont(state.ttfFont.deriveFont(14f));
	ch2.setForeground(Color.black);
        ch2.setFont(labelfont);
	ch3.setForeground(Color.black);
        ch3.setFont(labelfont);
	ch4.setForeground(Color.black);
        ch4.setFont(labelfont);
	ch5.setForeground(Color.black);
        ch5.setFont(labelfont);
	ch6.setForeground(Color.black);
        ch6.setFont(labelfont);
        labch6.setFont(labelfont);
        
	//ch1.setBackground(bgcolor);
	ch2.setBackground(bgcolor);
	ch3.setBackground(bgcolor);
	ch4.setBackground(bgcolor);
	ch5.setBackground(bgcolor);
	ch6.setBackground(bgcolor);
	labch6.setBackground(bgcolor);

	labc = new Label("line source =  ",Label.RIGHT);
        labc.setFont(labelfont);
        labc.setForeground(Color.black);
        labb = new Label("B =  ",Label.RIGHT);
        labb.setFont(labelfont);
        labb.setForeground(Color.black);

        textc = new TextField(6);
        textc.setFont(labelfont);
        textc.setForeground(Color.black);
	textc.setText(""+dec.format(state.chargeToAdd));
        textb = new TextField(6);
        textb.setFont(labelfont);
        textb.setForeground(Color.black);
	textb.setText("");
	textb.setEditable(false);
        
        textb.setBackground(Color.white);
        textc.setBackground(Color.white);

	ulabc = new Label("A ",Label.LEFT);
        ulabc.setFont(labelfont);

        //ulabb = new Label("A/m ",Label.LEFT);
	ulabb = new Label("T ",Label.LEFT);   // for "Teslas"
        ulabb.setFont(labelfont);

	sliderc = new JSlider(0,ScrollMax+1);
	//sliderc = new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,ScrollMax+1);
	sliderc.setValue((int)((state.chargeToAdd-state.chargeMin)*
                               ScrollMax/(state.chargeMax-state.chargeMin)));

        sa = new ShowAngle(state);

	add(labc);
	add(textc);
	add(sliderc);
	add(ulabc);
        add(labb);
        add(textb);
        add(ulabb);

        add(sa);

	int vPosInit = state.s80, vPos, dvPos = state.s4, vHeight = state.s20; 
	vPos = vPosInit;
        // column = 240
        int col = state.s50;
        
        /*
	labc.setBounds(col+20,vPos,80,vHeight);
	textc.setBounds(col+100,vPos,80,vHeight);
	ulabc.setBounds(col+180,vPos,80,vHeight);
        */
	labc.setBounds(col+state.s20,vPos,state.s120,vHeight);
	textc.setBounds(col+state.s140,vPos,state.s60,vHeight);
	ulabc.setBounds(col+state.s202,vPos,state.s80,vHeight);

	    vPos += vHeight + dvPos; 
	   
	sliderc.setBounds(col+state.s22,vPos+state.s2,state.s196,vHeight);
	/*
        sliderc.setBackground(Color.white);
            Panel ps1 = new Panel();
            ps1.setBackground(Color.orange);
	    add(ps1);
	    ps1.setBounds(col+state.s22-1,vPos+state.s2-1,state.s196+2,vHeight+2);

            Panel ps10 = new Panel();
            ps10.setBackground(Color.black);
            add(ps10);
            ps10.setBounds(col+state.s22-2,vPos+state.s2-2,state.s196+4,vHeight+4);
	*/
	add(titlelabel);

	//add(ch1);
	add(ch2);
	add(ch3);
	add(ch4);
	add(ch5);
	add(ch6);
	add(labch6);

	titlelabel.setBounds(state.s30,state.s35,state.s50,state.s18);

        int column1 = state.s70;
        //int column2 = 240;
        int column2 = state.s200;
        //int startingY = 40;
        int deltaY = state.s30;
        int startingY = state.s60 + 3*deltaY;
        int h = state.s20;
	//ch1.setBounds(column1,startingY,w1,h);
	ch2.setBounds(column1,startingY,w2,h);
	ch3.setBounds(column1,startingY+deltaY,w3,h);
	ch4.setBounds(column1,startingY+2*deltaY,w4,h);
	ch5.setBounds(column1,startingY+3*deltaY,w5,h);
	ch6.setBounds(column1,startingY+53*deltaY/10,w6,h);
	labch6.setBounds(column1,startingY+6*deltaY,w6+state.s40,h);

        int newCol = column1;
        getCursorStuffWidths();

        labb.setBounds(newCol,startingY+7*deltaY+state.s10,wb1,h);
        newCol+=wb1;
        textb.setBounds(newCol,startingY+7*deltaY+state.s10,state.s100,h);
        newCol+=state.s100;
        ulabb.setBounds(newCol,startingY+7*deltaY+state.s12,wb2+state.s20,h);

        newCol = column1+state.s180;
        
        sa.setBounds(newCol,startingY+5*deltaY+state.s20,state.s40,state.s40);

        //  band VI
	//int y_update =355;
	//int y_update =455;

        int y_update = state.s440;
        int x_update = state.s170;
        
	reset.setBounds(x_update,y_update,state.s60,state.s20);
	Panel ps7 = new Panel();
	    ps7.setBackground(Color.orange);
	    add(ps7);
	    ps7.setBounds(x_update-1,y_update-1,state.s60+2,state.s20+2);
	    
	Panel ps8 = new Panel();
	    ps8.setBackground(Color.black);
	    add(ps8);
	    ps8.setBounds(x_update-2,y_update-2,state.s60+4,state.s20+4);

        sliderc.addChangeListener(this);
    }


    private void getLabelWidths() {
        //setFont(state.ttfFont.deriveFont(14f));
        setFont(labelfont);
        //Graphics g=getGraphics();
        //FontMetrics fm = g.getFontMetrics();
        
        FontMetrics fm = getFontMetrics(getFont());

        int extra = fm.stringWidth("gegegege"); //TEMPORARY???SAT JUL 26
        w2 = fm.stringWidth("add line source")+extra;
        w3 = fm.stringWidth("edit current value")+extra;
        w4 = fm.stringWidth("delete line source")+extra;
        w5 = fm.stringWidth("drag line source")+extra;
        w6 = fm.stringWidth("display magnetic field")+extra;
        
        //g.dispose();
    }


    private void getCursorStuffWidths() {
        setFont(state.ttfFont.deriveFont(14f));
        //Graphics g=getGraphics();
        //FontMetrics fm = g.getFontMetrics();
        
        FontMetrics fm = getFontMetrics(getFont());

        int extra = fm.stringWidth("gege");
        wb1 = fm.stringWidth("B = ")+extra;
        wb2 = fm.stringWidth("A/m")+extra;
        //g.dispose();
    }


    // CHANGE this name later ...
    public void showBFieldAtCursor() {
        // read state.bfieldAtCursor
        // set flag that bfieldAtCursorIsValid = true;
        // repaint();
        textb.setText(""+dec3.format(state.bfieldAtCursor));
        sa.draw();
        return;
    }


    public void readTextUpdateSlider() {
        //if (ignoreNextTextEvent) {
        //    ignoreNextTextEvent = false;
        //    return;
        //}
        double charge=  0.0;
        ignoreSliderAdjustment = true;

        try { 
            if(Double.valueOf(textc.getText()).doubleValue()<=state.chargeMax &&
               Double.valueOf(textc.getText()).doubleValue()>=state.chargeMin){
                charge=Double.valueOf(textc.getText()).doubleValue();
                sliderc.setValue((int)(ScrollMax*(charge-state.chargeMin)
                                            /(state.chargeMax-state.chargeMin)));
                charge = rounder(charge,2);
            } else if(Double.valueOf(textc.getText()).doubleValue()>state.chargeMax){
                charge = state.chargeMax;
                textc.setText(""+dec.format(state.chargeMax));
                sliderc.setValue(ScrollMax);
            } else if(Double.valueOf(textc.getText()).doubleValue()<state.chargeMin){
                charge = state.chargeMin;
                textc.setText(""+dec.format(state.chargeMin));
                sliderc.setValue(0);
            }
        } catch(NumberFormatException e){
            charge =  0.0;
        }
        state.chargeToAdd = charge;
        //if (ch2.getState()) state.chargeToAdd = charge;
        //else if (chxx.getState()) state.chargeChangeValue = charge;
    }


    public void stateChanged(ChangeEvent evt){//NEED HERE
        if (evt.getSource()==sliderc) {

            // want to ignore the slider if it was changed PROGRAMMATICALLY,
            //  (done after user changed the charge value text-field.
            if (ignoreSliderAdjustment) {
                ignoreSliderAdjustment = false;
                // BUT don't want to remove event, so Mod1 can still see it,
                //  and draw the plot and display the output
                return;
            }

            if(sliderc.getValue()>=ScrollMax){
                state.chargeToAdd=state.chargeMax;
                sliderc.setValue((int)((state.chargeToAdd-state.chargeMin)*
                                       ScrollMax/(state.chargeMax-state.chargeMin)));
	    } else {
		state.chargeToAdd = state.chargeMin + 
		    (state.chargeMax-state.chargeMin)*
                    sliderc.getValue()/ScrollMax;
                //state.chargeToAdd = MaestroA.rounder(state.chargeToAdd,7);
                state.chargeToAdd = rounder(state.chargeToAdd,2);
	    }
	    textc.setText(""+dec.format(state.chargeToAdd));
            //ignoreNextTextEvent = true;
        }
    }

    private double rounder(double dval, int places) {
        int multiplier = 1;
        for (int i=0; i<places ; i++)
            multiplier *= 10;
        int value = (int)(dval*multiplier);
        double newDval = 1.0*value/multiplier;
        return newDval;
    }


    public void reset() {
        ch2.setState(true);   // set to "Place Charge"
        state.bfieldAtCursor = 0.0; 
        textb.setText("0");
        state.chargeToAdd = state.chargeMax;
	textc.setText(""+dec.format(state.chargeToAdd));
	sliderc.setValue((int)((state.chargeToAdd-state.chargeMin)*
                               ScrollMax/(state.chargeMax-state.chargeMin)));
        sa.reset();
    }


    public void clearShowBFieldStuff() {
        //textv.setText("0");
        //texte.setText("0");
        textb.setText("0");
        sa.reset();
    }


    
    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);

	Graphics2D g2d = (Graphics2D)g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                             RenderingHints.VALUE_ANTIALIAS_ON);

        /*
        g.setColor(Color.black);
        g.setFont(TheFonts.bold14);
        g.drawLine(5,yLine,455,yLine);
        g.drawString("Plots",25,yLine+20);
        */
	sliderc.requestFocusInWindow();
    }
}
