package compphys_640364;
import java.applet.*;
import java.awt.*;
import java.awt.Scrollbar;
import java.awt.event.*;

public class MultiThreadedApplet extends Applet implements ActionListener, AdjustmentListener, ItemListener {
	private AppletWorker appletWorker;
	private Lattice lattice;
	private Label sweepsLabel = new Label();
	private Label betaLabel = new Label();
	private Label magLabel = new Label();
	private Checkbox magCheckbox;
	public Scrollbar sb1;

	public void init() {
	    setBackground(Settings.colourBg);
	    
	    if (Settings.appletWidth < 300) {
	        Settings.appletWidth = 300;
	    }
	    
	    if (Settings.appletHeight< 100) {
	        Settings.appletHeight = 100;
	    }
	    
	    
	    setLayout(new BorderLayout());
		Panel p = new Panel();
		p.setLayout( new GridLayout(0,3,2,2));
		
	    Button b0 = new Button("Start");
	    b0.addActionListener(this);
	    p.add(b0);

	    Button b1 = new Button("Pause");
	    b1.addActionListener(this);
	    p.add(b1);
	    
	    Button b2 = new Button("Thermalise");
	    b2.addActionListener(this);
	    p.add(b2);
	    
	    sb1 = new Scrollbar(Scrollbar.HORIZONTAL,(int) (Settings.beta * 100),1,0,200);
	    sb1.addAdjustmentListener(this);
	    p.add(sb1);	 
	    
	    updateBetaLabel();
	    p.add(betaLabel);
	    
	    Button b3 = new Button("Automate");
	    b3.addActionListener(this);
	    p.add(b3);
	    
	    magCheckbox =  new Checkbox("Calculate Mag", Settings.calculateMag);
	    magCheckbox.addItemListener(this);
	    p.add(magCheckbox);
	    
	    p.add(magLabel);	    
	    
	    updateSweepsLabel(0);
	    p.add(sweepsLabel);	    
	    
	    add("North", p);
	    resize(Settings.appletWidth, Settings.appletHeight);
	}
	
	public void updateSweepsLabel(int x) {
	    sweepsLabel.setText("Sweeps = " + x);
	}
	
	public void updateBetaLabel() {
	    betaLabel.setText("Beta = " + DecimalString.getString(Settings.beta));
	    sb1.setValue((int) (Settings.beta * 100));
	}
	
	public void updateMagLabel(double x) {
	        magLabel.setText("M = " + DecimalString.getString(x));
	}

	public void paint(Graphics g) {
		displayLattice(g);
	}

	public void start() {
	    // Create AppletWorker thread and start it (doesn't start algorithm)
	    appletWorker = new AppletWorker(this);
	    // lattice reference is now set
	    
        if (Settings.calculateMag) {
            updateMagLabel(lattice.magnetisation());
        }
        
        // Start the appletWorker thread (calls its run() method)
	    appletWorker.start();
	}
	
	public void stop() {
		appletWorker.stop_thread();
	}

	public void actionPerformed(ActionEvent evt) {
	    if (evt.getActionCommand() == "Start") {
	        appletWorker.start_algorithm();
	    } else if (evt.getActionCommand() == "Pause") {
	        appletWorker.pause_algorithm();
	    } else if (evt.getActionCommand() == "Thermalise") {
	        appletWorker.thermalise();
	    } else if (evt.getActionCommand() == "Automate") {
	        appletWorker.automate();
	    } else if (evt.getActionCommand() == "Automate") {
	        System.out.println("Kill!");
	    }
	}
	
	public void adjustmentValueChanged(AdjustmentEvent evt) {
	    Settings.beta = (double) evt.getValue() * 0.01;
	    updateBetaLabel();
	}
	
	public void itemStateChanged(ItemEvent evt) {
	    Settings.calculateMag = magCheckbox.getState();

	    magLabel.setVisible(magCheckbox.getState());
	}

	public boolean mouseDown(Event evt, int x, int y) {
		appletWorker.sweep();
		return true;
	}

	public boolean mouseDrag(Event e, int x, int y) {
		appletWorker.sweep();	
	    return true;
	}
	
	public void setLatticeReference(Lattice l) {
	    lattice = l;
	}
	public void displayLattice(Graphics g){
	    for (int i=0;i<Settings.nx;i++) {
            for (int j=0; j<Settings.ny; j++) {
        	       if(lattice.l[j][i] == 1) {
	            	    g.setColor(Settings.colourUp);
	                } else {
	                    g.setColor(Settings.colourDown);
	                }
	                g.fillRect(Settings.appletBorderX + i*Settings.pointSizeX, 
	                        Settings.appletBorderY + j*Settings.pointSizeY, 
	                        Settings.pointSizeX, Settings.pointSizeY);
            }
        }	    
    }
};
