/**
 * @author    STF 424_ITS_Test_Platform
 * @version    $id$
 */
package org.etsi.ttcn.tool.elvior.adapter;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

import org.elvior.ttcn.tritci.TriFactory;
import org.elvior.ttcn.tritci.TriProvider;
import org.etsi.ttcn.adapter.core.ITERequired;
import org.etsi.ttcn.tri.TriAddress;
import org.etsi.ttcn.tri.TriCommunicationTE;
import org.etsi.ttcn.tri.TriMessage;
import org.etsi.ttcn.tri.TriStatus;

public class TeRequiredImpl implements ITERequired {

    private TriFactory triFactory = TriProvider.getInstance().getTriFactory();
    private final PluginAdapter ta;
    private static Properties properties = new Properties();
    
    public TeRequiredImpl(PluginAdapter ta) {
        this.ta = ta;
        
        // Load TA settings
        try {
        	properties.load(new FileInputStream("./etc/ta.properties"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public TriCommunicationTE getCommunicationTE() {
        return (TriCommunicationTE)ta;
    }

    /**
     * @desc Retrieve TA settings from external file
     * It does not work with TestcatT3 because of TCI is only available during test case execution (from triExecuteTestCase method call till triEndTestCase method call)
     */
    @Override
    public String getTaParameter(String param) { 
        return properties.getProperty(param, "");
    }

    @Override
    public TriAddress getTriAddress(byte[] message) {
        TriAddress address = triFactory.createAddress();
        address.setEncodedAddress(message);
        return address;
    }

    @Override
    public TriMessage getTriMessage(byte[] message) {
        TriMessage msg = triFactory.createMessage();
        msg.setEncodedMessage(message);
        msg.setNumberOfBits(message.length * Byte.SIZE);
        return msg;
    }

    @Override
    public TriStatus getTriStatus(int statusCode) {
        TriStatus status = triFactory.createErrorStatus();
        status.setStatus(statusCode);
        return status;
    }

    @Override
    public TriStatus getTriStatus(int statusCode, String message) {
        return getTriStatus(statusCode);
    }

    @Override
    public void logDebug(String debugMessage) {
		System.err.println(debugMessage);
    }

    @Override
    public void logError(String errorMessage) {		
    	System.err.println(errorMessage);
    }

} // End of class TeRequiredImpl
