#include "LiteListViewMask.h"
#include "gType.h"
#include "GraphLow.h"
#include "GraphBase.h"
#include "GlobalString.h"
#include "MainCtl.h"
#include "RuntimeData.h"
#include "Runtime.h"
#include "Gui.h"

#define TextTopOffSet	6
#define ListLineCnt	10

void TLiteListViewMask::Init(int x1, int y1, int aW, int aH, int OwnerX, int OwnerY, u32 aColor)
{
	#define CH	30	//Caption Height
	unsigned short Left,Right, Top, Bottom, Width, Height, i;
	Bound.SetBySize(OwnerX + x1, OwnerY + y1, aW, aH);
	Left = Bound.Left +3;
	Top = Bound.Top+3;
	Width = aW - 7;
	Height = aH - 7;
	Right = Bound.Right -4;
	Bottom = Bound.Bottom -4;
	Caption.Init(Left,Top,Width, CH, 0, 0, clWhite, clMaroon);
	Content.Set(Left, Top+CH, Right-20, Bottom);
	VScrollBarBox.Set(Right-20 +1, Top+CH + 33, Right, Bottom);
	Color = aColor;
	#undef CH

	ActiveLineNum=0;
	
	ListView.ColHeight = 33;
	ListView.TopIndex=0;
	ListView.RowCount = 9;
	ListView.FixColTop = Content.Top;
	ListView.FixColBottom = ListView.FixColTop + ListView.ColHeight;
	ListView.ColTop[0] = ListView.FixColBottom + 1;
	ListView.ColBottom[0] = ListView.ColTop[0] + ListView.ColHeight -1;
	ListView.ColLeft[0] = 1;
	ListView.ColRight[0] = ListView.ColWidth[0] +1;
	for(i=1; i<12; i++){
		ListView.ColTop[i] = ListView.ColTop[i -1] + ListView.ColHeight;
		ListView.ColBottom[i] =  ListView.ColBottom[i-1] + ListView.ColHeight; 
	}
	for(i=1;i<ListView.RowCount;i++){
		ListView.ColLeft[i] = ListView.ColWidth[i-1] + ListView.ColLeft[i-1];
		ListView.ColRight[i] = ListView.ColWidth[i] + ListView.ColLeft[i];
	}
	
	Caption.Border.Color = clGray;
	VScrollBar.Color = clFrmFace;
	VScrollBar.RibbonColor =  clBlue;
	
	FixColColor = clMaroon;
	FixColTextColor = clNearWhite;
	
	TextColor = clNearBlack;
	SelectedColor = clBlue;
	SelectedTextColor = clNearWhite;
	
	LineCount = 10;
	//Items.SelectedIndex  = 0;
}

void TLiteListViewMask::SetListColWidth(int w0, int w1, int w2, int w3, int w4,int w5,int w6, int w7, int w8)
{
	unsigned int i;
	ListView.ColWidth[0] = w0;
	ListView.ColWidth[1] = w1;
	ListView.ColWidth[2] = w2;
	ListView.ColWidth[3] = w3;
	ListView.ColWidth[4] = w4;
	ListView.ColWidth[5] = w5;
	ListView.ColWidth[6] = w6;
	ListView.ColWidth[7] = w7;
	ListView.ColWidth[8] = w8;
	
	ListView.ColLeft[0] = Content.Left +1;
	ListView.ColRight[0] = ListView.ColLeft[0] + w0;
	for(i=1; i<9; i++){
		ListView.ColLeft[i] = ListView.ColRight[i-1] +1;
		ListView.ColRight[i] = ListView.ColLeft[i] + ListView.ColWidth[i];	
	}
}

void TLiteListViewMask::RenderBackGround(void)
{
	RectFillRender(Content.Left, Content.Top, Content.Right, Content.Bottom, Color);
}

void TLiteListViewMask::RenderBackGround(unsigned int aClr)
{
	RectFillRender(Content.Left, Content.Top, Content.Right, Content.Bottom, aClr);
}

