package com.nsc.webench.calculators;

import com.nsc.webench.pojo.ComponentData;
import com.nsc.webench.pojo.DesignInfo;
import com.nsc.webench.pojo.ElSimulationData;
import com.nsc.webench.pojo.WebenchDesign;
import com.nsc.webench.pojo.CircuitData;



/***
 * Revision History: 
 * Author					Date				Description
 * ------					----				-----------
 * Swathi R H				26 OCT 2016		Initial commit
 
 **/

public class UCC28704_Calculator extends BuckCalculator {
	/*
	 * Constructor
	 */

	public UCC28704_Calculator(WebenchDesign webenchDesign) {
		super(webenchDesign);
	}
	
	/*
	 * Calculations
	 */

	public Double getCalculationValue(String calculationName) {
		return this.getNumericCalculationValue(calculationName, null);
	}

	// Retrieves the numeric value of a calculation

	public Double getNumericCalculationValue(String calculationName, ComponentData component) {
		// Initialize return value
		Double calculationValue = null;
		// Retrieve design info

		DesignInfo myDesign = this.getWebenchDesign().getMyDesign();
		if (calculationName.equals("DESIGN_VIN")) {
			//calculationValue = this.getNumericCalculationValue("F").toString() ;
			double VinMin = getDesignInputValue("VinMin"); 
			double VinMax = getDesignInputValue("VinMax"); 
			double Vout = getDesignInputValue("Vout1");
			double Iout =getDesignInputValue("Iout1");
			double FSW = getOpValValue("Frequency");
			if ( myDesign.getSimType().equals("Input Transient")){
				calculationValue = getDesignInputValue("VinMin");
			}
			else{
				// Avg of VinMin & VinMax
				calculationValue = (getDesignInputValue("VinMin") + getDesignInputValue("VinMax")) / 2;
			}
		}

		if (calculationName.equals("DESIGN_VOUT")){
			calculationValue = getDesignInputValue("Vout1");
		}

		if (calculationName.equals("DESIGN_IOUT")){
			calculationValue = getDesignInputValue("Iout1");
		}

		if (calculationName.equals("MASK_VDD_PULSE")){
			calculationValue = 0.0;
		}
		
		if (calculationName.equals("VDD_PULSE_DELAY")){
			double Frequency = getDesignInputValue("acFrequency");
			double Timeperiod = 1/Frequency;
			if ( myDesign.getSimType().equals("Startup")){
				calculationValue = 0.25*Timeperiod;
			}
			else {
				//calculationValue = 0.000001;
				calculationValue = 0.25*Timeperiod;
			}
		}
		
		if (calculationName.equals("SINE_AMPLITUDE")){
			double VinMin = getDesignInputValue("VinMin"); 
			double VinMax = getDesignInputValue("VinMax"); 
			calculationValue = 1.4142135623731*((VinMin + VinMax)/2);	//This is done as Input Transient sims is not present
		}
		
		
		
		
		if (calculationName.equals("TSTOP_STARTUP") ) {
			double Vinx = (getDesignInputValue("VinMin") + getDesignInputValue("VinMax")) / 2;
			
			   	 	if (Vinx <=200)
			{calculationValue = 25e-3 ;}
			else
			{             			
			    double FSW = getOpValValue("Frequency_");
				if(FSW > 40e3)
				 {calculationValue = 28e-3 ;}
				else 
				{calculationValue = 65e-3 ;}
				
			}		
		}

		// Run calculation from parent if calculation not found

		if (calculationValue == null) {
			calculationValue = super.getNumericCalculationValue(calculationName, component);
		} 
		else{
			// Store calculation
			String indexName = calculationName;
			this.getEvaluatedCalculationMap().put(indexName, calculationValue.toString());
		}
		return calculationValue;

	}



	public String getTextCalculationValue(String calculationName) {

		return this.getTextCalculationValue(calculationName, null);

	}

}