/*
 *  Terminal printing services 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;

public class TSTerminalServices extends TASSynchronousService{

    /*
     * mt - print model time; param:
     * p - print; param: object
     * pl - println; params: object
     */

    TObject printTime(){
        TMass._Terminal.printlnSystemInfo(TMass.getSimulator().getModelTime().toString());
        return(null);
    }

    TObject printLn(TObject param){
        TMass._Terminal.printlnSystemInfo(param.getContent());
        return(null);
    }

    TObject print(TObject param){
        TMass._Terminal.printSystemInfo(param.getContent());
        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("mt"))
                            return(printTime());

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

                        if(op.contentEquals("pl"))
                            return(printLn(parameters.get(1)));
                        
                        if(op.contentEquals("pat")){
                             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";
                             TMass._Terminal.printlnSystemInfo(hod+":"+mz+min+":"+
                                     sz+sec+"."+time);
                             return(null);
                        }

                    }catch(Exception e){
                        TMass._Terminal.reportError("Wrong print parameter");
                        return(new TFail());
                    }
        return(new TFail());
    }

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

}
