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

import java.math.BigInteger;

import org.elvior.ttcn.tritci.IntegerValueEx;
import org.elvior.ttcn.tritci.TciProvider;
import org.etsi.codec.ITciCDWrapper;
import org.etsi.ttcn.tci.FloatValue;
import org.etsi.ttcn.tci.IntegerValue;
import org.etsi.ttcn.tci.OctetstringValue;
import org.etsi.ttcn.tci.TciCDRequired;
import org.etsi.ttcn.tci.Type;
import org.etsi.ttcn.tci.Value;

/** This class implements the ITciCDWrapper interface
 * 
 * Note that the TCI Codec Interface (TCI-CD) describes the operations a TTCN-3 Executable is required to implement and the operations a codec implementation for a certain encoding scheme shall provide to the TE
 * 
 * See ETSI ES 201 873-6 V4.2.1 - Clause 7.3.2.1 TCI-CD required
 */
public class TciCDWrapper implements ITciCDWrapper {

    private TciCDRequired _tciFactory = TciProvider.getInstance().getTciCDRequired();
    
    public TciCDWrapper() {
        // Nothing to do
    }
    
    @Override
    public IntegerValue getInteger() {
        return (IntegerValue)_tciFactory.getInteger().newInstance();
    }

    @Override
    public IntegerValue getBigInteger(final BigInteger bigInt) { 
    	System.out.println(">>> getBigInteger" + bigInt);
        IntegerValueEx bi = (IntegerValueEx) _tciFactory.getTypeForName("org.elvior.ttcn.tritci.IntegerValueEx"); // FIXME To be solved with Janek 
        if (bi != null) { 
            System.out.println("getBigInteger: then");
            bi.setInt64(bigInt.longValue()); 
            System.out.println("getBigInteger: bi=" + bi);
            return (IntegerValue)bi;
        } else { 
            System.out.println("getBigInteger: else");
            IntegerValue i = (IntegerValue) _tciFactory.getInteger().newInstance();
            System.out.println("getBigInteger: i=" + i);
            i.setInteger(bigInt.intValue());
            return i;
        }
    }

    @Override
    public OctetstringValue getOctetstring() {
        return (OctetstringValue)_tciFactory.getOctetstring().newInstance();
    }

    @Override
    public FloatValue getFloat() {
        return (FloatValue)_tciFactory.getFloat().newInstance();
    }

    @Override
    public Value getUnionValue(final Type decodingHypothesis, final String variantName) {
//        System.out.println(">>> getUnionValue: " + decodingHypothesis.getName());
        
        String variantTypeName = decodingHypothesis.getDefiningModule().getModuleName() + "." + decodingHypothesis.getName() + "." + variantName;
//        System.out.println("getUnionValue: variantTypeName=" + variantTypeName);
        Type variantType = _tciFactory.getTypeForName(variantTypeName);
        if(variantType != null) {
//            System.out.println("Variant Type name is: " + variantType.getName());
//            String moduleBase = variantType.getDefiningModule().getBaseName();
//            System.out.println("Variant Type DefiningModule.BaseName is: " + variantType.getName());
//            String moduleObj = variantType.getDefiningModule().getModuleName();
//            System.out.println("Variant Type DefiningModule.ModuleName is: " + variantType.getName());
            Value testVal = variantType.newInstance();
            if(testVal != null) {
//                System.out.println("Variant value has been created.");
                return testVal;
            }
        } else {
            System.err.println("variantType is null");
        }
        
        return null;
    }
    
    @Override
    public Type getTypeForName(final String type) { 
        return TciProvider.getInstance().getTciCDRequired().getTypeForName(type);
    } // End of method getTypeForName
    
} // End of class TciCDWrapper
