package com.twc.isa.AssetModule;

import java.net.InetAddress;
import java.util.Vector;

import org.apache.log4j.Logger;
import org.omg.CORBA.StringHolder;

import com.twc.isa.ContentModule.Content;
import com.twc.isa.ContentModule.ContentListHolder;
import com.twc.isa.MetadataModule.MetadataList;
import com.twc.isa.MetadataModule.MetadataListHolder;
import com.twc.isa.ServerModule.AdministrativeState;
import com.twc.isa.ServerModule.AdministrativeStateHolder;
import com.twc.isa.ServerModule.InvalidStateChange;
import com.twc.isa.ServerModule.NoGuiProvisioned;
import com.twc.isa.ServerModule.OperationalState;
import com.twc.isa.ServerModule.OperationalStateHolder;
import com.twc.isa.ServerModule.OutOfService;
import com.twc.isa.ServerModule.ProvisioningFailed;
import com.twc.isa.ServerModule.ServantBase_;
import com.twc.isa.ServerModule.ServantBase_impl;
import com.twc.isa.ServerModule.ServantFactory_;
import com.twc.isa.ServerModule.ServantNotFound;
import com.twc.isa.ServerModule.ServantNotProvisioned;
import com.twc.isa.ServerModule.Unimplemented;
import com.twc.isa.ServerModule.UnspecifiedException;
import com.twc.isa.SessionModule.Session;
import com.twc.isa.StreamModule.Stream;

public class Asset_impl extends AssetPOA {
    
	private final Logger log = Logger.getLogger(Asset_impl.class);
	
    public static AssetFactory m_AssetFactory  = null;
	
    private ServantBase_ m_base         = null;
    public MetadataList  m_MetadataList = null;
    public Content[]     m_Content      = null;
    public Vector        m_Assets       = null;
    public Asset         m_ParentAsset  = null;
    
    private static int m_iCnt = 0;
    private int        m_iIdx = 0;
    
    public com.twc.isa.MetadataModule.Metadata[]
    getMetadata() {
    	log.debug("getMetadata() not implemented.");
    	return null;
    }
    
    public AssetWithRelations
    getAssetDetails() {
    	log.debug("getAssetDetails() not implemented.");
    	return null;
    }
    
    public void
    setAssetGroupID(String groupId, String groupType) {
    	log.debug("setAssetGroupId() not implemented.");
    }
    
    public int
    getN2BBAssetVersion() {
    	log.debug("getN2BBAssetVersion() not implemented.");
    	return 0;
    }
    
    public AssetData getAssetData() {
    	log.debug("getAssetData() not implemented.");
    	return null;
    }
    
    public String
    getAssetGroupID(String grpType) {
    	log.debug("getAssetGroupID() not implemented.");
    	return null;
    }
    
    public void
    removeAsset(Asset a) {
    	log.debug("removeAsset() not implemented.");
    }
    
    public Asset_impl(org.omg.CORBA.ORB orb,String strName) {
        
        m_iIdx  = ++ m_iCnt;
        
        if( strName == null || strName.trim().equals("")) {
            try {
                strName = "Asset_" + m_iIdx + "_on_" + InetAddress.getLocalHost().getHostAddress();
            } catch( Exception ex){
                log.error(null, ex);
            }
        }
        
        m_base  = new ServantBase_(orb,strName );
        
        if(m_AssetFactory == null ){
            
            // get the reference of Asset Factory Object from Naming Service
            
            String [] strFullPath  = {"Factories","AssetFactory"};
            org.omg.CORBA.Object factoryObj = ServantBase_impl.getFactoryObject(orb,strFullPath);
            m_AssetFactory = AssetFactoryHelper.narrow(factoryObj);
            
        }
        
        log.debug("Asset Created with name : " + strName);
    }
    
    
    public Content 
    locateContent(int serviceGroup) {
        // return the Content having provided service group
        // currently no attribute like service group for content
        
        Content [] contents = null;
        contents = this.returnContents();
        return contents[0];
    }
    
    public void 
    addAsset(Asset a){
        if( m_Assets == null) m_Assets = new Vector();
        m_Assets.add(a);
    }
       
    public void 
    provisionAsset(AdministrativeState theAdministrativeState) 
    throws UnspecifiedException, InvalidStateChange, ProvisioningFailed {
            m_base.setAdminState(theAdministrativeState);
    }
    
