#include "main.h" 
#include "menu.h"

#ifndef _MENU_BAR_H_
#define _MENU_BAR_H_ 

class MenuBar
{
	public:	
		MenuBar(Menu *menus, int direction, int xBeg, int yBeg, int menuCount); 	
		~MenuBar(); 	
		void drawBox();
		void drawMenus();
		void handleTrigger(char trig);
		char getKey(); 
		void reset();
		int getPosX(); 
		int getPosY();
		int getSizeX(); 
		int getSizeY(); 
	private: 
		int i, xSize, ySize, xBeg, yBeg, menuCount, direction, currPos = 0; 
		int selected_menu = -1;
		int prevSelected_menu = -1;
		char currIndex; 
		Menu *menus;
		WINDOW *menuBarWin; 
};

MenuBar::MenuBar(Menu *menus, int direction, int xBeg, int yBeg, int menuCount)
{
	this->menus = menus;			// All the menu class 
	this->direction = direction;	// Direction o the menu Bar Horizontal = 0, vertical = 1 
	this->xSize = xSize;			// Size x of the menuBar box
	this->ySize = ySize;			// Size y of the menuBar box
	this->xBeg = xBeg;				// beginnin x Coordinate for the menuBar Box 
	this->yBeg = yBeg;				// beginnin y Coordinate for the menuBar Box 
	this->menuCount = menuCount;	// Total Menu class element count
	
	// Jsut to be sure
	xSize = 0; 
	ySize = 0;
	selected_menu = -1; // No menus are selected a the beggining
	prevSelected_menu = - 1; // Deselect for the next itteration. (unless handleTrigger has a new trigger) 


	// Vertical Alignment	
	if(direction)
	{	
		currPos = 1; // So that it doesn write the first item on the top border
		// Finding the longest word between the disponible menus	
		for(i = 0; i < menuCount ; i ++) 
		{
			if(xSize < this->menus[i].getNameLenght()) 
			{
				xSize = this->menus[i].getNameLenght()+2; // +1 for left +1 lor right = +2
			}
		}	
		
		// Defining vertical separations
		for(i = 0; i < menuCount ; i ++) 
		{
			this->menus[i].setPosY(currPos); 
			this->menus[i].setPosX(1); // So tha it write in the middle
			currPos ++; 

			this->menus[i].setMenuBarBegX(xBeg); 
			this->menus[i].setMenuBarBegY(yBeg); 
			this->menus[i].setMenuBarSizeX(xSize); 
		}
		ySize = menuCount + 2;
	}
	else // Horizontal Alignment -> The Deffault 
	{
		currPos = 1; // So that it doesn write the first item on the left border
		for(i = 0; i < menuCount ; i ++) // Defining horizontal distances 
		{
			this->menus[i].setPosX(currPos);
			this->menus[i].setPosY(1); // So tha it write in the middle
			currPos += this->menus[i].getNameLenght() + 1; 
			
			this->menus[i].setMenuBarBegX(xBeg); 
			this->menus[i].setMenuBarBegY(yBeg); 
			this->menus[i].setMenuBarSizeX(xSize); 
		}
		xSize = currPos;		
		ySize = 3;
	}	
}

MenuBar::~MenuBar()
{
	werase(menuBarWin);
	wrefresh(menuBarWin);
}

void MenuBar::drawBox()
{	
	// Creates a box with caclulated dimention xSize & ySize and user given position
	menuBarWin = newwin(ySize, xSize, yBeg, xBeg); 
	box(menuBarWin,0,0);	//Here we can define other cahr insterad of 0,0
	wrefresh(menuBarWin);	//Refresh so that we can see it
}

// Draws menus and highlight the selected one selection in made on the handdleTrigger() Function
void MenuBar::drawMenus()
{
	for(i = 0; i < menuCount ; i ++) 
	{
		if(i == selected_menu) // If the iterration come to the selected menu than it will highlight it
		{
			prevSelected_menu = i; 
			wattron(menuBarWin, A_STANDOUT);
			mvwprintw(menuBarWin, menus[i].getPosY(),menus[i].getPosX(), menus[i].getName().c_str());
			wattroff(menuBarWin, A_STANDOUT);
			//menus[i].drawBox(); // Draws the lesefted Menus Items
		}
		else // Otherwise do not hightlight
 		{
			if(menus[i].isBoxCreated())
			{
				//menus[i].eraseBox(); // Draws the lesefted Menus Items
			}
			mvwprintw(menuBarWin, menus[i].getPosY(),menus[i].getPosX(), menus[i].getName().c_str());
		}
	}
	selected_menu = - 1; // Deselect for the next itteration. (unless handleTrigger has a new trigger) 
	prevSelected_menu = - 1; // Deselect for the next itteration. (unless handleTrigger has a new trigger) 
	wrefresh(menuBarWin); // so we can see it
}

// recieves a char an checks it fo ot corrensponds to a Menu Trigger
void MenuBar::handleTrigger(char trig)
{
	for(i = 0; i < menuCount ; i ++) 
	{
		if(trig == this->menus[i].getTrigger()) // If the recieved trigger matches the one from a menu
		{
			selected_menu = i; 
		}
	}
}

char MenuBar::getKey()
{
	return wgetch(menuBarWin);
}	

// Will draw the menu as for the first time
void MenuBar::reset()
{
	for(i = 0; i < menuCount ; i ++) 
	{
		mvwprintw(menuBarWin, menus[i].getPosY(),menus[i].getPosX(), menus[i].getName().c_str());
	}
	selected_menu = - 1; // Deselect for the next itteration. (unless handleTrigger has a new trigger) 
	wrefresh(menuBarWin); // so we can see it
}

int MenuBar::getPosX()
{
	return xBeg;	
}
 
int MenuBar::getPosY()
{
	return yBeg;	
}

int MenuBar::getSizeX()
{
	return xSize;
}
 
int MenuBar::getSizeY()
{
	return ySize;
}

#endif
