/**
 * @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 java.util.logging.Logger;

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 {

    TriFactory _factory = TriProvider.getInstance().getTriFactory();
    
//    private TriTciChannel _tciFactory = TriTciChannel.getInstance();
    
    private final PluginAdapter _ta;
    
    private static Properties _properties = new Properties();
    
    protected final static Logger _logger = Logger.getLogger("org.etsi.its");
    
    public TeRequiredImpl(PluginAdapter ta) {
        _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 = _factory.createAddress();
        address.setEncodedAddress(message);
        return address;
    }

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

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

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

    @Override
    public void logDebug(String debugMessage) {
        _logger.info(debugMessage);
    }

    @Override
    public void logError(String errorMessage) {
        _logger.severe(errorMessage);
    }

} // End of class TeRequiredImpl
