

#include "GoodList.h"
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include "PublicFun.h"

extern  PAINTSTRUCT ps;
extern 	HDC hdc;
extern 	HBITMAP hbmp; 
extern 	HBITMAP hbmp2; 
extern 	BITMAP bmp;
extern 	HDC hdcMem;
extern 	HDC hdcTmp;
extern 	RECT rect;
extern 	HWND hwnd;
extern  char texta[TEXTAMAX][20];
extern char t_goodName[GOODMAX][GOODNAMEMAXLEN];
extern  int daojuflag;

extern int KeyReturn;
extern int KeyEsc;
extern int KeyHome;
extern int KeySpace;
extern int KeyLeft0;
extern int KeyUp0;
extern int KeyRight0;
extern int KeyDown0;
extern int color;

extern HPEN t_hpen;
extern HBRUSH t_hbrush;

using namespace std;


void GoodList::PubInit(int choice)
{
	wlogfile(1,__FUNCTION__,"开始",0);

	char name[NORMALSTRLEN];	 
	sprintf_s(name,"Res\\Good\\goods%d.sdt",choice+1);
	ifstream infile(name,ios::in);

	for(int y=0;y<GOODMAX;y++)
	{
		//good[y]=new Good();
		//good[y].InitData(choice,y);
		//wlogfile(1,__FUNCTION__,"开始",0);



		infile>>good[y].no;
		infile>>good[y].owner;
		infile>>good[y].style;
		infile>>good[y].wuliup;
		infile>>good[y].ziliup;
		infile>>good[y].speedup;
		infile>>good[y].bingzhong;

		infile>>good[y].hiscity;
		infile>>good[y].hisfinder;
		infile>>good[y].finded;

		if(infile.eof()!=0) break;		
	}
	infile.close();
}



void GoodList::Init(int unx,int uny,int uchoice,int xuanze,int xuan)
{
	wlogfile(1,__FUNCTION__,"开始",0);
	statf=0;
	statf2=0;
	choice=uchoice;

	this->unx=unx;
	this->uny=uny;
	GetListStyle(xuanze);
	GetCityNum();
	GetGoodNum();
	flagu=0;
	flagd=0;
	tmpfx=0;
	statbtp=statf;
	count=0;
	goodowner=0;

}


