/////////////////////////////////////////////////////////////////////////////
// update the current player data
/////////////////////////////////////////////////////////////////////////////
void CTradeOfferDialog::updatePlayer()
{
	int i;
	int iPlayer;
	CButtonST *pButton;
	CHoverStatic *pText;
	CHoverStatic *pQty;
	CString str;

	//get this player
	iPlayer = GAME->m_iIndex;

	//go through and set up this players cards
	for(i = 0; i < 5; i++)
	{
		//get the controls
		pButton = &m_OHand1Button + i;
		pText = &m_OHand1Text + i;
		pQty = &m_Inhand1Qty + i;

		//add cards if they have them
		if(GAME->m_players[iPlayer].m_iRes[i])
		{
			//set the button
			pButton->ShowWindow(SW_SHOW);

			//set the text
			pText->ShowWindow(SW_SHOW);

			//enable windows
			pQty->EnableWindow();
			pQty->ShowWindow(SW_SHOW);

			//set the quantity
			str.Format("%d", GAME->m_players[iPlayer].m_iRes[i]);
			pQty->SetText(str);
		}
		else
		{
			pButton->ShowWindow(SW_HIDE);
			pText->ShowWindow(SW_HIDE);
			pQty->SetText("");
		}
	}
}
/////////////////////////////////////////////////////////////////////////////
// update other player trade data
/////////////////////////////////////////////////////////////////////////////
void CTradeOfferDialog::updateOTrade(BOOL bNew, int iPlace)
{
	CButtonST *pButton;
	CHoverStatic *pText;
	CHoverStatic *pQty;
	CString str;
	CDC *dc;
	HDC hDC;

	//get the device context
	dc = GetDC();
	hDC = dc->GetSafeHdc();

	//set the quantity pointer
	pQty = &m_OTrade1Qty + iPlace;

	//set the quantity
	str.Format("%d", m_iOTrade[m_iOTradePlace[iPlace]]);
	pQty->SetText(str);

	//see if we need to add a new card
	if(TRUE == bNew)
	{
		//set the bitmaps and unhide
		pButton = &m_OTrade1Button + iPlace;
		pText = &m_OTrade1Text + iPlace;

		//set the bitmaps
		pButton->SetBitmaps(VIEW->getResImage(m_iOTradePlace[iPlace], hDC), COLOR_TRANSP, VIEW->getResImage(m_iOTradePlace[iPlace], hDC), COLOR_TRANSP);
		pButton->ShowWindow(SW_SHOW);

		//set the text
		pText->SetText(VIEW->getResName(m_iOTradePlace[iPlace]));
		pText->ShowWindow(SW_SHOW);

		//show quantity
		pQty->ShowWindow(SW_SHOW);
	}

	//release the device context
	ReleaseDC(dc);
}
/////////////////////////////////////////////////////////////////////////////
// update the incoming trader players cards
/////////////////////////////////////////////////////////////////////////////
void CTradeOfferDialog::updateOPlayer()
{
	int i;
	CButtonST *pButton;
	CHoverStatic *pText;

	//go through and set up this players cards
	for(i = 0; i < 5; i++)
	{
		//get the controls
		pButton = &m_InHand1Button + i;
		pText = &m_Inhand1Text + i;
	
		//set the button
		pButton->ShowWindow(SW_SHOW);

		//set the text
		pText->ShowWindow(SW_SHOW);
	}
}
/////////////////////////////////////////////////////////////////////////////
// handle a click on a card currently in the trade queue
/////////////////////////////////////////////////////////////////////////////
void CTradeOfferDialog::HandleTradeClick(UINT nID)
{
	int i;
	int iQty;
	int iRes;
	int iPlace;
	int iPlayer;
	CString str;
	CButtonST *pButton;
	CHoverStatic *pText;
	CHoverStatic *pQty;

	//get the current place
	iPlace = (nID - IDC_TRADE1_BUTTON);

	//get the current player
	iPlayer = VIEW->getPlayer();

	//get the resource in question
	iRes = m_iTradePlace[iPlace];

	//subtract one from the amounts being traded
	m_iTrade[iRes]--;

	//get the quantity button
	pQty = &m_Trade1Qty + iPlace;

	//if it's a zero, remove this card from the trade stack
	if(m_iTrade[iRes] == 0)
	{
		//get the buttons
		pButton = &m_Trade1Button + iPlace;
		pText = &m_Trade1Text + iPlace;

		//hide them
		pButton->ShowWindow(SW_HIDE);
		pText->SetText("");
		pQty->SetText("");

		//set this value to empty
		m_iTradePlace[iPlace] = -1;

		//enable the other side
		pButton = &m_OHand1Button + iRes;
		pButton->EnableWindow();

		pText = &m_OHand1Text + iRes;
		pText->EnableWindow();

		pQty = &m_Inhand1Qty + iRes;
		pQty->EnableWindow();
	}
	else
	{
		//set the new quantity
		str.Format("%d", m_iTrade[iRes]);
		pQty->SetText(str);
	}

	//get the new in hand quantity
	iQty = MAP->m_iBankRes - m_iTrade[iRes];

	//if it's 1, then we need to re show this window and text
	if(iQty == 1)
	{
		//get the buttons
		pButton = &m_InHand1Button + iRes;
		pText = &m_Inhand1Text + iRes;

		//show them
		pButton->ShowWindow(SW_SHOW);
		pText->ShowWindow(SW_SHOW);
	}

	//check to see if we're not all empty
	for(i = 0, m_iNumTrade = 0; i < 4; i++)
	{
		if(m_iTradePlace[i] != -1)
		{
			m_iNumTrade++;
			break;
		}
	}

	//see if we can trade
	enableTrade();

	//set the changed flag
	m_bChanged = TRUE;

	//set the focus to the view
	VIEW->SetFocus();
}
/////////////////////////////////////////////////////////////////////////////
// handle a click on another player's card
/////////////////////////////////////////////////////////////////////////////
void CTradeOfferDialog::HandleOHandClick(UINT nID)
{
	int i;
	int iRes;
	int iQty;
	int iPlayer;
	BOOL bNew = FALSE;
	BOOL bInList = FALSE;
	CString str;
	CButtonST *pButton;
	CHoverStatic *pText;
	CHoverStatic *pQty;

	//get the resource type
	iRes = (nID - IDC_OHAND1_BUTTON);

	//get who we are
	iPlayer = GAME->m_iIndex;

	//search for it in the list
	for(i = 0; i < 4; i++)
	{
		//if we find it, or a -1, break;
		if(m_iOTradePlace[i] == iRes)
		{
			bInList = TRUE;
			break;
		}
	}

	if(FALSE == bInList)
	{
		for(i = 0; i < 4; i++)
		{
			//it's a new card
			if(m_iOTradePlace[i] == -1)
			{
				//set the place
				bNew = TRUE;
				m_iOTradePlace[i] = iRes;

				//disable this card on the other side
				pButton = &m_InHand1Button + iRes;
				pButton->EnableWindow(FALSE);

				pText = &m_Inhand1Text + iRes;
				pText->EnableWindow(FALSE);
				break;
			}
		}
	}

	//increase the number of cards being traded
	m_iOTrade[iRes]++;

	//get the onhand quantity
	iQty = GAME->m_players[iPlayer].m_iRes[iRes] - m_iOTrade[iRes];

	//get quantity pointer
	pQty = &m_Inhand1Qty + iRes;

	//if it's zero, remove this card
	if(iQty == 0)
	{
		//get the other pointers
		pButton = &m_OHand1Button + iRes;
		pText = &m_OHand1Text + iRes;

		//remove the texts
		pText->ShowWindow(SW_HIDE);
		pButton->ShowWindow(SW_HIDE);
		pQty->ShowWindow(SW_HIDE);
	}
	else
	{
		//set the new text
		str.Format("%d", iQty);
		pQty->SetText(str);
	}

	//update the trade data
	updateOTrade(bNew, i);

	//set the trade bool
	m_iNumOTrade++;

	//see if we can trade
	enableTrade();

	//set the changed flag
	m_bChanged = TRUE;

	//set the focus to the view
	VIEW->SetFocus();
}
/////////////////////////////////////////////////////////////////////////////
// show everything
/////////////////////////////////////////////////////////////////////////////
void CTradeOfferDialog::OnCounterButton() 
{
	int i;
	int iRes;
	int iPlayer;
	int iCount = 4;
	int nQty;
	CRect rect;
	CString str;
	CButtonST *pButton;
	CHoverStatic *pText;
	CHoverStatic *pQty;

	//kill the timer
	stopTimer();

	//send the reply
	VIEW->clientReplyOffer(m_uiTradeID, TRADE_COUNTER);

	//set new texts
	str.Format("Ask %s for these cards...", GAME->m_players[VIEW->getPlayer()].m_player.getName());
	m_PlayerStatic.SetWindowText(str);

	//set our text
	m_OPlayerStatic.SetWindowText("in exchange for these cards");

	//get who we are
	iPlayer = GAME->m_iIndex;

	//go through what the trader wants from us and weed out anything that
	//we don't have
	for(i = 0; i < 4; i++)
	{	
		//enable all other player trade windows
		pButton = &m_Trade1Button + i;
		pButton->EnableWindow();

		//disable this side, if necessary
		if(-1 != m_iTradePlace[i])
		{
			iRes = m_iTradePlace[i];

			pButton = &m_OHand1Button + iRes;
			pButton->EnableWindow(FALSE);

			pText = &m_OHand1Text + iRes;
			pText->EnableWindow(FALSE);

			pQty = &m_Inhand1Qty + iRes;
			pQty->EnableWindow(FALSE);
		}

		//skip ones that aren't there
		if(-1 == m_iOTradePlace[i])
		{
			iCount--;
			continue;
		}

		//get the res
		iRes = m_iOTradePlace[i];

		//get button
		pButton = &m_OTrade1Button + i;

		//enable window for trading
		pButton->EnableWindow();

		//determine if we have enough to match what they want
		if(GAME->m_players[iPlayer].m_iRes[iRes] < m_iOTrade[iRes])
		{
			//reset counter spot
			m_iOTradePlace[i] = -1;
			m_iOTrade[iRes] = 0;

			pText = &m_OTrade1Text + i;
			pQty = &m_OTrade1Qty + i;

			//hide all
			pText->ShowWindow(SW_HIDE);
			pQty->ShowWindow(SW_HIDE);
			pButton->ShowWindow(SW_HIDE);

			//we've changed the trade already
			m_bChanged = TRUE;

			//decrement counter
			iCount--;
		}
		else
		{
			//disable opposing side
			pButton = &m_InHand1Button + iRes;
			pButton->EnableWindow(FALSE);

			pText = &m_Inhand1Text + iRes;
			pText->EnableWindow(FALSE);

			//now correctly set how many cards should appear below

			//get the onhand quantity
			nQty = GAME->m_players[iPlayer].m_iRes[iRes] - m_iOTrade[iRes];

			//get quantity pointer
			pQty = &m_Inhand1Qty + iRes;

			//if it's zero, remove this card
			if(nQty == 0)
			{
				//get the other pointers
				pButton = &m_OHand1Button + iRes;
				pText = &m_OHand1Text + iRes;

				//remove the texts
				pText->ShowWindow(SW_HIDE);
				pButton->ShowWindow(SW_HIDE);
				pQty->ShowWindow(SW_HIDE);
			}
			else
			{
				//set the new text
				str.Format("%d", nQty);
				pQty->SetText(str);
			}
		}
	}

	//get screen rect
	GetWindowRect(&rect);

	//disable this button
	m_CounterButton.EnableWindow(FALSE);

	//disable the accept button so they dont' accidently press it
	m_OKButton.EnableWindow(FALSE);

	//show all
	SetWindowPos(&wndTop, 0, 0, rect.Width(), 260, SWP_NOMOVE | SWP_NOZORDER);

	//set the trade flag
	m_iNumOTrade = iCount;

	//enable/disable the offer button
	enableTrade();

	//set the focus to the view
	VIEW->SetFocus();
}
/////////////////////////////////////////////////////////////////////////////
// set offer data
/////////////////////////////////////////////////////////////////////////////
void CTradeOfferDialog::setOffer(int *iTo, int *iFor, UINT uiID)
{
	int i, j, k;
	int iRes;
	int iPlayer;
	BOOL bCanTrade = TRUE;
	BOOL bAnyTrade = FALSE;
	CRect rect;
	CRect rectClient;
	CString str;
	CDC *dc;
	HDC hDC;
	CHoverStatic *pStatic;
	CHoverStatic *pText;
	CButtonST *pButton;

	//reset trade counter
	m_iNumTrade = 0;

	//reset the changed flag
	m_bChanged = FALSE;

	//get the device context
	dc = GetDC();
	hDC = dc->GetSafeHdc();
	
	//get screen rect
	GetWindowRect(&rect);

	//get client rect
	GetClientRect(&rectClient);

	//enable counter button
	m_CounterButton.EnableWindow();

	//set data
	memcpy(m_iTrade, iTo, sizeof(m_iTrade));
	memcpy(m_iOTrade, iFor, sizeof(m_iOTrade));

	//reset trade places
	memset(m_iTradePlace, -1, sizeof(m_iTradePlace));
	memset(m_iOTradePlace, -1, sizeof(m_iOTradePlace));

	//set trade places
	for(i = 0, j = 0, k = 0; i < 5; i++)
	{
		if(0 < m_iTrade[i])
		{
			m_iTradePlace[j++] = i;		
		}
		if(0 < m_iOTrade[i])
		{
			m_iOTradePlace[k++] = i;
		}
	}

	//trade id
	m_uiTradeID = uiID;

	//set various text
	iPlayer = VIEW->getPlayer();

	//set title text
	str.Format("%s wants to trade!", GAME->m_players[iPlayer].m_player.getName());
	SetWindowText(str);

	//set static text
	str.Format("%s is offering these cards", GAME->m_players[iPlayer].m_player.getFirstName());
	m_PlayerStatic.SetWindowText(str);

	//set player to who we are
	iPlayer = GAME->m_iIndex;

	//update statics
	for(i = 0; i < 4; i++)
	{
		//get controls
		pText = &m_Trade1Text + i;
		pStatic = &m_Trade1Qty + i;
		pButton = &m_Trade1Button + i;

		//get the res
		iRes = m_iTradePlace[i];

		//show/hide from trader
		if(-1 != iRes)
		{
			m_iNumTrade++;
			pText->SetText(VIEW->getResName(iRes));
			pText->ShowWindow(SW_SHOW);
			pButton->SetBitmaps(VIEW->getResImage(iRes, dc->GetSafeHdc()), COLOR_TRANSP, VIEW->getResImage(iRes, dc->GetSafeHdc()), COLOR_TRANSP);
			pButton->ShowWindow(SW_SHOW);
			pButton->EnableWindow(FALSE);
			str.Format("%d", m_iTrade[iRes]);
			pStatic->SetText(str);
			pStatic->ShowWindow(SW_SHOW);
		}
		else
		{
			pText->ShowWindow(SW_HIDE);
			pButton->ShowWindow(SW_HIDE);
			pStatic->ShowWindow(SW_HIDE);
		}

		//get controls
		pText = &m_OTrade1Text + i;
		pStatic = &m_OTrade1Qty + i;
		pButton = &m_OTrade1Button + i;

		//get the res
		iRes = m_iOTradePlace[i];

		//show/hide
		if(-1 != iRes)
		{
			pText->SetText(VIEW->getResName(iRes));
			pText->ShowWindow(SW_SHOW);
			pButton->SetBitmaps(VIEW->getResImage(iRes, dc->GetSafeHdc()), COLOR_TRANSP, VIEW->getResImage(iRes, dc->GetSafeHdc()), COLOR_TRANSP);
			pButton->ShowWindow(SW_SHOW);
			pButton->EnableWindow(FALSE);
			str.Format("%d", m_iOTrade[iRes]);
			pStatic->SetText(str);
			pStatic->ShowWindow(SW_SHOW);
		}
		else
		{
			pText->ShowWindow(SW_HIDE);
			pButton->ShowWindow(SW_HIDE);
			pStatic->ShowWindow(SW_HIDE);
		}

		//see if they can even do this trade
		if(GAME->m_players[iPlayer].m_iRes[iRes] < m_iOTrade[iRes])
		{
			bCanTrade = FALSE;
		}
	}

	//reset the counteroffer buttons to be on (turned off in OnCounter)
	for(i = 0; i < 5; i++)
	{
		pButton = &m_OHand1Button + i;
		pButton->EnableWindow();

		pButton = &m_InHand1Button + i;
		pButton->EnableWindow();

		pText = &m_OHand1Text + i;
		pText->EnableWindow();

		pText = &m_Inhand1Text + i;
		pText->EnableWindow();

		pText = &m_Inhand1Qty + i;
		pText->EnableWindow();

		//see if we even have the ability to counteroffer
		if(0 < GAME->m_players[iPlayer].m_iRes[i])
		{
			bAnyTrade = TRUE;
		}
	}

	//enable/disable ability to trade
	m_OKButton.EnableWindow(bCanTrade);
	m_CounterButton.EnableWindow(bAnyTrade);

	//update the current player
	updatePlayer();	

	//update other player
	updateOPlayer();

	//set auto-reject timer
	m_iTime = GetPrivateProfileInt(INI_GENERAL, INI_TRADE_TIMEOUT, 15, INI_FILE);
	m_uiReject = SetTimer(TIMER_REJECT_TRADE, 1000, NULL);

	//set window pos sans counter	
	SetWindowPos(NULL, 0, 0, rect.Width(), rect.Height() - rectClient.Height() + 105, SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);

	//show ourselves
	ShowWindow(SW_SHOWNOACTIVATE);

	//release context
	ReleaseDC(dc);

	//set the focus to the view
	VIEW->SetFocus();
}