/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package usda.weru.erosion;
import javax.swing.JOptionPane;
import usda.weru.util.*;


/**
 *
 * @author wjr
 */
public class Weibull extends usda.weru.erosion.gui.Weibull_n {
    private static final long serialVersionUID = 1L;
    
    FormValidator fv = null;

	/**
	 *
	 * @param fv
	 */
	public void setFormValidator(FormValidator fv) {
        this.fv = fv;
        if (fv != null) fv.validateForm(this);
    }

	/**
	 *
	 * @param parent
	 * @param weibullC
	 * @param weibullK
	 * @param weibullCalm
	 * @param numIntv
	 */
	public Weibull(java.awt.Frame parent, StringBuffer weibullC,
            StringBuffer weibullK, StringBuffer weibullCalm, int numIntv) {
        super(parent, true);
        JTF_calmFraction.setText(weibullCalm.toString().trim());
        JTF_weibullC.setText(weibullC.toString().trim());
        JTF_weibullK.setText(weibullK.toString().trim());
        JCB_intervals.setSelectedIndex(numIntv);
//        if (fv != null) fv.validateForm(this);
//        initComponents();
    }

	/**
	 *
	 * @param evt
	 */
	@Override
    protected void JTF_calmFractionActionPerformed(java.awt.event.ActionEvent evt) {
        if (fv != null) fv.validateForm(this);
    }

	/**
	 *
	 * @param evt
	 */
	@Override
    protected void JTF_calmFractionFocusLost(java.awt.event.FocusEvent evt) {
        if (fv != null) fv.validateForm(this);
    }

	/**
	 *
	 * @param evt
	 */
	@Override
    protected void JTF_weibullKActionPerformed(java.awt.event.ActionEvent evt) {
        if (fv != null) fv.validateForm(this);
    }

	/**
	 *
	 * @param evt
	 */
	@Override
    protected void JTF_weibullKFocusLost(java.awt.event.FocusEvent evt) {
        if (fv != null) fv.validateForm(this);
    }

	/**
	 *
	 * @param evt
	 */
	@Override
    protected void JTF_weibullCActionPerformed(java.awt.event.ActionEvent evt) {
        if (fv != null) fv.validateForm(this);
    }

	/**
	 *
	 * @param evt
	 */
	@Override
    protected void JTF_weibullCFocusLost(java.awt.event.FocusEvent evt) {
        if (fv != null) fv.validateForm(this);
    }

	/**
	 *
	 * @param evt
	 */
	@Override
    protected void JCB_intervalsActionPerformed(java.awt.event.ActionEvent evt) {
    }

	/**
	 *
	 * @param evt
	 */
	@Override
    protected void formWindowClosing(java.awt.event.WindowEvent evt) {                                   
        rtnFlg = false;
    }

	/**
	 *
	 * @param evt
	 */
	@Override
        protected void JTF_calmFractionFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_JTF_calmFractionFocusGained
            ((javax.swing.JTextField) evt.getComponent()).selectAll();
    }//GEN-LAST:event_JTF_calmFractionFocusGained

	/**
	 *
	 * @param evt
	 */
	@Override
    protected void JTF_weibullCFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_JTF_weibullCFocusGained
            ((javax.swing.JTextField) evt.getComponent()).selectAll();
    }//GEN-LAST:event_JTF_weibullCFocusGained

	/**
	 *
	 * @param evt
	 */
	@Override
    protected void JTF_weibullKFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_JTF_weibullKFocusGained
            ((javax.swing.JTextField) evt.getComponent()).selectAll();
    }//GEN-LAST:event_JTF_weibullKFocusGained

	/**
	 *
	 */
	public boolean rtnFlg = true;
    String windSpeeds = null;
    int[] stepArr = {24, 48, 72, 96, 144, 288};

	/**
	 *
	 * @param evt
	 */
	@Override
    protected void JB_OKActionPerformed(java.awt.event.ActionEvent evt) {
        if(!fv.validateForm(this)){
            JOptionPane.showMessageDialog(this, "Weibull parameters out of range.", "Error", JOptionPane.ERROR_MESSAGE);
            return;
        }
        double weiCalm = Double.parseDouble(this.JTF_calmFraction.getText());
        double weiC = Double.parseDouble(this.JTF_weibullC.getText());
        double weiK = Double.parseDouble(this.JTF_weibullK.getText());
        
        int steps = stepArr[this.JCB_intervals.getSelectedIndex()];
        
        System.out.printf("%f %f %f %d\n", weiCalm, weiC, weiK, steps);
        
        double[] weiArr = calcWeibullSpeeds(weiC, weiK, weiCalm, steps);
        
        StringBuffer sb = new StringBuffer();
        for (int idx = 0; idx < weiArr.length; idx++) {
            sb.append(String.format("%7.3f", weiArr[idx]));
//            sb.append(((idx % 6) == 5) ? "\n" : " ");
        }
        
        windSpeeds = sb.toString();
        System.out.println("wS " + windSpeeds);
//        rtnFlg = true;
        dispose();
    }

	/**
	 *
	 * @param evt
	 */
	@Override
    protected void JB_CancelActionPerformed(java.awt.event.ActionEvent evt) {
        rtnFlg = false;
        dispose();
    }

    private static double[] calcWeibullSpeeds(double weiC, double weiK, double weiCalm, int nSteps) {

        double step = nSteps;

        double[] f = new double[nSteps];
        double[] tmpArr = new double[nSteps];
        for (int idx = 0; idx < nSteps; idx++) {
            f[idx] = (1.0 / (2.0 * step)) + idx / step + 0.3 / (step * weiK);
            if (f[idx] < weiCalm) {
                f[idx] = weiCalm;
            }
            tmpArr[idx] = (-Math.log((1.0 - f[idx]) / (1.0 - weiCalm)));
            tmpArr[idx] = weiC * Math.pow(tmpArr[idx], 1.0 / weiK);
        }

        double[] rtnArr = new double[nSteps];

        int jdx = 0;
        for (int idx = 0; idx < nSteps / 2; idx++, jdx += 2) {
            rtnArr[idx] = tmpArr[jdx];
        }
        jdx = nSteps - 1;
        for (int idx = nSteps / 2; idx < nSteps; idx++, jdx -= 2) {
            rtnArr[idx] = tmpArr[jdx];
        }
        return rtnArr;
    }
}