void GoodList::Show(WPARAM KeyCode)
{
	wlogfile(1,__FUNCTION__,"开始",0);
	if (goodnum==0&&listno==0)
	{
		return;
	}
	if (herogoodnum==0&&listno==1)
	{
		return;
	}
	int heronu;
	heronu=goodnum;
	if(listno==1)
	{
		heronu=herogoodnum;
	}

	int ux=3;
	int uy=(heronu>5?6:heronu);
	if(flagd==1&&KeyCode==KeyDown0)
	{
		statbtp++;
		tmpfx=uy-1;
		if(statbtp==(heronu-uy+2))statbtp=heronu-uy+1;

	}
	else if(flagu==1&&KeyCode==KeyUp0)
	{
		statbtp--;
		tmpfx=0;
		if(statbtp==-1)statbtp=0;

	}
	else if(flagd==1&&KeyCode==KeyUp0)
	{
		flagd=0;
		tmpfx--;
	}
	else if(flagu==1&&KeyCode==KeyDown0)
	{
		flagu=0;
		tmpfx++;
	}
	else if(KeyCode==KeyUp0)
	{
		tmpfx--;
	}
	else if(KeyCode==KeyDown0)
	{
		tmpfx++;
	}
	if(tmpfx==(uy-1))flagd=1;
	if(tmpfx==0)flagu=1;

	InvalidateRect(hwnd,&rect,false);

	char str[20];
	MySetShowStyle();
	HPEN hpen=t_hpen;
	HPEN oldhpen=(HPEN)SelectObject(hdcTmp,hpen);

	HBRUSH hbrush = t_hbrush;
	HBRUSH hbrold = (HBRUSH)SelectObject(hdcTmp, hbrush); 

	Rectangle(hdcTmp,10,55,785,heronu>5?475:(heronu+1)*60+55); 	
		
	SelectObject(hdcTmp, hbrold); 
	DeleteObject(hbrush);
	SelectObject(hdcTmp,oldhpen);
	DeleteObject(hpen);
	

	int gb=statf; 
	if(listno==1)
	{
		gb=gb+goodnum;
	}

	int ga=gb;

	if(statf2==0)
	{
		ShowText(GetStrText("GOODS_ATRR_STR0"),40,60);
		ShowText(GetStrText("GOODS_ATRR_STR1"),300,60);
		ShowText(GetStrText("GOODS_ATRR_STR2"),520,60);
		int tmp=(heronu>5?6:heronu);
		int h=0;

		for(int g=statf;g<tmp+statf;g++)
		{
			GetGoodName(ga);
			GetGoodUse(ga);
			ShowText(goodname,40,120+60*h);
			ShowText(gooduseway,300,120+60*h);
			sprintf_s(str,"%d",good[goodowned[ga]-1].wuliup);
			ShowText(str,530,120+60*h);
			h++;
			ga++;
		}
	}
	else if(statf2==1)
	{
		ShowText(GetStrText("GOODS_ATRR_STR0"),40,60);
		ShowText(GetStrText("GOODS_ATRR_STR3"),300,60);
		ShowText(GetStrText("GOODS_ATRR_STR4"),520,60);
		int tmp=(heronu>5?6:heronu);
		int h=0;
		for(int g=statf;g<tmp+statf;g++)
		{
			GetGoodName(ga);
			ShowText(goodname,40,120+60*h);
			sprintf_s(str,"%d",good[goodowned[ga]-1].ziliup);
			ShowText(str,300,120+60*h);
			sprintf_s(str,"%d",good[goodowned[ga]-1].speedup);
			ShowText(str,530,120+60*h);
			h++;
			ga++;
		}
	}
	else if(statf2==2)
	{
		ShowText(GetStrText("GOODS_ATRR_STR0"),40,60);
		ShowText(GetStrText("GOODS_ATRR_STR5"),300,60);

		int tmp=(heronu>5?6:heronu);
		int h=0;
		for(int g=statf;g<tmp+statf;g++)
		{
			GetGoodName(ga);
			ShowText(goodname,40,120+60*h);
			GetGoodChange(ga);
			ShowText(goodchange,300,120+60*h);

			h++;
			ga++;
		}
	}

	BitBlt(hdcTmp,15,tmpfx*60+120,60*ux+60,50,hdcTmp,15,tmpfx*60+120,DSTINVERT);

}

int GoodList::KeyDown(WPARAM KeyCode)
{
	wlogfile(1,__FUNCTION__,"开始",KeyCode);
	if (goodnum==0&&listno==0)
	{
		return 0;
	}
	if (herogoodnum==0&&listno==1)
	{
		return 0;
	}
	int heronu;
	heronu=goodnum;
	if(listno==1)
	{
		heronu=herogoodnum;
	}

	int tmp=heronu-6;
	if (tmp<=0)statf=0;
	if(KeyCode==KeyDown0)
	{
		count++;
		if(count>5)
		{
			statf++;
			count--;
		}
		if(statf==tmp+1)statf=tmp;
		if (tmp<=0)statf=0;
		Show(KeyCode);
		return 0;
	}
	else if(KeyCode==KeyUp0)
	{
		count--;
		if(count<0)
		{
			statf--;
			count++;
		}
		if(statf==-1)statf=0;
		if (tmp<=0)statf=0;
		Show(KeyCode);
		return 0;
	}
	else if(KeyCode==KeyReturn)
	{
		return 1;
	}
	else if(KeyCode==KeyEsc)
	{
		return -1;
	}
	else if(KeyCode==KeyRight0)
	{
		statf2++;
		if(statf2==3)statf2=2;
		Show(KeyCode);
		return 0;
	}
	else if(KeyCode==KeyLeft0)
	{
		statf2--;
		if(statf2==-1)statf2=0;
		Show(KeyCode);
		return 0;
	}
	else
	{
		return 0;
	}

}



