/*
 *  Store a parameter in active register 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.jTMassPlatform.jTServices;

import jTMass.TMass;
import jTMass.core.jALLL.jTObjects.TAtom;
import jTMass.core.jALLL.jTObjects.TFail;
import jTMass.core.jALLL.jTObjects.TList;
import jTMass.core.jALLL.jTObjects.TObject;
import jTMass.core.jTMassPlatform.TPlatform;
import java.util.Random;

public class TSStandardServices extends TASSynchronousService {

    /*
     * r - read variable; param: variable name
     * w - write variable; params: variable name, object
     * s - set register; param: object
     * c - compare two values; params: object1, object2
     * mt - model time;
     * ts - terminate service; string - serviceName
     */

    TObject read(TPlatform platform, TObject varName){
        String name=varName.getContent();
        TObject result=platform.getVariable(name);
        if(result!=null)
            return(result);
        else
            return(new TFail());
    }

    TObject write(TPlatform platform, TObject varName, TObject value){
        String name=varName.getContent();
        platform.addVariable(name, value);
        return(null);
    }

    public static TObject terminateService(String serviceName, TPlatform platform){
        platform.getPCU().terminateService(serviceName);
        return(null);
    }

    @Override
    public TObject processService(TList parameters, String clientName, String serviceName) {
        super.processService(parameters, clientName, serviceName);
            if(parameters.size()>=1)
                if(parameters.get(0) instanceof TAtom)
                    try{
                        String op=parameters.get(0).getContent();

                        if(op.contentEquals("r"))
                            return(read(PPlatform, parameters.get(1)));

                        if(op.contentEquals("w"))
                            return(write(PPlatform, parameters.get(1),parameters.get(2)));

                        if(op.contentEquals("s"))
                            return(parameters.get(1));

                        if(op.contentEquals("c")){
                            if(parameters.get(1).getContent().contentEquals(parameters.get(2).getContent()))
                                return(null);
                            else
                                return(new TFail());
                        }

                        if(op.contentEquals("mt"))
                            return(new TAtom(TMass.getSimulator().getModelTime().toString()));

                        if(op.contentEquals("rd")){
                            Random random=new Random();
                            Integer range=Integer.valueOf(parameters.get(1).getContent());
                            Long val=Math.abs(random.nextLong()%range);
                            return(new TAtom(val.toString()));
                        }

                        if(op.contentEquals("rt")){
                             Long time=System.currentTimeMillis();
                             Long sec=time/1000;
                             Long min=sec/60;
                             Long hod=min/60;
                             time=time%1000;
                             sec=sec%60;
                             min=min%60;
                             hod=hod%24;
                             String sz="",mz="";
                             if(sec<10) sz="0";
                             if(min<10) mz="0";
                             TList result=new TList();
                             result.add(new TAtom(hod.toString()));
                             result.add(new TAtom(mz.concat(min.toString())));
                             result.add(new TAtom(sz.concat(sec.toString())));
                             result.add(new TAtom(time.toString()));
                             return(result);
                        }

      /*                  if(op.contentEquals("ts"))
                            return(terminateService(parameters.get(1).getContent(),PPlatform));
      */
                    }catch(Exception e){
                        TMass._Terminal.reportError("Wrong standard services parameter");
                    }
        return(new TFail());
    }

    public TSStandardServices(TPlatform platform){
        super(platform);
    }

}