package defaultPackage;

//package lt.ktu.gmj.tasks;

import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.JTabbedPane;

import lt.ktu.gmj.Method;
import lt.ktu.gmj.Task;
import lt.ktu.gmj.tasks.MePortfolio;
import lt.ktu.gmj.ui.ResultSet;

public class Counter extends Thread {
	int Iterations[], BankLength, ShareLength, InsuranceLength;

	JProgressBar wProgressBar;

	JLabel wALabel[];

	double AllBankrupt[], AllDividends[], x[], ObjectValue[], Uf[];

	double Resours;

	int InType;

	String mMethod;

	Vector wItVector, wFVector;

	int Length;

	JTabbedPane wTabbedPane;

	JButton w1Button, w2Button, w3Button;

	Method method;

	Task task;

	MeGraphics mGraphics;

	Counter(int Iterations[], int InType, double Resours, double AllBankrupt[],
			double AllDividends[], JProgressBar wProgressBar, JLabel wALabel[],
			JTabbedPane wTabbedPane, JButton w1Button, JButton w2Button,
			JButton w3Button, int BankLength, int ShareLength,
			int InsuranceLength, double ObjectValue[], String mMethod,
			MeGraphics mGraphics, double Uf[]) {
		this.Iterations = Iterations;
		this.InType = InType;
		this.Resours = Resours;
		this.AllBankrupt = AllBankrupt;
		this.AllDividends = AllDividends;
		this.wALabel = wALabel;
		this.wProgressBar = wProgressBar;
		this.wTabbedPane = wTabbedPane;
		this.w1Button = w1Button;
		this.w2Button = w2Button;
		this.w3Button = w3Button;
		this.BankLength = BankLength;
		this.ShareLength = ShareLength;
		this.InsuranceLength = InsuranceLength;
		this.ObjectValue = ObjectValue;
		this.mMethod = mMethod;
		this.mGraphics = mGraphics;
		this.Uf = Uf;
	}

	// ********************************************************************

	public void run() {
		Length = AllDividends.length;
		if (Iterations[0] > Iterations[1])
			wProgressBar.setMaximum(Iterations[0]);
		if (mMethod.equals("Globt"))
			wProgressBar.setMaximum((Iterations[0] * 3) + 1);
		else
			wProgressBar.setMaximum(Iterations[0]);
		wProgressBar.setValue(0);
		for (int i = 0; i <= Length + 2; i++)
			wALabel[i].setText("0");

		// ----------------------------------------------------

		mMethod = "lt.ktu.gmj.methods." + mMethod;
		try {
			method = (Method) Class.forName(mMethod).newInstance();
		} catch (Exception exc) {
			System.out.println("Error: class " + mMethod + " not found.");
			return;
		}
		try {
			task = (Task) new MePortfolio();
			task.domain().CreateArray(Length);
			task.setParameters(Iterations, InType, Resours, AllBankrupt,
					AllDividends, wProgressBar, wALabel, wTabbedPane, w1Button,
					w2Button, BankLength, ShareLength, InsuranceLength,
					ObjectValue, mMethod, Uf);
			task.CounterPoint(this);
			task.Data(wItVector, wFVector);
		} catch (Exception exc) {
			System.out
					.println("Error: class lt.ktu.gmj.tasks.MePortfolio not found.");
			return;
		}
		ResultSet results = new ResultSet(method, task);
		results.newLog();
		if (mGraphics != null) {
			int IT;
			if (mMethod.equals("lt.ktu.gmj.methods.Globt"))
				IT = (Iterations[0] * 3) + 1;
			else
				IT = Iterations[0];
			mGraphics.setIterations(IT);
			FrameC(mGraphics);
		}
		StartedCounter();
		try {
			sleep(200); // Pause
		} catch (InterruptedException ie) {
		}
		;
		try {
			method.setParameters(Iterations[0], Iterations[1]);
			method.run(results, task); // Run method and task
		} catch (Exception exception) {
			exception.printStackTrace();
		}
		Finished();
	}

	// ------------------------------------------

	public void Data(Vector wItVector, Vector wFVector) {
		this.wItVector = wItVector;
		this.wFVector = wFVector;
	}

	public void FrameD() {
		task.FrameD();
	}

	public void FrameC(MeGraphics mGraphics) {
		task.FrameC(mGraphics);
	}

	// -----------------------------------------------------------------
	// -------------- Thread Control -----------------------------------

	void Finished() // Inside thread
	{
		wTabbedPane.setEnabledAt(0, true);
		if (InType == 4)
			wTabbedPane.setEnabledAt(1, true);
		wTabbedPane.setEnabledAt(2, true);
		wTabbedPane.setEnabledAt(3, true);
		wTabbedPane.setEnabledAt(4, true);
		w2Button.setEnabled(false);
		w2Button.setToolTipText(null);
		w1Button.setText("Start");
		w1Button.setToolTipText("Start investment count");
	}

	public void FinishedCounter() // Outside Thread
	{
		task.FinishedCounter();
	}

	void StartedCounter() {
		wTabbedPane.setEnabledAt(0, false);
		wTabbedPane.setEnabledAt(1, false);
		wTabbedPane.setEnabledAt(2, false);
		wTabbedPane.setEnabledAt(3, false);
		wTabbedPane.setEnabledAt(4, false);
		w1Button.setText("Pause");
		w1Button.setToolTipText("Pause investment count");
		w1Button.setEnabled(true);

		w2Button.setToolTipText("Stop investment count");
		w2Button.setEnabled(true);

		w3Button.setToolTipText("Convergence grahpic");
		w3Button.setEnabled(true);
	}

	public void SuspendCounter() // Outside Thread
	{
		task.SuspendCounter();
	}

	public void ResumeCounter() // Outside Thread
	{
		task.ResumeCounter();
	}

}