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

package cz.vutbr.feec.utko.fojtoval.krypto.sifry;

import cz.vutbr.feec.utko.fojtoval.krypto.funkce.Constant;
import java.util.List;

/**
 * Sifrovani a desifrovani pomoci afinni sifry.
 * @author fojtoval
 */
public class AfinniSifra extends Substituce {

    private char[] abecedasifr = null;
    private char[] abecedalus = null;

    /**
     * Sifrovani pomoci afinni sifry.
     * @param zdroj otevreny text
     * @return sifrovy text
     */
    @Override
    public String sifruj(String zdroj) {
        return Utils.zmenaTextuSubs (zdroj, this.abecedasifr, true);
    }

    /**
     * Nastaveni a kontorla parametru sifry.
     * @param parametr
     */
    @Override
    public void setParametr(Object parametr) {
            
        if (parametr == null) {
            throw (new NullPointerException ("Parametr je null!"));
        } else if (!(parametr instanceof List)) {
                   throw (new IllegalArgumentException ("Parametr neni List! (" + parametr + ")"));
               }

        List params = (List)parametr;

        if (params.size() != 2) {
            throw (new IllegalArgumentException ("Klic neni nema dva parametry! (pocet parametru: " + params.size() + ")"));
        } else if ((params.get (0) == null) || !(params.get (0) instanceof Integer)) {
                   throw (new IllegalArgumentException ("Neplatny prvni parametr! (" + params.get (0) + ")"));
                } else if ((params.get (1) == null) || !(params.get (1) instanceof Integer)) {
                           throw (new IllegalArgumentException ("Neplatny druhy parametr! (" + params.get (1) + ")"));
                       }

                  Integer afinni1 = (Integer)params.get(0);
                  Integer afinni2 = (Integer)params.get(1);

                  if (afinni1 < 1 || afinni1 > 25) {
		      throw (new IllegalArgumentException ("Prvni parametr neni ze spravneho rozsahu (1-25), zadano " + afinni1 + "."));
		  } else if (afinni2 < 0 || afinni1 > 25) {
		 	     throw (new IllegalArgumentException ("Druhy parametr neni ze spravneho rozsahu (0-25), zadano " + afinni2 + "."));
		         }

		    this.abecedasifr = Utils.afinni (Constant.abcd, afinni1, afinni2);
            this.abecedalus = Utils.afinniLus (Constant.abcd, afinni1, afinni2);

    }

    /**
     * Desifrovani afinni sifry.
     * @param zdroj sifrovy text
     * @return otevreny text
     */
    @Override
    public String lusti(String zdroj) {
        return Utils.zmenaTextuSubs (zdroj, this.abecedalus, true);
    }

    /**
     * Prevod desifrovaci abecedy na String.
     * @return nova abeceda
     */
    @Override
    public String abecedaLusToString() {
        return String.valueOf (this.abecedalus);
    }

    /**
     * Prevod sifrovaci abecedy na String.
     * @return nova abeceda
     */
    @Override
    public String abecedaSifToString() {
        return String.valueOf (this.abecedasifr);
    }

   
}
