/*
* FX Editor Tabbed Class
* @version 7-12-06
* @author Joseph 'Pcjoe' Florencio
*/

#include "hud.h"
#include "cl_util.h"
#include "const.h"
#include "entity_state.h"
#include "cl_entity.h"
#include "vgui_loadtga.h"
#include "vgui_TeamFortressViewport.h"
#include "vgui_fxeditormisc.h"
#include "vgui_fxeditorwindow.h"
#include "vgui_fxeditortabbed.h"
#include "vgui_fxtoolpanel.h"
#include <VGUI_TextImage.h>

/*
* Tabbed Selector
*/
// Constructor
CHandler_TabSelect::CHandler_TabSelect( CTabbedWindow *pParent, CWindowTab *pTab )
{	m_pParent = pParent;
	m_pTab = pTab;
}

// Mouse Released
void CHandler_TabSelect::mouseReleased(MouseCode code,Panel* panel)
{	// If we're already the current tab, don't bother
	if(!m_pTab->isCurrentTab())
	{	// Set as current tab
		m_pParent->setCurrent(m_pTab);
	}
}

/*
* Single tab for tabbed manager
*/
#define TAB_PADDING 4
#define FADE_RATE 765.0 // How fast mouse overs fade
#define TAB_TEXT_PADDING 5

// Constructor
CWindowTab::CWindowTab( CTabbedWindow *pParent, CFadeImageButton *pBtn, CFadeImageButton *pBtnSel, Image *pPanelBK )
{	// Set variables to parameters
	setButton(pBtn);
	setButtonSel(pBtnSel);

	// set defaults
	setCurrentTab(false);
	setArmed(false);
	setFadeRate(FADE_RATE);
	setAlpha(255);
	// Panel will be re-adjusted by manager
	setPanel(new CImagePanel(pPanelBK, PANELTILE_ALL, 0, 0, 1, 1));
	getPanel()->setVisible(true);

	// set parent
	if(getButton())
	{	pParent->addChild(getButton());
		getButton()->setVisible(true);
		getButton()->resizeWidthToText(TAB_TEXT_PADDING);
		getButton()->addInputSignal( new CHandler_TabSelect( pParent, this ) );
	}
	if(getButtonSel())
	{	pParent->addChild(getButtonSel());
		getButtonSel()->setVisible(true);
		getButtonSel()->resizeWidthToText(TAB_TEXT_PADDING);
		getButtonSel()->addInputSignal( new CHandler_TabSelect( pParent, this ) );
	}
	// Bring standard button to front
	bringButtonToFront();
	pParent->addChild(getPanel());
}

// Set as Current
void CWindowTab::setAsCurrent( void )
{	setCurrentTab(true);
	bringPanelToFront();
	bringSelButtonToFront();
}

// Disable Current
void CWindowTab::disableCurrent( void )
{	setCurrentTab(false);
	bringButtonToFront();
}

// Bring panel to front
void CWindowTab::bringPanelToFront( void )
{	Panel *pParent = getPanel()->getParent();
	pParent->removeChild(getPanel());
	pParent->addChild(getPanel());
}

// Bring button to front
void CWindowTab::bringButtonToFront( void )
{	if(getButton())
	{	Panel *pParent = getButton()->getParent();
		pParent->removeChild(getButton());
		pParent->addChild(getButton());
	}
}

// Bring selected button to front
void CWindowTab::bringSelButtonToFront( void )
{	if(getButtonSel())
	{	Panel *pParent = getButtonSel()->getParent();
		pParent->removeChild(getButtonSel());
		pParent->addChild(getButtonSel());
	}
}

/*
* Tabbed window interface
*/

// Constructor
CTabbedWindow::CTabbedWindow(int x, int y, int wide, int tall) : CTransparentPanel(0,x,y,wide,tall)
{
}

/*
* Destructor
*/
CTabbedWindow::~CTabbedWindow()
{	// CWindowTabs arn't vgui objects, manually delete
	for( int i = 0; i < getTabAmount(); ++i )
	{	delete m_Tabs[i];
	}
	m_Tabs.clear();
}

