package network;

import java.text.DecimalFormat;
import java.text.DecimalFormat;
import javax.swing.JOptionPane;

/**
 * This class provides functionality that aids in the calculation of Shannon's
 * Theorem.
 * 
 * 
 * @author 
 * @version 1.8.0 2017/02/11
 */
public class ShannonsTheorem {

	private ShannonsModel Smodel;

	/**
	 * Default constructor
	 */
	public ShannonsTheorem() {
	}

	/**
	 * It sets ShannonsModel field.
	 * 
	 * @param model
	 *            This is for ShannonsModel filed.
	 */
	public ShannonsTheorem(ShannonsModel model) {
		this.Smodel = model;
	}

	/**
	 * Entry point "main()". This main method will prompt with JOptionPane for
	 * the certain data required for ShannonsModel.
	 * 
	 * @param args
	 *            Standard command line parameters (arguments) as a string
	 *            array.
	 */
	public static void main(String args[]) {

		// Instantiate ShannonTheorem and ShannonModel for MVC model.
		ShannonsModel model = new ShannonsModel();
		ShannonsTheorem shannon = new ShannonsTheorem(model);
		DecimalFormat df = new DecimalFormat("######0.00");

		while (true) {
			try {
				/* read in bandwidth from user as a string */
				shannon.setBandwidth(Double.parseDouble(JOptionPane.showInputDialog("Enter the bandwidth")));

				/* read in signal power S from user as a string */
				shannon.setSignalToNoise(Double.parseDouble(JOptionPane.showInputDialog("Enter the Signal to noise")));

				// Display Maximum Data rate and prompt
				JOptionPane.showMessageDialog(null, "Maximum Data rate is " + df.format(shannon.getMaximumDataRate()),
						"Results", JOptionPane.PLAIN_MESSAGE);
				// store answer about "Y" or "N" from user
				String answer = JOptionPane
						.showInputDialog("Do you want to continue running this program again? \"Y\" or \"N\" ");
				if (answer.equals("N") || answer.equals("n")) {
					break;
				}

				// When Input data is character or negative value, it will catch
				// the number format exception.
			} catch (NumberFormatException e) {
				String answer1 = JOptionPane.showInputDialog(
						"Wrong input. Do you want to continue running this program again? Enter \"Y\" or \"N\" ");
				if (answer1.equals("N") || answer1.equals("n")) {
					break;
				}
				// Once there is no value inside a String answer2, it will catch
				// the Null pointer exception.
			} catch (NullPointerException e) {
				String answer2 = JOptionPane.showInputDialog(
						"You clicked cancel. Do you want to finish this program? Enter \"Y\" or \"N\" ");
				if (answer2.equals("y") || answer2.equals("Y")) {
					break;
				}

			}
		}
	}

	/**
	 * Returns the private double bandwidth field.
	 * 
	 * @return bandwidth
	 */
	public double getBandwidth() {
		return Smodel.getBandwidth();
	}

	/**
	 * Returns the value of getMaximumDataRate(double, double).
	 * 
	 * @return getMaximumDateRate(double, double)
	 */
	public double getMaximumDataRate() {
		return Smodel.getMaximumDataRate();
	}

	/**
	 * Returns the value of the private double signalToNoise field
	 * 
	 * @return signalToNoise
	 */
	public double getSignalToNoise() {
		return Smodel.getSignalToNoise();
	}

	/**
	 * Sets the value of the private bandwidth field.
	 * 
	 * @param h Bandwidth value
	 */
	public void setBandwidth(double h) {
		this.Smodel.setBandwidth(h);
	}

	/**
	 * Sets the value of the private signalToNoise field
	 * 
	 * @param snr Signal to noise value
	 */
	public void setSignalToNoise(double snr) {
		this.Smodel.setSignalToNoise(snr);
	}

	/**
	 * Displays information based on Shannon's Theorem.
	 */
	public String toString() {
		return Smodel.toString();
	}

}// End of the ShannonsTheorem class