    public void 
    provisioningGui(StringHolder strHolder) throws NoGuiProvisioned {
    	
        String strName  = getClass().getName();
        int iIdx    = strName.lastIndexOf("_");
        String strTxtFileName   = strName.substring(0,iIdx) + "ProvisioningURL.txt";
        strHolder.value = ServantFactory_.getProvisionGUI_URL_Name( strTxtFileName);
    }
    
    public void 
    statusGui(org.omg.CORBA.StringHolder strHolder) throws NoGuiProvisioned {
    	
        String strName  = getClass().getName();
        int iIdx    = strName.lastIndexOf("_");
        String strTxtFileName   = strName.substring(0,iIdx) + "StatusURL.txt";
        strHolder.value = ServantFactory_.getProvisionGUI_URL_Name( strTxtFileName);
    }
    
    public void 
    destroy() {
        m_base.destroy();
    }
    
    public String 
    name(){
        return m_base.name();
    }
    
    public AdministrativeState 
    getAdminState() 
    throws UnspecifiedException {
        return m_base.getAdminState();
    }
    
    public void 
    setAdminState(AdministrativeState st) 
    throws UnspecifiedException {
        m_base.setAdminState(st);
    }
    
    public void 
    getCreateTime(org.omg.CORBA.LongHolder longHolder) 
    throws UnspecifiedException, Unimplemented {
        m_base.getCreateTime(longHolder);
    }
    
    public void 
    getLastModifiedTime(org.omg.CORBA.LongHolder longHolder) 
    throws UnspecifiedException, Unimplemented {
        m_base.getLastModifiedTime(longHolder);
    }
    
    public OperationalState 
    getOpState() throws com.twc.isa.ServerModule.UnspecifiedException {
        return m_base.getOpState();
    }
    
    public void 
    getAssetProvisioning(org.omg.CORBA.StringHolder name, 
    		             MetadataListHolder theMetadataList, 
    		             ContentListHolder contentListHolder, 
    		             AssetHolder theParentAsset, 
    		             AssetListHolder aChildAssetList, 
    		             OperationalStateHolder theOperationalState, 
    		             AdministrativeStateHolder theAdministrativeState) 
    throws UnspecifiedException, ServantNotProvisioned {
        name.value = name();
        theMetadataList.value = m_MetadataList;
        //aContent.value = aContent();
        theParentAsset.value = m_ParentAsset;
        //aChildAsset.value = aChildAsset();
        theOperationalState.value = m_base.getOpState();
        theAdministrativeState.value = m_base.getAdminState();      
    }
    
    
    public void logPurchase(String str, String str1) 
    throws ServantNotFound, UnspecifiedException, Unimplemented, OutOfService {
        throw new Unimplemented();
    }
    
    public Stream play(Session session, int param) 
    throws ServantNotFound, StreamFailed, PlayFailed, UnspecifiedException, OutOfService {
        throw new PlayFailed();
    } 
    
    public Content[] 
    returnContents(){       
        return m_Content;        
    }
    
    public Asset[] 
    aChildAsset() {
    	
        int iLen  = 0;
        if( m_Assets != null){
            iLen = m_Assets.size();
        } else iLen =0;
        
        Asset [] assets = null;
        
        if (iLen == 0) {
            assets = new Asset[1];
            assets[0] =null;
        } else {
            assets = new Asset[iLen];
            m_Assets.toArray(assets);
        }
        return assets;
    }
    
    public void 
    aChildAsset(Asset[] value) {
        int iLen = value.length;
        if( m_Assets == null) m_Assets = new Vector();
        
        for( int iCnt = 0;iCnt < iLen;iCnt++) {
            m_Assets.add(iCnt,value[iCnt]);
        }
    }
    
    public void 
    theParentAsset(Asset value) {
        m_ParentAsset = value;
    }
    
    public Asset 
    theParentAsset() {
        return m_ParentAsset;
    }
       
    public void 
    aContent(Content[] a) {
        m_Content = a;
    }
    
    public Content[] 
    aContent() {
        return  m_Content;
    }
    
    public AssetFactory 
    theAssetFactory() {
        return m_AssetFactory;
    }
    
    public void 
    theAssetFactory(AssetFactory value) {
        m_AssetFactory	= value;
    }
    
    public MetadataList 
    theMetadataList(){
        return m_MetadataList;
    }
    
    public void 
    theMetadataList(MetadataList value){
        m_MetadataList	= value;
    }
}

