///////////////////////////////////////////////////////////////////////////
//
// File: ToolButton.cpp
//
// Description: Helper class for tool (mode) buttons
//
// Copyright 2010 Autodesk, Inc. All rights reserved.
// Use of this software is subject to the terms of the Autodesk license
// agreement provided at the time of installation or download, or which
// otherwise accompanies this software in either electronic or hard copy
// form.
//
///////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <fstream>
#include <string>
#include <stdarg.h>
#include <xsi_utils.h>
#include "ButtonBar.h"
#include "ToolButtonBarButton.h"

#include <xsi_application.h>


// Build circle points

ButtonBar::ButtonBar()
:	m_buttonCount( 0 )
{
	
}

ButtonBar::~ButtonBar()
{
}

void ButtonBar::Setup( LONG in_prevButtoncount , LONG in_totalButtonCount,CString in_folderpath, CString in_pluginPath, bool in_enum, CStringArray in_buttonNames, bool l_drawOutline )
{
	m_buttonCount	= in_buttonNames.GetCount();
	m_folderpath	= in_folderpath;
	m_pluginPath	= in_pluginPath;
	m_prevButtonCount = in_prevButtoncount;
	m_buttonTotalCount = in_totalButtonCount;
	m_enum	=	in_enum;
	m_drawOutline = l_drawOutline;
	m_selectedIndex = 0;
	AddButtons(  in_buttonNames );
	CleanButtonSelectionData(!in_enum);
	if(m_enum)
		m_buttons[0].SetSelected(true);
}

void ButtonBar::Cleanup()
{
	for (int i=0;i < m_buttonCount;i++)
		m_buttons[i].Cleanup();
	m_buttons.clear();	
}


void ButtonBar::Draw( ToolContext &in_ctxt )
{

	in_ctxt.BeginViewDraw();
		if (m_drawOutline)	{
			const LONG spacing = 4;
			LONG rect_x=0,rect_y=0, rect_w=0, rect_h;
			LONG rect1[4],rect[4];
			m_buttons[0].GetButtonPosition(in_ctxt, rect1);
			rect_h = rect1[3]-rect1[1];
			for (int i=0;i < m_buttonCount; i++)
			{	
				m_buttons[i].GetButtonPosition(in_ctxt, rect);
				if (rect[3]-rect[1] > rect_h)
					rect_h = rect[3]-rect[1];
				rect_w += rect[2]-rect[0];
				rect_y += (rect[3]-rect[1])/2+4;
			}
		//	rect_w -= 4;
			rect_x = rect1[0];
			rect_y = rect_y*2 / m_buttonCount-4;
		
			glEnable(GL_BLEND);
			glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
			glDepthMask( GL_FALSE );
			glEnable( GL_CULL_FACE );
			glDisable( GL_DEPTH_TEST );
			glColor4ub(55,55,55,50);
			glEnable(GL_POLYGON_SMOOTH);
		
			ogl_drawRoundedRectangle(rect_x,rect_y,rect_w,rect_h,10);
			glColor4ub(155,155,155,50);
			ogl_drawLinedRoundedRectangle(rect_x,rect_y,rect_w,rect_h,10,1.5);
		
			glDisable(GL_POLYGON_SMOOTH);
			glEnable( GL_CULL_FACE );
			glDisable(GL_BLEND);
			glEnable( GL_DEPTH_TEST );
			glDepthMask( GL_TRUE );
		}
		for (int i=0;i < m_buttonCount; i++)
		{	
			m_buttons[i].Draw(in_ctxt);
		}
	in_ctxt.EndViewDraw();
}

void ButtonBar::AddButtons(CStringArray in_buttonNames)
{
		CString slash = CUtils::Slash();

		CString texturePath, namelistPath=m_pluginPath;
#ifndef linux
		texturePath =  L".." + slash + L".." + slash; // On windows plugin is located two levels below...
		namelistPath += texturePath;
#endif
		texturePath += L"data" +slash+ m_folderpath + slash;

		for(int i=0;i < in_buttonNames.GetCount();i++)
		{
			CString bPath = texturePath + in_buttonNames[i];
			m_buttons.push_back(ToolButtonBarButton());
			m_buttons[i].Setup( bPath.GetWideString(), i+m_prevButtonCount, m_buttonTotalCount);
		}

}
void ButtonBar::SetSelection(ToolContext &in_ctxt, LONG mousex,LONG mousey)
{
	for(int i=0;i<m_buttonCount;i++)
	{
		if(m_buttons[i].IsPointInside(in_ctxt, mousex,mousey)) 
		{
			m_selectedIndex = i;
			m_buttons[i].SetSelected(true);
		}
		else 
			if (m_enum)	//only 1 button can be selected in button bar
				m_buttons[i].SetSelected(false);	
	}
}
void ButtonBar::SetSelection(LONG in_index)
{

	if (in_index < m_buttonCount && in_index >= 0 ) {
		m_selectedIndex = in_index;
		CleanButtonSelectionData(!m_enum);
		m_buttons[in_index].SetSelected(true);
	}
};

bool ButtonBar::IsPointInside( ToolContext &in_ctxt, LONG x, LONG y, LONG &out_index ) const
{
	for(int i=0;i<m_buttonCount;i++)
		if ( m_buttons[i].IsPointInside(in_ctxt,x,y) )	{
			out_index = i;
			return true;
		}
	return false;
}

void ButtonBar::CleanButtonSelectionData(bool setflag)
{
	for(int i=0;i<m_buttonCount;i++)
		m_buttons[i].SetSelected(setflag);
}