//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "FavorTextureUnit.h"
#include "GlobalUnit.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
//---------------------------------------------------------------------------

#define TCF_VERSION 0x01

typedef struct tTextureCollectionFileHeader{
	char head[4];
	UINT idNum;

	tTextureCollectionFileHeader(){
		head[0] = 'T';
		head[1] = 'X';
		head[2] = 'T';
		head[3] = TCF_VERSION;

		idNum = 0;
	}
} TTCFHEADER; //织体收藏文件头

typedef struct tagFavorTexture
{
	UINT idLoc;
	UINT idStyle;
	char segType;   //片段类型
	char segNo;		//片段号
	char chan;		//通道号
	char iTS;		//拍号

	tagFavorTexture()
	{
		idLoc = 0;
		idStyle = 0;
		segType = segNo = chan = -1;
	};

	tagFavorTexture( UINT loc, UINT sty, char st, char sn, char cn, char ts )
	{
		idLoc = loc;
		idStyle = sty;
		segType = st;
		segNo = sn;
		chan = cn;
		iTS = ts;
	};
} TFavorTexture; //织体收藏项

typedef vector<TFavorTexture*> FAVORTXTRLIST;

FAVORTXTRLIST u_lstFavorTextures,
				u_lstFTSItems;

//////////////////////////////////////////////////////////////////////////////////////
void WriteFTSItems()
{
	AnsiString strPath = g_strUserPath.c_str();
	strPath += L"\\profile\\dt_eerp\\colIns.daj";

	//读取收藏列表
	FILE* fp = fopen( strPath.c_str(), "wb" );

	if( fp ){
		TTCFHEADER header;
		header.idNum = u_lstFTSItems.size();

		fseek( fp, 0, SEEK_SET );
		fwrite( (void*)&header, sizeof(header), 1, fp );

		for( int i=0; i<u_lstFTSItems.size(); i++ ){
			fwrite( u_lstFTSItems[i], sizeof(TFavorTexture), 1, fp );
		}

		fclose( fp );
	}
}
//////////////////////////////////////////////////////////////////////////////////////

void ReadFTSItems()
{
	AnsiString strPath = g_strUserPath.c_str();
	strPath += L"\\profile\\dt_eerp\\colIns.daj";

	//读取收藏列表
	FILE* fp = fopen( strPath.c_str(), "rb" );
	if( fp ){
		TTCFHEADER head;
		char buf[128];
		bool bChanged = false;

		int ret = fread( (void*)buf, 1, sizeof(head), fp );
		if( ret==sizeof(head) && !strncmp( buf, (char*)&head.head, sizeof(head.head))){
			int num = ((TTCFHEADER*)buf)->idNum;

			for( int i=0; i<num; i++ ){
				TFavorTexture* pItem = new TFavorTexture;
				fread( pItem, sizeof(TFavorTexture), 1, fp );

				TPROSTYLE* pStyle = GetStyleInfo( pItem->idLoc, pItem->idStyle );
				if( !pStyle ){
					delete pItem;
					bChanged = true;
				}
				else{
					u_lstFTSItems.push_back( pItem );
				}
			}
		}

		fclose( fp );

		if( bChanged )
			WriteFTSItems();
	}
}
//////////////////////////////////////////////////////////////////////////////////////

int FindListItem( UINT idLoc, UINT idStyle, char sType, char sNo, char cno, bool bAll )
{
	TFavorTexture *pTemp = NULL;
	FAVORTXTRLIST& lstItems = bAll ? u_lstFTSItems : u_lstFavorTextures;
	int middle, front = 0,
		rear = lstItems.size() - 1;

	while( front<=rear ){
		pTemp = lstItems[front];
		if( pTemp->idLoc==idLoc && pTemp->idStyle==idStyle && pTemp->segType==sType
											&& pTemp->segNo==sNo && pTemp->chan==cno )
			return front;

		pTemp = lstItems[rear];
		if( pTemp->idLoc==idLoc && pTemp->idStyle==idStyle && pTemp->segType==sType
											&& pTemp->segNo==sNo && pTemp->chan==cno )
			return rear;

		middle = (front+rear)/2;

		pTemp = lstItems[middle];
		if( pTemp->idLoc==idLoc && pTemp->idStyle==idStyle && pTemp->segType==sType
											&& pTemp->segNo==sNo && pTemp->chan==cno )
			return middle;
		else if( pTemp->idLoc>idLoc || pTemp->idLoc==idLoc && pTemp->idStyle>idStyle
									|| pTemp->idLoc==idLoc && pTemp->idStyle==idStyle
															&& pTemp->segType>sType
									|| pTemp->idLoc==idLoc && pTemp->idStyle==idStyle
															&& pTemp->segType==sType
															&& pTemp->segNo>sNo
									|| pTemp->idLoc==idLoc && pTemp->idStyle==idStyle
															&& pTemp->segType==sType
															&& pTemp->segNo==sNo
															&& pTemp->chan>cno )
			front=middle+1;
		else
			rear=middle-1;
	}

	if( front>rear ){
		return front;
	}

	return -1;
}
//////////////////////////////////////////////////////////////////////////////////////