// Resize panels to correspond with tabbed window size
void CTabbedWindow::resizePanels( void )
{	for( int i = 0; i < getTabAmount(); ++i )
	{	int wide=1,tall=1;
		int pWide,pTall;
		if(m_Tabs[i]->getButton())
			m_Tabs[i]->getButton()->getSize(wide,tall);
		// Use size of parent window
		m_Tabs[i]->getPanel()->getParent()->getSize(pWide,pTall);
		pTall -= tall;
		// Set accordingly
		m_Tabs[i]->getPanel()->setPos(0,tall);
		m_Tabs[i]->getPanel()->setSize(pWide,pTall);
	}
}

// Set Current
void CTabbedWindow::setCurrent( int iTab )
{	// Set as current tab
	if(iTab >= 0 && iTab < getTabAmount())
	{	m_Tabs[iTab]->setAsCurrent();
	}
	// Disable current/visibility for the remaining tabs
	for( int i = 0; i < getTabAmount(); ++i )
	{	if(i != iTab)
		{	m_Tabs[i]->disableCurrent();
		}
	}
}

// Set Current
void CTabbedWindow::setCurrent( CWindowTab *pTab )
{	for( int i = 0; i < getTabAmount(); ++i )
	{	if(m_Tabs[i] == pTab)
		{	setCurrent(i);
			break;
		}
	}
}

// Add Tab
CWindowTab *CTabbedWindow::addTab(char *pTitle)
{
	// Get font used for tabs
	CSchemeManager *pSchemes = gViewPort->GetSchemeManager();
	vgui::Font *pFont = pSchemes->getFont(pSchemes->getSchemeHandle( getFontHandleName().c_str() ));

	// Tab
	CFadeImageButton *pImgBtn = new CFadeImageButton(0,0,0,0,getImageVGUIBTGA((getTabPrefixName()+"_btn").c_str()),getImageVGUIBTGA((getTabPrefixName()+"_mo_btn").c_str()),pTitle);
	pImgBtn->getTextImage()->setColor(Color(0,0,0,0));
	pImgBtn->getTextImage()->setFont(pFont);

	// Selected tab
	CFadeImageButton *pImgSelBtn = new CFadeImageButton(0,0,0,0,getImageVGUIBTGA((getTabPrefixName()+"_sel_btn").c_str()),getImageVGUIBTGA((getTabPrefixName()+"_mo_btn").c_str()),pTitle);
	pImgSelBtn->getTextImage()->setColor(Color(0,0,0,0));
	pImgSelBtn->getTextImage()->setFont(pFont);

	// Create tab and add to local window's child list
	CWindowTab *pNewTab = new CWindowTab(this, pImgBtn, pImgSelBtn, getImageVGUI( getPanelBGName().c_str() ));

	// Set position
	if(!m_Tabs.empty())
	{	CWindowTab *pLastTab = m_Tabs[m_Tabs.size()-1];
		if(pLastTab->getButton())
		{	int wide,tall,x,y;
			pLastTab->getButton()->getSize(wide,tall);
			pLastTab->getButton()->getPos(x,y);
			// Create new position
			x += wide + TAB_PADDING;

			// Set corresponding positions for both buttons
			if(pNewTab->getButton())
				pNewTab->getButton()->setPos(x,y);
			if(pNewTab->getButtonSel())
				pNewTab->getButtonSel()->setPos(x,y);
		}
	}
	// Add to tabbed interface for easy management
	m_Tabs.push_back(pNewTab);
	
	// Constantly bring first tab to front if we're adding new entries
	m_Tabs[0]->setAsCurrent();

	// Resize panels
	resizePanels();

	return pNewTab;
}

// Get Tab Amount
int CTabbedWindow::getTabAmount( void )
{	return static_cast<int>(m_Tabs.size());
}

// Get Tab by Index
CWindowTab *CTabbedWindow::getTabByIndex( int idx )
{	return m_Tabs[idx];
}