/**
 * This is a menu made with ncurses, i wil do my best to comment every aspect of it
 **/

#include <ncurses.h>
//#include <string>

int main(int argc, char** argv)
{
	int i, xBeg, yBeg, std_xMax, std_yMax = 0;
	int choices, highlight = 0;
	char* menu_items[3]={"Menu1", "menu2", "Menu3"}; 

	// start of ncurses
	initscr(); 
	noecho(); 
	cbreak(); 

	getmaxyx(stdscr, std_yMax, std_xMax); 
		
	WINDOW * menu_win = newwin(6,std_xMax-5,std_yMax-8,5);
	keypad(menu_win, true); // so that we can use the arrow keys
	box(menu_win,0,0);
	refresh(); 
	wrefresh(menu_win); 	

	while(1)
	{
		for(i = 0; i<3; i++)
		{
			if(i==highlight)
			{
				wattron(menu_win, A_REVERSE);
			}	

			mvwprintw(menu_win, i+1, 1,menu_items[i]); 
			wattroff(menu_win, A_REVERSE);
		}	
		
		choices = wgetch(menu_win); 
		
		switch(choices) 
		{
			case KEY_UP:
				highlight --;
				if(highlight == -1) 
					highlight = 0; 
			break; 

			case KEY_DOWN: 
				highlight ++; 
				if(highlight == 3) 
					highlight = 2;
			break; 

			default: 
			break; 
		}
		if(choices == 10)// 10 = enter in asci
		{
			break; 
		}
	}
	
	printw("your choice was : %s", menu_items[highlight]);
	getch();
	endwin();	
}

int tutorial_0_1() // Hello Word and user input
{
	int c,y,x = 0; 

	y=10; 
	x=10; 

	//Init sequence. to set up the screen and init the memory usage
	initscr(); // inits the screen and sets up the screen

	move(y,x);// Moving to the given coordinates
	
	printw("Hello World!");
	//Refeshes the screen to match what is in the memory
	refresh(); 

	// Gets the user input
	c = getch(); 

	// Clreasthe screen
	clear(); 

	move(0,0);
	printw("You pressed %d", c); 

	// Gets the user input before quiting
	getch(); 

	// Delocates memory and ends ncurses
	endwin(); 
	return 0; 
}

int tutorial_2() // create a box and write into it
{
	// VAriable declarations
	int height, width, start_y, start_x = 0; 
	height = 10; 
	width = 30; 
	start_y = 10; 
	start_x = 10;

	//Init sequence. to set up the screen and init the memory usage
	initscr(); // inits the screen and sets up the screen
	
	// Creating a window 
	WINDOW * win= newwin(height,width,start_y, start_x); 
	//This refresh will initialize the base screen. 
	refresh(); 
	
	// Making a box out of the window
	box(win,0,0);

	// this will print on 0,0 coordinate of rhtis window
	wprintw(win,"this is an new window");

	//and this refresh will only refresh the created window
	wrefresh(win);

	//this will moove inside of the window an print to tth given coordinate
	mvwprintw(win,5,5,"Content of the box");

	//and this refresh will only refresh the created window
	wrefresh(win);

	// Gets the user input before quiting
	getch(); 

	// Delocates memory and ends ncurses
	endwin(); 
	return 0; 
}