void TLiteListViewMask::RenderContent(void)
{
	#define FixColColor	clMedGray
	#define OddColor		clNearMoneyGreen
	unsigned int x,y, y2, clr;
	RectFillRender(Content.Left, ListView.FixColTop, VScrollBarBox.Right, ListView.FixColBottom, FixColColor);
	y = ListView.FixColBottom + 1;
	for(x=0; x<(LineCount+1); x++){	
		y2 =  y + ListView.ColHeight - 1;
		if(y2 > Content.Bottom)y2 = Content.Bottom;		
		if(x == ListView.SelectedCol){
				RectFillRender(Content.Left, y, Content.Right, y2, SelectedColor);			
		}else{
			if(x & 1){
				RectFillRender(Content.Left, y, Content.Right, y2, OddColor);
			}else{
				RectFillRender(Content.Left, y, Content.Right, y2, Color);
			}							
		}
		y = y2 + 1;
		if(y>Content.Bottom)break;
	}	
}

void TLiteListViewMask::DrawSelf(void)
{
	unsigned int x, y, x2, y2, w, h;
	
	VertLineRender(Bound.Left,		Bound.Top, 			Bound.Height -1,			0xFFFFFFFF);
	VertLineRender(Bound.Right -1,	Bound.Top +1,		Bound.Height -2,			0xFF808080);
	VertLineRender(Bound.Right,		Bound.Top,			Bound.Height,			0xFF404040);
	
	HorizLineRender(Bound.Left, 		Bound.Top, 			Bound.Width - 1, 			0xFFFFFFFF);
	HorizLineRender(Bound.Left +1, 	Bound.Bottom -1,  	Bound.Width - 2, 			0xFF808080);
	HorizLineRender(Bound.Left,		Bound.Bottom,		Bound.Width,			0xFF404040);
	
	x = Bound.Left +2 ;	
	x2 = Bound.Right -2;
	y = Bound.Top +2;
	y2 = Bound.Bottom - 2;
	w = Bound.Width -4;
	h = Bound.Height - 4;
	VertLineRender	(x,		y, 		h-1,	0xFF808080);
	HorizLineRender	(x,		y, 		w-1,	0xFF808080);
		
	VertLineRender	(x+1,		y+1, 		h-2,	0xFF404040);
	HorizLineRender	(x+1,		y+1, 		w-2,	0xFF404040);
	
	VertLineRender	(x2-1,	y+1, 		h-2,	0xFFD4D0C8);
	HorizLineRender	(x+1,		y2-1, 	w-2,	0xFFD4D0C8);

	VertLineRender	(x2,		y, 	  	h,	0xFFFFFFFF);
	HorizLineRender	(x,		y2, 		w,	0xFFFFFFFF);
	
	RenderContent();
}

void TLiteListViewMask::DrawCaption(void)
{	
	Caption.Show();
}

void TLiteListViewMask::DrawVertScrollBar(void)
{
	TVScrollBar::sDrawBySize(VScrollBarBox.Left, VScrollBarBox.Top,
					VScrollBarBox.Width, VScrollBarBox.Height,
					clMedGray, clGray,
					EntryCount, 10, ListView.TopIndex);
}

void TLiteListViewMask::DrawList(void)
{
	int i, index, BGClr, TextClr, aTop;
	unsigned int aClr, aAddr, aP0,aP1;;
	if(EntryCount < 1)return;

	RenderContent();		
	aTop = ListView.FixColTop + TextTopOffSet;

	SetListColWidth(30,90,100,150,100,100,1,1,1);
	TextRender_string24(ListView.ColLeft[1],aTop,TextColor, " 序号" );	
	TextRender_string24(ListView.ColLeft[2],aTop,TextColor, " 路径");
	TextRender_string24(ListView.ColLeft[3],aTop,TextColor, "设备类型");
	TextRender_string24(ListView.ColLeft[4],aTop,TextColor, " 描述 ");

	for(i=0; i<LineCount; i++){
		if(i == ListView.SelectedCol)aClr = SelectedTextColor;
		else aClr = TextColor;
		if(  (Entry[i].Path0 !=0) && (Entry[i].Path1 !=0)  ){
			
			aTop = ListView.ColTop[i] + TextTopOffSet;
			ImageList.Render(24,    Entry[i].iType1,      ListView.ColLeft[0], aTop);
			
			Str.FromUInt6Dg(ListView.TopIndex + i +1);
			TextRender_string24(ListView.ColLeft[1], aTop, aClr, Str.Text);
			
			Str.FromUInt3Dg(Entry[i].Path0);
			Str.AddOnStr("-");
			Str.AddOn3Dg(Entry[i].Path1);
			TextRender_string24(ListView.ColLeft[2], aTop, aClr, Str.Text);
			
			Str.FromStr(StrTypeShortName[Entry[i].iType1]);
			TextRender_string24(ListView.ColLeft[3], aTop, aClr, Str.Text);
			
			//Load Descp 
			aP0 = Entry[i].Path0;
			aP1 = Entry[i].Path1;
			aAddr = dAddrSdRam_Descp + (aP0 * 8192) + (aP1 *32);
			for(index=0; index<32; index++){
				Str.Text[index] = *(volatile unsigned char *)(aAddr + index);
				if(Str.Text[index] == '\0'){
					break;
				}
			}
			Str.Text[32] = '\0';
			TextRender_string24(ListView.ColLeft[4], aTop, aClr, Str.Text);
		}
	}
	DrawVertScrollBar();
}

