package net.krakendev.util.temperature;

import java.text.DecimalFormat;

import net.krakendev.util.temperature.Temperature.tempUnit;


public abstract class AbstractTemperature {
	
	private AbstractTemperature() {}
	
	public String toString(double value, tempUnit unit) {
		char unitChar = ' ';
		DecimalFormat df = new DecimalFormat("0.################");
		
		switch (unit) {
			case FARENHEIT: unitChar = 'F';
				break;
			case CELCIUS: unitChar = 'C';
				break;
			case KELVIN: unitChar = 'K';
				break;
			default: return value + "°";
		}
		
		return df.format(value) + "°" + unitChar;
	}
	
	public double toFarenheit(double value, char unit) {
		if (unit == 'F' || unit == 'f') {
			return value;
		} else if (unit == 'C' || unit == 'c') {
			return 9d/5d * (value + 32);
		} else if (unit == 'K' || unit == 'k') {
			return 9d/5d * (value - 32) + 273;
		} else {
			return value;
		}
	}
	public double toFarenheit(double value, String unit) {
		if (unit.equalsIgnoreCase("farenheit") || unit.equalsIgnoreCase("f")) {
			return value;
		} else if (unit.equalsIgnoreCase("celcius") || unit.equalsIgnoreCase("c")) {
			return 9d/5d * (value + 32);
		} else if (unit.equalsIgnoreCase("kelvin") || unit.equalsIgnoreCase("k")) {
			return 9d/5d * (value - 32) + 273;
		} else {
			return value;
		}
	}
	public double toFarenheit(double value, tempUnit unit) {
		switch (unit) {
			case FARENHEIT: return value;
			case CELCIUS: return 9d/5d * (value + 32);
			case KELVIN: return 9d/5d * (value - 32) + 273;
			default: return value;
		}
	}
	
	public double toCelcius(double value, tempUnit unit) {
		switch (unit) {
			case FARENHEIT: return 5d/9d * (value - 32);
			case CELCIUS: return value;
			case KELVIN: return value - 273.15;
			default: return value;
		}
	}
	public double toCelcius(double value, char unit) {
		if (unit == 'F' || unit == 'f') {
			return 5d/9d * (value - 32);
		} else if (unit == 'C' || unit == 'c') {
			return value;
		} else if (unit == 'K' || unit == 'k') {
			return value - 273.15;
		} else {
			return value;
		}
	}
	public double toCelcius(double value, String unit) {
		if (unit.equalsIgnoreCase("farenheit") || unit.equalsIgnoreCase("f")) {
			return 5d/9d * (value - 32);
		} else if (unit.equalsIgnoreCase("celcius") || unit.equalsIgnoreCase("c")) {
			return value;
		} else if (unit.equalsIgnoreCase("kelvin") || unit.equalsIgnoreCase("k")) {
			return value - 273.15;
		} else {
			return value;
		}
	}
	public double toKelvin(double value, char unit) {
		if (unit == 'F' || unit == 'f') {
			return (5d/9d * (value - 32)) + 273.15;
		} else if (unit == 'C' || unit == 'c') {
			return value + 273.15;
		} else if (unit == 'K' || unit == 'k') {
			return value;
		} else {
			return value;
		}
	}
	public double toKelvin(double value, String unit) {
		if (unit.equalsIgnoreCase("farenheit") || unit.equalsIgnoreCase("f")) {
			return (5d/9d * (value - 32)) + 273.15;
		} else if (unit.equalsIgnoreCase("celcius") || unit.equalsIgnoreCase("c")) {
			return value + 273.15;
		} else if (unit.equalsIgnoreCase("kelvin") || unit.equalsIgnoreCase("k")) {
			return value;
		} else {
			return value;
		}
	}
	public double toKelvin(double value, tempUnit unit) {
		switch (unit) {
			case FARENHEIT: return (5d/9d * (value - 32)) + 273.15;
			case CELCIUS: return value + 273.15;
			case KELVIN: return value;
			default: return value;
		}
	}
	
	public boolean isValid(double value, char unit) {
		if (value < -459.67 && (unit == 'F' || unit == 'f')) {
			return false;
		} else if (value < -273.15 && (unit == 'C' || unit == 'c')) {
			return false;
		} else if (value < 0 && (unit == 'K' || unit == 'k')) {
			return false;
		} else {
			return true;
		}
	}
	public boolean isValid(double value, String unit) {
		if (value < -459.67 && (unit.equalsIgnoreCase("farenheit") || unit.equalsIgnoreCase("f"))) {
			return false;
		} else if (value < -273.15 && (unit.equalsIgnoreCase("celcius") || unit.equalsIgnoreCase("c"))) {
			return false;
		} else if (value < 0 && (unit.equalsIgnoreCase("kelvin") || unit.equalsIgnoreCase("k"))) {
			return false;
		} else {
			return true;
		}
	}
	public boolean isValid(double value, tempUnit unit) {
		if (value < -459.67 && unit == tempUnit.FARENHEIT) {
			return false;
		} else if (value < -273.15 && unit == tempUnit.CELCIUS) {
			return false;
		} else if (value < 0 && unit == tempUnit.KELVIN) {
			return false;
		} else {
			return true;
		}
	}
}