/*
 *  Element that process ACL messages class
 *
 *  2004 - 2011 Frantisek Zboril jr.
 *  Faculty of Information Technology
 *  zborilf@fit.vutbr.cz
 *  http://www.fit.vutbr.cz/~zborilf
 *
 * (C) Brno University of Technology
 * Freeware licence VUT v Brně
 * https://wis.fit.vutbr.cz/FIT/db/dir.php/dr/VZ2007/externiInformace/softwareNaWebu/Priloha2_rozh23.pdf

 */

package jTMass.core.jTMassModel;

import jTMass.core.jTMassModel.jModelData.TModelData;
import jTMass.core.jTMassModel.jModelData.TSpeechAct;


public abstract class TACLElement extends TModelElement{
    
    // ===== NOT UNDERSTOOD - OBLIGATORY SPEECH ACT =====

    protected TSpeechAct generateNotUnderstood(TSpeechAct act){
        return(new TSpeechAct(TSpeechAct._SA_Not_Understood, act.getReceiver(),
               act.getSender(), TSpeechAct._CL_ALLL, "(NOU)"));
    }

    // ===== SPEECH ACT TYPES - DEFAULT ANSWER IS "NOT UNDERSTOOD" =====

    protected TSpeechAct processAcceptProposal(TSpeechAct act){
       return(generateNotUnderstood(act));
    }

    protected TSpeechAct processAgree(TSpeechAct act){
       return(generateNotUnderstood(act));
    }
    
    protected TSpeechAct processCancel(TSpeechAct act){
       return(generateNotUnderstood(act));
    }

    protected TSpeechAct processCfp(TSpeechAct act){
       return(generateNotUnderstood(act));
    }

    protected TSpeechAct processConfirm(TSpeechAct act){
       return(generateNotUnderstood(act));
    }

    protected TSpeechAct processDisconfirm(TSpeechAct act){
       return(generateNotUnderstood(act));
    }

    protected TSpeechAct processFailure(TSpeechAct act){
       return(generateNotUnderstood(act));
    }

    protected TSpeechAct processInform(TSpeechAct act){
       return(generateNotUnderstood(act));
    }

    protected TSpeechAct processInformIf(TSpeechAct act){
       return(generateNotUnderstood(act));
    }

    protected TSpeechAct processInformRef(TSpeechAct act){
       return(generateNotUnderstood(act));
    }

    protected TSpeechAct processNotUnderstood(TSpeechAct act){
       return(null); // no answer to NOU
    }

    protected TSpeechAct processPropagate(TSpeechAct act){
       return(generateNotUnderstood(act));
    }

    protected TSpeechAct processPropose(TSpeechAct act){
       return(generateNotUnderstood(act));
    }

    protected TSpeechAct processProxy(TSpeechAct act){
       return(generateNotUnderstood(act));
    }

    protected TSpeechAct processQueryIf(TSpeechAct act){
       return(generateNotUnderstood(act));
    }
    
    protected TSpeechAct processQueryRef(TSpeechAct act){
       return(generateNotUnderstood(act));
    }

    protected TSpeechAct processRefuse(TSpeechAct act){
       return(generateNotUnderstood(act));
    }

    protected TSpeechAct processRejectProposal(TSpeechAct act){
       return(generateNotUnderstood(act));
    }

    protected TSpeechAct processRequest(TSpeechAct act){
       return(generateNotUnderstood(act));
    }

    protected TSpeechAct processRequestWhen(TSpeechAct act){
       return(generateNotUnderstood(act));
    }

    protected TSpeechAct processRequestWhenever(TSpeechAct act){
       return(generateNotUnderstood(act));
    }

    protected TSpeechAct processSubscribe(TSpeechAct act){
       return(generateNotUnderstood(act));
    }

    // ===== PROCESSING SPEECH ACTS =====

    protected TSpeechAct processSpeechAct(TSpeechAct speechAct){
        TSpeechAct reply=null;
        int act=speechAct.getAct();
        switch (act){
            case TSpeechAct._SA_Accept_Proposal:
                reply=processAcceptProposal(speechAct);
                return(reply);
            case TSpeechAct._SA_Agree:
                reply=processAgree(speechAct);
                return(reply);
            case TSpeechAct._SA_Cancel:
                reply=processCancel(speechAct);
                return(reply);
            case TSpeechAct._SA_Call_for_Proposal:
                reply=processCfp(speechAct);
                return(reply);
            case TSpeechAct._SA_Confirm:
                reply=processConfirm(speechAct);
                return(reply);
            case TSpeechAct._SA_Disconfirm:
                reply=processDisconfirm(speechAct);
                return(reply);
            case TSpeechAct._SA_Failure:
                reply=processFailure(speechAct);
                return(reply);
            case TSpeechAct._SA_Inform:
                reply=processInform(speechAct);
                return(reply);
            case TSpeechAct._SA_Inform_If:
                reply=processInformIf(speechAct);
                return(reply);
            case TSpeechAct._SA_Inform_Ref:
                reply=processInformRef(speechAct);
                return(reply);
            case TSpeechAct._SA_Not_Understood:
                reply=processNotUnderstood(speechAct);
                return(reply);
            case TSpeechAct._SA_Propagate:
                reply=processPropagate(speechAct);
                return(reply);
            case TSpeechAct._SA_Propose:
                reply=processPropose(speechAct);
                return(reply);
            case TSpeechAct._SA_Proxy:
                reply=processProxy(speechAct);
                return(reply);
            case TSpeechAct._SA_Query_If:
                reply=processQueryIf(speechAct);
                return(reply);
            case TSpeechAct._SA_Query_Ref:
                reply=processQueryRef(speechAct);
                return(reply);
            case TSpeechAct._SA_Refuse:
                reply=processRefuse(speechAct);
                return(reply);
            case TSpeechAct._SA_Reject_Proposal:
                reply=processRejectProposal(speechAct);
                return(reply);
            case TSpeechAct._SA_Request:
                reply=processRequest(speechAct);
                return(reply);
            case TSpeechAct._SA_Request_When:
                reply=processRequestWhen(speechAct);
                return(reply);
            case TSpeechAct._SA_Request_Whenever:
                reply=processRequestWhenever(speechAct);
                return(reply);
            case TSpeechAct._SA_Subscribe:
                reply=processSubscribe(speechAct);
                return(reply);            
       }
       return(reply);
    }

    // ===== INPUT PROCESSING =====

    @Override
    public boolean acceptableInput(TModelData data){
        if(data!=null)
            if((data.getReceiver().contentEquals(getElementName())))
                if(data instanceof TSpeechAct)
                return(true);
        return(false);
   }

   @Override
   public int processInputs(){
        TSpeechAct reply;

        for(TModelData act:PInputDataBuffer){
        if(act instanceof TSpeechAct){
            reply=processSpeechAct((TSpeechAct)act);
            if(reply!=null)
                POutputDataBuffer.add(reply);
            }
      }
      PInputDataBuffer.clear();
      return(TModelElement._ESReady);
    }

   // ===== CONSTRUCTOR =====

    public TACLElement(String name, int nig, int nog, boolean element){
        super(name,nig,nog,element);
    }

}