/**
 * @author      ETSI / STF462 / Alexandre Berge
 * @version     $URL: https://forge.etsi.org/svn/oneM2M/trunk/javasrc/tool/org/etsi/its/tool/testingtech/Its_CodecProvider.java $
 *              $Id: Its_CodecProvider.java 141 2016-10-17 14:55:22Z berge $
 */
package org.etsi.ttcn.tool.spirent.codec;

import org.etsi.ttcn.codec.core.ITciCDWrapper;
import org.etsi.ttcn.codec.core.TciCDWrapperFactory;
import org.etsi.ttcn.codec.core.MainCodec;
import org.etsi.ttcn.tci.TciCDProvided;
import org.etsi.ttcn.tci.Type;
import org.etsi.ttcn.tci.Value;
import org.etsi.ttcn.tri.TriMessage;

import com.testingtech.ttcn.extension.CodecProvider;
import com.testingtech.util.plugin.PluginInitException;

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

/** This is the main entry point for Its codec
 */
public class Its_CodecProvider implements CodecProvider {

    /** 
     * This method provides the codec associated to the specified encoding identifier
     * 
     * @param rb TTwb Runtime reference
     * @param encodingName The name of the encoding, specified by the TTCN-3 key words 'with/encode'
     * @return The codec associated to the specified encoding identifier
     */
    @Override
    public TciCDProvided getCodec(RB rb, String encodingName) throws PluginInitException {
        
        return new Codec(rb);
    }

    private class Codec implements TciCDProvided {

        public Codec(RB rb) {
        	TciCDWrapperFactory.getInstance().setImpl(new TciCDWrapper(rb));
        }

        @Override
        public Value decode(TriMessage message, Type decodingHypothesis) {
            MainCodec codec = new MainCodec((ITciCDWrapper) TciCDWrapperFactory.getTciCDInstance());
            Value v = null;
            try {
                v = codec.triDecode(message, decodingHypothesis);
            }
            catch(Exception e) {
                e.printStackTrace();
            }
            return v;
        }

        @Override
        public TriMessage encode(Value value) {
            MainCodec codec = new MainCodec((ITciCDWrapper) TciCDWrapperFactory.getTciCDInstance());
            TriMessage m = null;
            try {
                m = codec.triEncode(value);
            }
            catch(Exception e) {
                e.printStackTrace();
            }
            return m;
        }                
    }
}
