/////////////////////////////////////////////////////////////////////////////
// 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();
}
/////////////////////////////////////////////////////////////////////////////
// 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();
}
/////////////////////////////////////////////////////////////////////////////
// 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();
}
/////////////////////////////////////////////////////////////////////////////
// setup
/////////////////////////////////////////////////////////////////////////////
BOOL CTradeJungleDialog::OnInitDialog() 
{
	int i;
	int nRes = 0;
	int nChoice = 0;
	BOOL bSingles;
	CDC *dc;
	CString str;
	CHoverStatic *pStatic;
	CButtonST *pButton;
	HDC hDC;

	CHelpDialog::OnInitDialog();

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

	//set up all the statics and bitmaps
	for(i = 0; i < 9; i++)
	{
		//get the res
		switch(i / 3)
		{
		case 0: nRes = RES_WHEAT; break;
		case 1: nRes = RES_SHEEP; break;
		case 2: nRes = RES_ORE; break;
		}

		//bitmaps
		pButton = &m_W2Button + i;

		//set the bitmaps
		pButton->SetBitmaps(VIEW->getResImage(nRes, hDC), COLOR_TRANSP, VIEW->getResImage(nRes, hDC), COLOR_TRANSP);
		pButton->EnableWindow(FALSE);

		//statics
		pStatic = &m_W2Static + i;

		//set the properties
		pStatic->SetFont(Tahoma12);
		pStatic->SetCenter();

		//set the text
		pStatic->SetText(VIEW->getResName(nRes));
	}
	
	//release the device context
	ReleaseDC(dc);

	//determine if we show single cards or not
	bSingles = (1 == (m_nJungles - m_nNeeded));

	//set top text
	str.Format("Which card%s would you like to keep?", bSingles ? "" : "s");
	SetWindowText(str);

	//show or hide the single options
	showSingles(bSingles);

	//if we've got singles and 2 jungles, it means we have to hide the one card
	//choice they don't have
	if((TRUE == bSingles) && (2 == m_nJungles))
	{
		//look at the player's res count for each
		if(0 == GAME->m_players[m_nPlayer].m_iRes[RES_WHEAT])
		{
			m_WButton.ShowWindow(SW_HIDE);
			m_WStatic.ShowWindow(SW_HIDE);
			m_1Radio.ShowWindow(SW_HIDE);
			nChoice = 1;
		}
		else if(0 == GAME->m_players[m_nPlayer].m_iRes[RES_ORE])
		{
			m_OButton.ShowWindow(SW_HIDE);
			m_OStatic.ShowWindow(SW_HIDE);
			m_2Radio.ShowWindow(SW_HIDE);
		}
		else
		{
			m_SButton.ShowWindow(SW_HIDE);
			m_SStatic.ShowWindow(SW_HIDE);
			m_3Radio.ShowWindow(SW_HIDE);
		}
	}

	//initial set radio selection
	switch(nChoice)
	{
	case 0:	m_1Radio.SetCheck(TRUE); break;
	case 1: m_2Radio.SetCheck(TRUE); break;
	case 2: m_3Radio.SetCheck(TRUE); break;
	}

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}