void TLiteListViewMask::Show(void)
{
	DrawSelf();
	Caption.Enable = 1;
	DrawCaption();
	DrawList();	
}

void TLiteListViewMask::ReDraw(void)
{
	Caption.Enable = 1;
	DrawCaption();
	DrawList();	
}

void TLiteListViewMask::FullRedraw(int Prm)
{
	if(Prm == 0){
		DrawSelf();
		Caption.Enable = 1;
		DrawCaption();
		DrawList();			
	}else
	if(Prm == 1){
		RectFillRender(GuiRedrawDataList[1][0], GuiRedrawDataList[1][1], GuiRedrawDataList[1][2], GuiRedrawDataList[1][3], Color);
		Caption.Enable = 1;
		DrawCaption();
		DrawList();
	}
}

void TLiteListViewMask::EntryClear()
{
	int i;
	EntryFound.Path0 = 0;
	EntryFound.Path1 = 0;
	for(i=0; i<10; i++){
		Entry[i].Path0 = 0;
		Entry[i].Path1 = 0;
		Entry[i].iType0 = 0;
		Entry[i].iType1 = 0;
		Entry[i].Loaded = 0;
	}
	ListView.SelectedCol = 0;
	ListView.TopIndex = 0;
}

void TLiteListViewMask::EntryLoadForward(void)
{
	unsigned int sPath0,sPath1,aType0,aType1, aInx;
	int i, j, isFull;
	int NextIndex;
	unsigned int FoundNew;
	
	sPath0 = 0;
	sPath1 = 0;

	isFull = 1;
	for(i=0; i<10; i++){
		if( Entry[i].Loaded ){
			sPath0 = Entry[i].Path0;
			sPath1 = Entry[i].Path1;
		}else{
			isFull = 0;
			aInx = i;
			break;
		}
	}

	if(isFull){
		for(i=0; i<9; i++){
			Entry[i].Path0 = Entry[i+1].Path0;
			Entry[i].Path1 = Entry[i+1].Path1;
			Entry[i].iType0 = Entry[i+1].iType0;
			Entry[i].iType1 = Entry[i+1].iType1;
			Entry[i].Loaded = Entry[i+1].Loaded;
		}
		Entry[9].Path0 =0;
		Entry[9].Path1 =0;
		Entry[9].iType0 =0;
		Entry[9].iType1 =0;
		Entry[9].Loaded =0;
		sPath0 = Entry[8].Path0;
		sPath1 = Entry[8].Path1;
		aType0 = Entry[8].iType0;
		aType1 = Entry[8].iType1;
		aInx = 9;
	}

	FoundNew = 0;
	do{
		if(sPath0 < dPORT_MAX_COUNT){
			//is Port; Find Current Port Sub Items
			i = sPath0 -1;
			j = sPath1 -1 +1;
			if(MainCtl.fData.Split.ExistTablePort[i] == 0xA5){
				for(; j<dEP_MAX_COUNT_PER_PORT; j++){
					if(Port[i].ExistTable[j] == 0xA5){
						if(Port[i].StateTable[j] & (dEpStateBitMask)){
							EntryFound.Path0 = i + dADDR_OFFSET_PORT;
							EntryFound.Path1 = j +1;
							EntryFound.iType0 = dgTYPE_PORT;
							EntryFound.iType1 = EpDev[i][j].fData.Split.dType;
							FoundNew = 1;
							break;
						}
					}
				}
			}			
			if(FoundNew)break;
			//is Port; Find All Next Port Sub Items
			for(i=sPath0; i<dPORT_MAX_COUNT; i++){
				if(MainCtl.fData.Split.ExistTablePort[i] == 0xA5){
					for(j=0; j<dEP_MAX_COUNT_PER_PORT; j++){
						if(Port[i].ExistTable[j] == 0xA5){
							if(Port[i].StateTable[j] & (dEpStateBitMask)){
								EntryFound.Path0 = i + dADDR_OFFSET_PORT;
								EntryFound.Path1 = j +1;
								EntryFound.iType0 = dgTYPE_PORT;
								EntryFound.iType1 = EpDev[i][j].fData.Split.dType;
								FoundNew = 1;
								break;
							}
						}
					}
					if(FoundNew)break;
				}
			}			
		}
	}while(0);
	
	ListView.TopIndex++;
	if(FoundNew){
		Entry[aInx].Path0 = EntryFound.Path0;
		Entry[aInx].Path1 = EntryFound.Path1;
		Entry[aInx].iType0 = EntryFound.iType0;
		Entry[aInx].iType1 = EntryFound.iType1;
		Entry[aInx].Loaded = 1;
		SelectedIndex++;
	}
}

