#include <stddef.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <string.h>
#include <ncurses.h>
#include <stdlib.h>
#include <panel.h>



#define MSG_WIN_SIZE_ROWS 5

extern int title(WINDOW *win);
 

/*int main(void)
{
        WINDOW *win;
        char path[1024];
	char input[100] = {0};
 
        initscr();
        //cbreak();
        start_color();
 
        keypad(stdscr, TRUE);
		
	userInputBox("aaaaaweioroijewofjdl;kfjoiepwqhfiowjfpqwioefj", input);
	endwin();

      	printf("%s\n",input);	
        return 0;
}*/
 
 
char *userInputBox(char *message)
{
	WINDOW* lineWin;
	WINDOW* win;
	PANEL* linePanel;
	PANEL* panel;
	int ch = 0;
	char *input;
	int i;
	
	//init_pair(20, COLOR_GREEN, 	COLOR_GREEN);   
	//init_pair(21, COLOR_RED,	COLOR_GREEN);
	init_pair(22, COLOR_YELLOW,	COLOR_BLACK);

	input = (char *)malloc(sizeof(char) * PATH_MAX);

	int len = strlen(message);
	if(len < 20) len = 20;

	//테두리 윈도우 및 패널////////////////////////////
	lineWin = newwin(MSG_WIN_SIZE_ROWS+2, len+12,
			(LINES/2)-((MSG_WIN_SIZE_ROWS+2)/2),
			(COLS/2)-((len+12)/2));

	linePanel = new_panel(lineWin);
	box(lineWin, 0, 0);

	wrefresh(lineWin);
	show_panel(linePanel);
	update_panels();
	doupdate();

	//입력부 윈도우 및 페널////////////////////////////
	win = newwin(MSG_WIN_SIZE_ROWS, len+10, 
		(LINES/2)-(MSG_WIN_SIZE_ROWS/2),
		(COLS/2)-((len+10)/2) );

	panel = new_panel(win);

	/*wattron(win, COLOR_PAIR(20));
	for(i=0; i<len+10; i++)
		mvwprintw(win, 0, i," ");
	wattroff(win, COLOR_PAIR(20));*/

	wattron(win, COLOR_PAIR(22));
	mvwprintw(win, 0, ((len+10)/2)-(strlen(message)/2), "%s", message);
	wattroff(win, COLOR_PAIR(22));

	mvwhline(win, 1, 0, ACS_HLINE, len+12);

	wattron(win, COLOR_PAIR(22));
	mvwprintw(win, 2, 1, "input>>");
	wattroff(win, COLOR_PAIR(22));

	wrefresh(win);

	top_panel(panel);
	show_panel(panel);

	update_panels();
	doupdate();

	keypad(win, FALSE); 		// 일반 화면에서 키패드 이용 가능
	echo();
 	mvwgetstr(win, 2, 1+strlen("intput>>"), input);

	keypad(win, TRUE); 		// 일반 화면에서 키패드 이용 가능
	noecho();
	
 	
	wclear(win);
	hide_panel(panel);
	doupdate();
	delwin(win);
	del_panel(panel);

	wclear(lineWin);
	hide_panel(linePanel);
	update_panels();
	doupdate();
	delwin(lineWin);
	del_panel(linePanel);

	return input;
}
 




/*int title(WINDOW *win)
{
	int i=0, j=0;
	int y=0, x=0;


	getmaxyx(win, y, x);

	//for(i=0; i<y; i++)
	for(j=0; j<x; j++)
		mvwprintw(win, 1 ,j ," ");

	return 0;

}*/