void GoodList::GetGoodNum()
{
	wlogfile(1,__FUNCTION__,"开始",0);
	for(int gg=0;gg<GOODMAX;gg++)goodowned[gg]=0;
	int ii=0;
	goodnum=0;
	herogoodnum=0;
	//goodtoheronum=0;
	for(int g=0;g<GOODMAX;g++)
	{
		if((good[g].hiscity-1)==number&&good[g].finded==1&&good[g].owner==0)//自己城市道具
		{
			goodowned[ii]=good[g].no;
			goodnum++;
			ii++;
		}
	}
	for(int g=0;g<GOODMAX;g++)
	{
		if(/*(good[g].hiscity-1)==number&&*/good[g].finded == 1 && good[g].owner==goodowner && good[g].style==0)//该武将道具
		{
			goodowned[ii]=good[g].no;
			herogoodnum++;
			ii++;
		}
	}

	/*if((good[g].hiscity-1)==number&&good[g].finded==1&&daojuflag==2)//自己城市武将的道具
	{
	goodowned[ii]=good[g].no;
	goodnum++;
	ii++;
	}*/


}


void GoodList::GetCityNum()
{
	wlogfile(1,__FUNCTION__,"开始",0);
	//ChangeRes(name);
	ifstream infile("Res\\City\\postion.sdt",ios::in);;
	int tmpx;
	int tmpy;
	int o=0;
	while(infile>>tmpx)
	{
		infile>>tmpy;
		o++;
		if(tmpx==unx&&tmpy==uny)
		{
			number=o-1;
			break;
		}

	}
	infile.close();	
	//ChangeRes(name);
}


void GoodList::GetGoodName(int g)
{
	wlogfile(1,__FUNCTION__,"开始",0);
	OverflowCheck(__FILE__,__LINE__,GOODMAX,goodowned[g]-1);
	OverflowCheck(__FILE__,__LINE__,GOODMAX,g);
	sprintf_s(goodname,"%s",t_goodName[goodowned[g]-1]);
}




void GoodList::GetGoodChange(int g)
{	
	wlogfile(1,__FUNCTION__,"开始",0);

	if(good[goodowned[g]-1].style ==0)
	{	
		sprintf_s(goodchange,"%s",GetStrText("ATRR_STR70"));

		return;
	}
	else
	{
		int x=good[goodowned[g]-1].bingzhong;
		if (x==0)sprintf_s(goodchange,"%s",GetStrText("ATRR_STR11"));
		else if (x==1)sprintf_s(goodchange,"%s",GetStrText("ATRR_STR12"));
		else if (x==2)sprintf_s(goodchange,"%s",GetStrText("ATRR_STR13"));
		else if (x==3)sprintf_s(goodchange,"%s",GetStrText("ATRR_STR14"));
		else if (x==4)sprintf_s(goodchange,"%s",GetStrText("ATRR_STR15"));
		else if (x==5)sprintf_s(goodchange,"%s",GetStrText("ATRR_STR16"));

	}

}

void GoodList::GetGoodUse(int g)
{
	wlogfile(1,__FUNCTION__,"开始",0);
	OverflowCheck(__FILE__,__LINE__,GOODMAX,goodowned[g]-1);
	OverflowCheck(__FILE__,__LINE__,GOODMAX,g);
	if(good[goodowned[g]-1].style==0)sprintf_s(gooduseway,"%s",GetStrText("GOODS_USEFLAG0"));
	else if (good[goodowned[g]-1].style==1)sprintf_s(gooduseway,"%s",GetStrText("GOODS_USEFLAG1"));


}


void GoodList::GetListStyle(int x)
{
	if(x==-1)
	{
		listno=0;//自己城市道具队列
	}
	else
	{
		listno=1;//自己城市武将道具队列
		goodowner=x;
	}
}