void TLiteListViewMask::EntryLoadBackward(void)
{
	int sPath0,sPath1,aType0,aType1;
	int i, j, point;
	unsigned int FoundNew;
	
	if( (ListView.TopIndex-1) < 0){
		return;
	}
	
	sPath0 = Entry[0].Path0;
	sPath1 = Entry[0].Path1;
	
	if( (sPath0 == 0) || (sPath1 == 0) )
	{
		return;
	}
	
	FoundNew = 0;
	do{
		if(sPath0 <= dPORT_MAX_COUNT){
			//is Port; 
			//Find Current Port Sub Items
			i = sPath0 -1;
			j = sPath1 -1 -1;
			if(MainCtl.fData.Split.ExistTablePort[i] == 0xA5){
				for(; j>-1; j--){
					if(Port[i].ExistTable[j] == 0xA5){
						if(Port[i].StateTable[j] & (dEpStateBitMask)){
							EntryFound.Path0 = i + dADDR_OFFSET_PORT;
							EntryFound.Path1 = j +1;
							EntryFound.iType0 = dgTYPE_PORT;
							EntryFound.iType1 = EpDev[i][j].fData.Split.dType;
							FoundNew = 1;
							break;
						}
					}
				}
			}
			//is Port; Find All Next Port Sub Items
			if(FoundNew)break;
			i = sPath0 -1 -1;
			for(; i>-1; i--){
				if(MainCtl.fData.Split.ExistTablePort[i] == 0xA5){
					j = dEP_MAX_COUNT_PER_PORT -1;
					for(; j>-1; j--){
						if(Port[i].ExistTable[j] == 0xA5){
							if(Port[i].StateTable[j] & (dEpStateBitMask)){
								EntryFound.Path0 = i + dADDR_OFFSET_PORT;
								EntryFound.Path1 = j +1;
								EntryFound.iType0 = dgTYPE_PORT;
								EntryFound.iType1 = EpDev[i][j].fData.Split.dType;
								FoundNew = 1;
								break;
							}
						}
					}
					if(FoundNew)break;
				}
			}
		}
	}while(0);
	
	if(FoundNew){
		for(i=9; i>0; i--){
			Entry[i].Path0 = Entry[i-1].Path0;
			Entry[i].Path1 = Entry[i-1].Path1;
			Entry[i].iType0 = Entry[i-1].iType0;
			Entry[i].iType1 = Entry[i-1].iType1;
			Entry[i].Loaded = Entry[i-1].Loaded;
		}		
		Entry[point].Path0 = EntryFound.Path0;
		Entry[point].Path1 = EntryFound.Path1;
		Entry[point].iType0 = EntryFound.iType0;
		Entry[point].iType1 = EntryFound.iType1;
		Entry[point].Loaded = 1;
		
		ListView.TopIndex--;
	}	
}

void TLiteListViewMask::EntryTotalize(void)
{
	unsigned int i,j;
	EntryCount = 0;
	for(i=0; i<dPORT_MAX_COUNT; i++){
		if(MainCtl.fData.Split.ExistTablePort[i] == 0xA5){
			for(j=0; j<dEP_MAX_COUNT_PER_PORT;j++){
				if(Port[i].ExistTable[j] == 0xA5){
					if(Port[i].StateTable[j] & (dEpStateBitMask)){
						EntryCount++;
					}
				}
			}
		}
	}
	for(i=0; i<dHANDCTL_MAX_COUNT; i++){
		if(MainCtl.fData.Split.ExistTableHandCtl[i] == 0xA5){
			for(j=0; j<HandCtlBoard[i].fData.HandCtl.ResistedCount;j++){
				//if(HandCtlBoard[i].[j] & (dEpStateBitMask)){
					//EntryCount++;
				//}
			}
		}
	}
	for(i=0; i<dDIRECTCTL_MAX_COUNT; i++){
		if(MainCtl.fData.Split.ExistTableDirectCtl[i] == 0xA5){
			for(j=0; j<DirectCtlBoard[i].fData.DirectCtl.ResistedCount;j++){
				//if(DirectCtlBoard[j].Flag [j] & (dEpStateBitMask)){
				//	EntryCount++;
				//}
			}
		}
	}	
}

