package org.etsi.ttcn.tool.spirent.adapter;

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

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;

import com.testingtech.ttcn.logging.RTLoggingConstants;
import com.testingtech.ttcn.tri.TriAddressImpl;
import com.testingtech.ttcn.tri.TriMessageImpl;
import com.testingtech.ttcn.tri.TriStatusImpl;

import de.tu_berlin.cs.uebb.muttcn.runtime.RB;

public class TeRequiredImpl implements ITERequired {

	private RB rb;
	private Properties properties = new Properties();
	
	public TeRequiredImpl(RB rb) {
		this.rb = rb;
		try {
			properties.load(new FileInputStream("./etc/ta.properties"));
		} catch (IOException e) {
			System.err.println("Unable to load TA properties");
            e.printStackTrace();
        }
	}

	@Override
	public TriCommunicationTE getCommunicationTE() {
		return rb.getTriCommunicationTE();
	}

	@Override
	public String getTaParameter(String param) {
		return properties.getProperty(param, "");
	}

	@Override
	public TriAddress getTriAddress(byte[] message) {
		return new TriAddressImpl(message);
	}

	@Override
	public TriMessage getTriMessage(byte[] message) {
		return new TriMessageImpl(message); 
	}

	@Override
	public TriStatus getTriStatus(int statusCode) {
		return new TriStatusImpl(statusCode);
	}

	@SuppressWarnings("deprecation")
    @Override
	public TriStatus getTriStatus(int statusCode, String message) {
		switch(statusCode) {
		case TriStatus.TRI_OK:
			rb.getTciTLProvidedV321TT().tliRT("", System.nanoTime(), "", -1, null, RTLoggingConstants.RT_LOG_INFO, message);
			return  new TriStatusImpl();
		default:
			return  new TriStatusImpl(message);
		}
		
	}

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

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