// AudioTielineShim.cpp: implementation of the AudioTielineShim class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "AudioTielineShim.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

AudioTielineShim::AudioTielineShim()
{
	int iDestOnSourceRouter;
	int iMainSource;
	bncs_string sConnectingRouter;
	
		m_stddevice=getInstanceDevice("studer_router");
		m_aesdevice=getInstanceDevice("aes_router");
		m_intSizeMaindest = getRouterSize(m_stddevice, 1);
		m_intSizeMainsource = getRouterSize(m_stddevice, 0);
		
	    Make_Links_Map("aes_router");
		Make_Links_Map("studer_router");

	//	sConnectingRouter=GetDest_Tieline_Details("aes_router",3,iDestOnSourceRouter,iMainSource);
}

AudioTielineShim::~AudioTielineShim()
{

}

void AudioTielineShim::Make_Links_Map(bncs_string sinstance){

	//<setting id="1" value="1,studer_router,108,0"/>
//	bncs_string	strmixervalue="start";
    int i=0;
	int j=0;
	bncs_string strvalue = "start";
	bncs_string sSource,sRouter,sDest1,sDest2,sService3,sService4,sService5,sIndex,sstate,sSTATE,srouter,ssce,sdest,sObject;
	cAssLink *pLink;
	bncs_string sDestination;
	bncs_string srtra, srtrb;
	int iOffset;
	char szValue[250] = "start";

	int istoredrtr;
	mapIterator mapofrtrs;

	
	
	sObject="allocated_highways_" +  sinstance;

	while (strcmp(szValue,"")){
		i++;
		sSource="";
		sRouter="";
		strvalue = (getObjectSetting(bncs_string(sObject), i));
		
		wsprintf(szValue,strvalue);

		strvalue.split(',',sSource,sDestination);
	//	wsprintf(szValue,strvalue);

		OutputDebugString(sSource);
		strvalue=sDestination;

		strvalue.split(',',sDestination,sRouter);
	//	wsprintf(szValue,strvalue);

		OutputDebugString(sSource);
		strvalue=sRouter;
		strvalue.split(',',sRouter,sDest1);
		strvalue=sDest1;
		strvalue.split(',',sDest1,sDest2);
	
	
	
		
		if(strcmp(szValue,"")){
			pLink=new cAssLink();
			pLink->m_sRouter=sRouter;
			pLink->m_source_main=sSource.toInt();
			pLink->m_intdest1=sDest1.toInt();
			pLink->m_intdest2=sDest2.toInt();
		
			if ( sinstance=="aes_router"){
				maplinkaes.insert(pair<int,cAssLink*>(sDestination.toInt(),pLink));

				int iDriver =getInstanceDevice(sRouter);
				if (iDriver){//if it is not then insert it
						mapofrouters.insert(pair<int,int>(iDriver,0));
					
				}
			}
			else if ( sinstance=="studer_router"){
				maplinkstuder.insert(pair<int,cAssLink*>(sSource.toInt(),pLink));

				int iDriver =getInstanceDevice(sRouter);
				if (iDriver){//if it is not then insert it
						mapofrouters.insert(pair<int,int>(iDriver,0));
					
				}
			}
		 
		}
		
	}
	
}

bncs_string AudioTielineShim::GetDest_Tieline_Details(bncs_string Router, int iDestination, int &iReturnedDestination,int & isourceMain){
	maplinkIterator maplinkit;
	cAssLink * pLink;

	if (Router=="aes_router"){
		maplinkit=maplinkaes.begin();
		maplinkit=maplinkaes.find(iDestination);

		if (maplinkit!=maplinkaes.end()){
			
			pLink=maplinkit->second;
			iReturnedDestination= pLink->m_intdest1;
			isourceMain=pLink->m_source_main;
			return pLink->m_sRouter;

		}
	}


return "";
}

bncs_string AudioTielineShim::CrosspointInformation(int iSource, int iDestination){


	//are they both on the same router = easy

	//is the source on the AES and the destination on the studer

	//is the source on the studer and the destination on the AES

		if (iSource>m_intSizeMainsource && iDestination<=m_intSizeMaindest) 
					OutputDebugString("AES to Mixer");
					
					

		else if(iSource<=m_intSizeMainsource && iDestination<=m_intSizeMaindest) 
						OutputDebugString("MIXER to Mixer");
						
						

		else if(iSource<=m_intSizeMainsource && iDestination>m_intSizeMaindest) 
						OutputDebugString("MIXER to AES");
					
						
		else if(iSource>m_intSizeMainsource && iDestination>m_intSizeMaindest) 
					OutputDebugString("AES to AES");
				
			
	return "";
}