void ClearFavorTextures( bool bAll )
{
	u_lstFavorTextures.clear();

	if( bAll ){
		for( int i=0; i<u_lstFTSItems.size(); i++ ){
			delete u_lstFTSItems[i];
		}

		u_lstFTSItems.clear();
	}
}
//////////////////////////////////////////////////////////////////////////////////////

void GetFavorTextures()
{
	ClearFavorTextures();

	if( u_lstFTSItems.empty() ){
		ReadFTSItems();
	}

	for( int i=0; i<u_lstFTSItems.size(); i++ ){
		if( u_lstFTSItems[i]->iTS==g_iMelodyTimeSignature ){
			u_lstFavorTextures.push_back( u_lstFTSItems[i] );
		}
	}
}
//////////////////////////////////////////////////////////////////////////////////////

bool IsFavorTexture( UINT idLoc, UINT idStyle, char sType,	char sNo, char cno )
{
	int ind = FindListItem( idLoc, idStyle, sType, sNo, cno );
	if( ind>=0 && ind<u_lstFavorTextures.size() ){
		TFavorTexture *pTemp = u_lstFavorTextures[ind];

		if( pTemp->idLoc==idLoc && pTemp->idStyle==idStyle && pTemp->segType==sType
											&& pTemp->segNo==sNo && pTemp->chan==cno )
			return true;
	}

	return false;
}
//////////////////////////////////////////////////////////////////////////////////////

void AddFavorTextures( UINT idLoc, UINT idStyle, char sType, char sNo, char cno )
{
	int ind = FindListItem( idLoc, idStyle, sType, sNo, cno );
	TFavorTexture * pItem = NULL;

	if( u_lstFavorTextures.empty() || ind<0 || ind>=u_lstFavorTextures.size() ){
		pItem = new TFavorTexture( idLoc, idStyle, sType, sNo, cno, g_iMelodyTimeSignature );
		u_lstFavorTextures.push_back( pItem );
	}
	else{
		TFavorTexture * pTemp = u_lstFavorTextures[ind];

		if( pTemp->idLoc==idLoc && pTemp->idStyle==idStyle && pTemp->segType==sType
											&& pTemp->segNo==sNo && pTemp->chan==cno )
			return;  //已收藏
		else{
			pItem = new TFavorTexture( idLoc, idStyle, sType, sNo, cno, g_iMelodyTimeSignature );
			u_lstFavorTextures.insert( u_lstFavorTextures.begin()+ind, pItem);
		}
	}

	if( pItem ){
		ind = FindListItem( idLoc, idStyle, sType, sNo, cno, true );
		u_lstFTSItems.insert( u_lstFTSItems.begin()+ind, pItem );

		WriteFTSItems();
	}
}
//////////////////////////////////////////////////////////////////////////////////////

void DelFavorTextures( UINT idLoc, UINT idStyle, char sType, char sNo, char cno )
{
	if( !u_lstFavorTextures.empty() ){
		int ind = FindListItem( idLoc, idStyle, sType, sNo, cno );

		if( ind>=0 && ind<u_lstFavorTextures.size() ){
			TFavorTexture *pTemp = u_lstFavorTextures[ind];

			if( pTemp->idLoc==idLoc && pTemp->idStyle==idStyle && pTemp->segType==sType
												&& pTemp->segNo==sNo && pTemp->chan==cno ){
				u_lstFavorTextures.erase( u_lstFavorTextures.begin()+ind );

				ind = FindListItem( idLoc, idStyle, sType, sNo, cno, true );

				delete u_lstFTSItems[ind];
				u_lstFTSItems.erase( u_lstFTSItems.begin()+ind );

				WriteFTSItems();
			}
		}
	}
}
//////////////////////////////////////////////////////////////////////////////////////

void SetFavorTexture( UINT idLoc, UINT idStyle, char sType,	char sNo, char cno, bool bSet )
{
	if( bSet ){
		AddFavorTextures( idLoc, idStyle, sType, sNo, cno );
	}
	else{
		DelFavorTextures( idLoc, idStyle, sType, sNo, cno );
	}
}
//////////////////////////////////////////////////////////////////////////////////////

