/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cryptolearning;

import com.sun.rave.web.ui.appbase.AbstractFragmentBean;
import com.sun.webui.jsf.component.StaticText;
import javax.faces.FacesException;

import core.ExMath;
import core.ExtraException;
import logarithm.PrimeGen;

import javax.faces.component.html.HtmlInputText;
import javax.swing.JOptionPane;

/**
 * <p>Fragment bean that corresponds to a similarly named JSP page
 * fragment.  This class contains component definitions (and initialization
 * code) for all components that you have defined on this fragment, as well as
 * lifecycle methods and event handlers where you may add behavior
 * to respond to incoming events.</p>
 *
 * @version ElGalman.java
 * @version Created on 28.3.2009, 11:26:55
 * @author jirka
 */

public class ElGalman extends AbstractFragmentBean {
    // <editor-fold defaultstate="collapsed" desc="Managed Component Definition">

    /**
     * <p>Automatically managed component initialization. <strong>WARNING:</strong>
     * This method is automatically generated, so any user-specified code inserted
     * here is subject to being replaced.</p>
     */
    private void _init() throws Exception {
    }
    private HtmlInputText txtN = new HtmlInputText();

    public HtmlInputText getTxtN() {
        return txtN;
    }

    public void setTxtN(HtmlInputText hit) {
        this.txtN = hit;
    }
    private HtmlInputText txtB = new HtmlInputText();

    public HtmlInputText getTxtB() {
        return txtB;
    }

    public void setTxtB(HtmlInputText hit) {
        this.txtB = hit;
    }
    private HtmlInputText txtK = new HtmlInputText();

    public HtmlInputText getTxtK() {
        return txtK;
    }

    public void setTxtK(HtmlInputText hit) {
        this.txtK = hit;
    }
    private StaticText labY = new StaticText();

    public StaticText getLabY() {
        return labY;
    }

    public void setLabY(StaticText st) {
        this.labY = st;
    }
    private StaticText labPublicKey = new StaticText();

    public StaticText getLabPublicKey() {
        return labPublicKey;
    }

    public void setLabPublicKey(StaticText st) {
        this.labPublicKey = st;
    }
    private StaticText labPrivateKey = new StaticText();

    public StaticText getLabPrivateKey() {
        return labPrivateKey;
    }

    public void setLabPrivateKey(StaticText st) {
        this.labPrivateKey = st;
    }
    // </editor-fold>

    public ElGalman() {
    }

    /**
     * <p>Callback method that is called whenever a page containing
     * this page fragment is navigated to, either directly via a URL,
     * or indirectly via page navigation.  Override this method to acquire
     * resources that will be needed for event handlers and lifecycle methods.</p>
     * 
     * <p>The default implementation does nothing.</p>
     */
    @Override
    public void init() {
        // Perform initializations inherited from our superclass
        super.init();
        // Perform application initialization that must complete
        // *before* managed components are initialized
        // TODO - add your own initialiation code here
        
        
        // <editor-fold defaultstate="collapsed" desc="Visual-Web-managed Component Initialization">
        // Initialize automatically managed components
        // *Note* - this logic should NOT be modified
        try {
            _init();
        } catch (Exception e) {
            log("Page1 Initialization Failure", e);
            throw e instanceof FacesException ? (FacesException) e: new FacesException(e);
        }
        
        // </editor-fold>
        // Perform application initialization that must complete
        // *after* managed components are initialized
        // TODO - add your own initialization code here
    }

    /**
     * <p>Callback method that is called after rendering is completed for
     * this request, if <code>init()</code> was called.  Override this
     * method to release resources acquired in the <code>init()</code>
     * resources that will be needed for event handlers and lifecycle methods.</p>
     * 
     * <p>The default implementation does nothing.</p>
     */
    @Override
    public void destroy() {
    }

    /**
     * <p>Return a reference to the scoped data bean.</p>
     *
     * @return reference to the scoped data bean
     */
    protected SessionBean1 getSessionBean1() {
        return (SessionBean1) getBean("SessionBean1");
    }

    /**
     * <p>Return a reference to the scoped data bean.</p>
     *
     * @return reference to the scoped data bean
     */
    protected ApplicationBean1 getApplicationBean1()
    {
        return (ApplicationBean1) getBean("ApplicationBean1");
    }

    public String button1_action() {
        Integer n, b, k, Y;
        try
        {
            n = Integer.parseInt(txtN.getValue().toString());
            b = Integer.parseInt(txtB.getValue().toString());
            k = Integer.parseInt(txtK.getValue().toString());

            if (!PrimeGen.isPrime(n))
                throw new ExtraException("Číslo n není prvočíslo.");
            
            if(k<1 || k>n-2)
                throw new ExtraException("Číslo k musí být: 0 < k < n-1");
            
            // pocita Y = b^k mod n
            Y =(int) (ExMath.myExp(b, k) % n);
            Double bNaK = ExMath.myExp(b, k);
            
            String PublicKey = n.toString() + ", " + b.toString() + ", " + Y.toString();
            
            labY.setText(Y.toString());
            labPublicKey.setText(PublicKey);
            labPrivateKey.setText(k.toString());

        }
        catch (ExtraException e)
        {
            JOptionPane.showMessageDialog(null, e.toString());
        }
        catch (Exception e)
        {
            JOptionPane.showMessageDialog(null, "Chybná vstupní data.");
        }
        return null;
    }

}
