#include "main.h" 
#include "item.h"

#ifndef _MENU_H_
#define _MENU_H_ 

#define BRD_SPC 1
class Menu
{
	public:	
		Menu(	std::string name, 
				Item *items, 
				int level, int no, int link, 
				int orientation,
				int xBeg, int yBeg, 
				int itemCount); 	
		~Menu();
		
		//Functions	
		void initBox(); // Inits the box dimentions and sets it up with basic parameters
		void drawBox();
		void deleteMenu(); 
		void startDraw(); 
		void endDraw(); 
		void update();
		void printItems(); 
		void selectItem(); 
		void findTheLongestItem();
		void printMenu();
		void handleTrigger(char ch);
		bool isActive();

		//Getters 
		int getSizeX(); 
		int getSizeY(); 
		int getBegX();
		int getBegY(); 
		int getLongestItem(); 
		int getSelectedItem(); 
		int getOrientation();
		char getKey();

		//Setters
		void setBegX(int x); 
		void setBegY(int y);
		void setTopLine(char ch);
		void setBottomLine(char ch);
		void setLeftLine(char ch);
		void setRightLine(char ch);
		void setActive();
		void setInactive();

	private:	
		WINDOW *menuWin; // window fo this menu 
		std::string name; // Name of the menu
		Item *items; // Items plresent for this menu
		int orientation = 0; // 0 = Horizontal 1 = Vertical
		int level = 0; // Hierarchical level of the menu 0 is the top one
		int no = 0; // To be able to track echa menu indvidualy
		int xBeg, yBeg = 0; // Beginning coordinates of the menu window
		int xSize, ySize = 0; // Size of the menu window
		int itemCount = 0; // Number of elents in this menu	
		int selectedItem = 0; // Currenlty selected Item on the menu
		int link = 0; //If this menu is linked to anothar one // level 0 menus can not be linked
		int longestItem = 0; //Lenght Of the Longest item
		int i, pos = 0;
		bool activeMenu = false;
};

Menu::Menu(	std::string name, 
			Item *items, 
			int level, int no, int link, 
			int orientation,
			int xBeg, int yBeg, 
			int itemCount)	
{
	this->name = name; 
	this->items = items;
	this->level = level; 
	this->no = no; 
	this->link = link; 
	this->orientation = orientation; 
	this->xBeg = xBeg; 
	this->yBeg = yBeg; 
	this->itemCount = itemCount;
	
	longestItem = 0; 
	xSize = 0;
	ySize = 0; 
	selectedItem = - 1; 
	activeMenu = false;

	initBox();
}	

//TODO: The correct way to end or erase s window is still unclear
Menu::~Menu()
{
	deleteMenu();
	werase(menuWin);
}

void Menu::initBox()
{
	pos = 1; // So that it doesn write over the border

	findTheLongestItem(); 
	
	// if Horizontal aligment else vertical
	if(orientation)
	{	
		xSize = longestItem + 2; 

		for(i = 0; i < itemCount ; i ++) 
		{
			this->items[i].setPosY(pos); 
			this->items[i].setPosX(1); // So tha it write in the middle
			pos ++; 
		}
		ySize = itemCount + 2;
	}
	else							 
	{
		for(i = 0; i < itemCount ; i ++) 
		{
			items[i].setPosX(pos);
			items[i].setPosY(1);
			pos += this->items[i].getLength() + 1; 
		}
		xSize = pos;		
		ySize = 3;
	}	

	// Creates a box with caclulated dimention xSize & ySize and user given position
	menuWin = newwin(ySize, xSize, yBeg, xBeg); 
}

//Functions	
void Menu::drawBox()
{
	//menuWin = newwin(5, 5, 5, 5); 
	box(menuWin,0,0);	//Here we can define other cahr insterad of 0,0
	wrefresh(menuWin);	//Refresh so that we can see it
}


//TODO: The correct way to end or erase s window is still unclear
void Menu::deleteMenu()
{
	wclear(menuWin); 
	endDraw();	
}
 
void Menu::startDraw()
{
	
}
 
void Menu::endDraw()
{
	wrefresh(menuWin);	//Refresh so that we can see it
}
 
void Menu::update()
{
	
}

void Menu::printItems()
{
	for(i=0 ; i < itemCount; i++)
	{ 	
		if(i == selectedItem)
		{
			wattron(menuWin, A_STANDOUT);
			mvwprintw(menuWin, items[i].getPosY(),items[i].getPosX(), items[i].getName().c_str());
			wattroff(menuWin, A_STANDOUT);
		}
		else 
		{
			mvwprintw(menuWin, items[i].getPosY(),items[i].getPosX(), items[i].getName().c_str());
		}	
	}
	selectedItem = -1; // Deselcting the item
}
 
void Menu::selectItem()
{
	
}
 
void Menu::findTheLongestItem()
{
	for(i=0; i < itemCount; i++)
	{
		if(longestItem < items[i].getLength())
		{
			longestItem = items[i].getLength();
		}
	}
}

void Menu::printMenu()
{
	drawBox(); 
	printItems();
	wrefresh(menuWin);	//Refresh so that we can see it
}
	
// Handles user input but there shoud be a way to register the selection. 
void Menu::handleTrigger(char ch)
{
	for(i=0; i < itemCount; i++)
	{
		if(ch == items[i].getTrigger())
		{
			selectedItem = i;
			activeMenu = true;
		}
		else
		{
			activeMenu = false;
		}
	}
}

bool Menu::isActive()
{
	return activeMenu;
}

//Getters 
int Menu::getSizeX()
{
	return xSize;	
}
 
int Menu::getSizeY()
{
	return ySize;	
}
 
int Menu::getBegX()
{
	return xBeg; 	
}

int Menu::getBegY()
{
	return yBeg; 	
}
 
int Menu::getLongestItem()
{
	return longestItem;	
}
 
int Menu::getSelectedItem()
{
	return selectedItem;	
}
 
int Menu::getOrientation()
{
	return orientation;
}


//Setters
void Menu::setBegX(int x)
{
	this-> xBeg = x;	
}
 
void Menu::setBegY(int y)
{
	this-> yBeg = y;	
}

void Menu::setTopLine(char ch)
{
	
}

void Menu::setBottomLine(char ch)
{
	
}

void Menu::setLeftLine(char ch)
{
	
}

void Menu::setRightLine(char ch)
{
	
}

char Menu::getKey()
{
	return wgetch(menuWin);
}

#endif