void TLiteListViewMask::EntryFindByIndex(unsigned int aIndex)
{
	unsigned int i,j;
	unsigned int fIndex = 0;
	for(i=0; i<dPORT_MAX_COUNT; i++){
		if(MainCtl.fData.Split.ExistTablePort[i] == 0xA5){
			for(j=0; j<Port[i].fData.Port.ResistedCount;j++){
				if(Port[i].ExistTable[j] == 0xA5){
					if(Port[i].StateTable[j] & (dEpStateBitMask)){
						if(fIndex == aIndex){
							EntryFound.Path0 = i;
							EntryFound.Path1 = j;
							EntryFound.iType0 = dgTYPE_PORT;
							EntryFound.iType1 = EpDev[i][j].fData.Split.dType;
						}
						fIndex++;
					}
				}
			}
		}
	}
	for(i=0; i<dHANDCTL_MAX_COUNT; i++){
		if(MainCtl.fData.Split.ExistTableHandCtl[i] == 0xA5){
			for(j=0; j<HandCtlBoard[i].fData.HandCtl.ResistedCount;j++){
				//if(HandCtlBoard[i].SubItemsFlag[j] & (dEpStateBitMask)){
				//	if(fIndex == aIndex){
				//		EntryFound.Path0 = i;
				//		EntryFound.Path1 = j;
				//		EntryFound.iType0 = dgTYPE_HAND_CTL;
				//		EntryFound.iType1 = dgTYPE_HAND_CTL_CHANNEL;
				//	}
				//	fIndex++;
				//}
			}
		}
	}
	for(i=0; i<MainCtl.fData.Split.DirectCtlCount; i++){
		if(MainCtl.fData.Split.ExistTableDirectCtl[i] == 0xA5){
			for(j=0; j<DirectCtlBoard[i].fData.DirectCtl.ResistedCount;j++){
				//if(HandCtlBoard[i].SubItemsFlag[j] & (dEpStateBitMask)){
				//	if(fIndex == aIndex){
				//		EntryFound.Path0 = i;
				//		EntryFound.Path1 = j;
				//		EntryFound.iType0 = dgTYPE_DIRECT_CTL;
				//		EntryFound.iType1 = dgTYPE_DIRECT_CTL_CHANNEL;
				//	}
				//	fIndex++;
				//}
			}
		}
	}	
}

void TLiteListViewMask::EntryLoad(void)
{
	int i;
	EntryTotalize();
	for(i=0; i<10; i++){
		EntryLoadForward();
	}
	ListView.TopIndex = 0;
	ListView.SelectedCol = 0;
	SelectedIndex = 0;
	
	TMyString::sFromStr("查询->已屏蔽设备",  Caption.Text);
}

TGuiMsgReturn TLiteListViewMask::KeyIn(unsigned char aKey)
{
	int ind,i,aIndex;
	TGuiMsgReturn aMsg = guiMsgNone;
	//Up or Down One Index___________________
	
	if(aKey == VK_TAB){
		
	}
	if(1){
		if(VK_UP == aKey){
			if(ListView.SelectedCol >0){
				ListView.SelectedCol--;
				DrawList();
			}else{
				if(ListView.TopIndex >0){
					EntryLoadBackward();	
					DrawList();
				}
			}
		}else
		if(VK_DOWN == aKey){
			if((ListView.TopIndex + ListView.SelectedCol) < (EntryCount-1) ){
				if( ListView.SelectedCol < (ListLineCnt -1) ){
					ListView.SelectedCol++;
					DrawList();
				}else{
					if(  (ListView.TopIndex + ListLineCnt) < EntryCount ){
						EntryLoadForward();	
						DrawList();
					}
				}
			}
		}
	}
	
	if(aKey == VK_EXECUTE){
		//Open path Device
	}else
	if(aKey == VK_F2){
		
	}else
	if(aKey == VK_RETURN){
		aMsg = guiMsgReturn;
	}	

	return aMsg;	
}