int tuttorial_3 () // Customize the windows/boxes 
{
	// Variable declarations
	int height, width, start_y, start_x = 0; 
	height = 10; 
	width = 30; 
	start_y = 10; 
	start_x = 10;

	//Init sequence. to set up the screen and init the memory usage
	initscr(); // inits the screen and sets up the screen
	
	// To use the standart linux bash commanc lije ctrl-c ctrl-x and so on : Is active as default
	cbreak(); 

	// To use every input as raw input and not a command 
	//raw(); 

	// Doesn't show the pressed char in the scrren
	noecho();
	
	// Creating a window 
	WINDOW * win= newwin(height,width,start_y, start_x); 
	//This refresh will initialize the base screen. 
	refresh(); 
	
	// Making a box out of the window
	box(win,'|','#');

	// this will print on 0,0 coordinate of rhtis window
	wprintw(win,"this is an new window");

	//and this refresh will only refresh the created window
	wrefresh(win);

	getch();

	// Adwanced border customization variables
	int left, right, top, bottom, tlc, trc, blc, brc = 0; 
	left = '|'; // Left side of the box
	right = '/'; // Right side of the box
	top = '#'; // Top side of the box
	bottom = '-'; // Bottom side of the box
	tlc = '0'; // Top Left Corner of the box
	trc = '1'; // Top Roght Corner of the box 
	blc = '2'; // Bottom Left Corner of the box 
	brc = '3'; // Bottom Right Corner of the box

	// Adwanced border customization function call
	wborder(win, left, right, top, bottom, tlc, trc, blc, brc); 

	//and this refresh will only refresh the created window
	wrefresh(win);

	//this will moove inside of the window an print to tth given coordinate
	mvwprintw(win,5,5,"Content of the box");

	//and this refresh will only refresh the created window
	wrefresh(win);

	// Gets the user input before quiting
	getch(); 

	// Delocates memory and ends ncurses
	endwin(); 
	return 0; 
}

int tuttorial_4() // Ncurses Tutorial 4 - Attributes and Colors (attron, attroff, has_colors, start_color, init_pair)
{
	//Init sequence. to set up the screen and init the memory usage
	initscr(); // inits the screen and sets up the screen
	
	// To use the standart linux bash commanc lije ctrl-c ctrl-x and so on : Is active as default
	cbreak(); 

	// Doesn't show the pressed char in the scrren
	noecho();

	if(!has_colors())
	{
		printw("Terminal has no colorsupport"); 
		getch(); 
		return -1;
	}
	start_color(); 
	init_pair(1, COLOR_BLUE, COLOR_RED); 
	init_pair(2, COLOR_BLUE, COLOR_YELLOW); 

	attron(COLOR_PAIR(2));
	printw("Hello wold");	
	attroff(COLOR_PAIR(2));

	// Gets the user input before quiting
	getch(); 

	// Delocates memory and ends ncurses
	endwin(); 
	return 0; 
}

int tuttorial_5() // Get position
{
	int x, y, xBeg, yBeg, xMax, yMax = 0;
	// start of ncurses
	initscr(); 
	noecho(); 
	cbreak(); 

	WINDOW * win = newwin(10,20,10,10);
	getyx(stdscr, y, x); 
	getbegyx(win, yBeg, xBeg); 
	getmaxyx(stdscr, yMax, xMax); 

	printw("x:%d y:%d xBeg:%d yBeg:%d xMax:%d yMax:%d",x,y,xBeg,yBeg,xMax,yMax); 
	mvprintw(yMax/2, xMax/2, "Hello from the middle"); 

	getch();
	endwin();	
	
	return 0; 
}

int tutorial_6() // user inputs 
{
	int c, x, y, xBeg, yBeg, xMax, yMax = 0;
	// start of ncurses
	initscr(); 
	noecho(); 
	cbreak(); 

	getmaxyx(stdscr, yMax, xMax); 
	getyx(stdscr, y, x); 
		
	WINDOW * input_win = newwin(3,xMax-12,yMax-5,5);
	
	box(input_win,0,0); 
	keypad(input_win, true);
	refresh();	
	wrefresh(input_win);
	c = wgetch(input_win);
	if(c == 'j') 
	{
		mvwprintw(input_win,1,1,"you have pressed j! now press the up arrow key");	
		wrefresh(input_win);
	}
	
	c = wgetch(input_win);
	if(c == KEY_UP) 
	{
		mvwprintw(input_win,1,1,"                                                ");	
		wrefresh(input_win);
		mvwprintw(input_win,1,1,"you have pressed key up");	
		wrefresh(input_win);
	}
	getch();
	endwin();	
}
