#define PDC_NCMOUSE
#include <curses.h>
#include <unistd.h> //sleep unix only
#include <time.h>  //clock

#include "../spec.h"
/* timing */

Let Clock(Co co){
    return co->lock;
}

void Wait(Co ck){
    clock_t lastTick = ck->lastTick;
	clock_t rangetck = CLOCKS_PER_SEC / FPS;
	unsigned int usleepmax = 1000000 / FPS;
	unsigned int slp = usleepmax;
	clock_t nowtck = clock();
	clock_t difftck = nowtck - lastTick;
	if (difftck < rangetck){
		clock_t timeleft = rangetck - difftck;
		slp = usleepmax * timeleft / rangetck;
	}
	usleep(slp);
    ck->lastTick = nowtck;
    ck->lock = (ck->lock + 1 ) % MAX;
}

void CuInit(Co co){
	initscr();
	cbreak();
	noecho();
    curs_set(0);
	keypad(stdscr,1);
	mousemask(ALL_MOUSE_EVENTS, NULL);
	nodelay(stdscr,1);
	if (has_colors() == 0 ){
		printw("no color");
        endwin();
        exit(0);
	}
	else{
		start_color();
		use_default_colors();
        short ofg=0, obg=0;
        pair_content(-1,&obg, &ofg);
        short palette[] = {-1, COLOR_BLACK, COLOR_RED, COLOR_GREEN, COLOR_BLUE,
                              COLOR_CYAN, COLOR_MAGENTA, COLOR_YELLOW, COLOR_WHITE }; 
        for(Color c=T; c < Colors; c++){
            short fg = c%9;
            short bg = c/9;
            init_pair(c,palette[bg],palette[fg]);
        }
	}
    refresh();
    co->curpan = newpad(25,80);
}

void CuResize(Co co,Cnt w, Cnt h)
{
    if (w < 80) w=80;
    if (h < 25) h=25;
    WINDOW * old = co->curpan;
    co->curpan = newpad(h,w);
    wclear(old);
    delwin(old);
}

void CuClear(Co co)
{
    werase(co->curpan);
    wclear(co->curpan);
}

void CuExit(Co co){
    endwin();
    exit(0);
}

void CuRefresh(Co co,Cnt col, Cnt row, Cnt width, Cnt height){
    int x=1,y=1,w,h;
    getmaxyx(stdscr,h,w);
    w-=2; /* hack for h scroll tear */
    h-=1;

    if (width < w) w = width;
    else if (width - col < w) col = width - w;

    if (height < h) h = height;
    if (height - row < h) row = height - h;

    prefresh(co->curpan,
             row,
             col,
             y, x, h, w);
    //wnoutrefresh(stdscr);
    //doupdate();
}

void CuMouse(Co co){
    MEVENT mouse;
    if (Peek(co,'!'))
    if (getmouse(&mouse) == OK)
    {
        // here juste set the sprite cursor !!
        //            co->llection[MMAND][ClickX] = mouse.x;
        //            co->llection[MMAND][ClickY] = mouse.y;
    }
}

void Control(Co co){
    Clean(co);
        // if the key repeat, just ignore until it pop out
        // no way to handle multiple held key

	int k = 1;
	while (k != ERR){
        Poke(co,'*');
        //convert special key to simple value
        switch(k){
            case KEY_MOUSE:
                k = '!';
                break;
            case 27 : 
                if (getch() == ERR)
                {
                    k = 'q';
                }
                break;
            case KEY_UP:
                break;
            case KEY_LEFT:
                break;
            case KEY_RIGHT:
                break;
            case KEY_DOWN:
                break;
            case KEY_ENTER:
                k = '!';
                break;
        }
        Poke(co,k);
        k = getch();
	}
    flushinp();
}


/*
corner and lines
ACS_BSSB ACS_SSBB ... ACS_[SB]{4} you get it ?
ACS_SSSS is PLUS +
ACS_BBBB dont exist,  nor ACS_SBBB BSBB BBSB BBBS

*/
void CuSPlot(Co co, Let ch, Cnt col,Cnt row, Color cl){
    int frames[] = {0,ACS_CKBOARD, ACS_BULLET, ACS_DIAMOND,
                    ACS_LARROW, ACS_RARROW, ACS_UARROW, ACS_DARROW,  
                    ACS_S1, ACS_S3, ACS_S7, ACS_S9,
                    ACS_VLINE,ACS_HLINE,ACS_PLUS,
                    ACS_LLCORNER,ACS_ULCORNER,ACS_URCORNER,ACS_LRCORNER,
                    ACS_TTEE,ACS_RTEE,ACS_BTEE,ACS_LTEE,
                    0};
    int chw = ch;
    if (ch < Symbols) chw = frames[ch];
    wmove(co->curpan,row,col);
    wattrset(co->curpan,COLOR_PAIR(cl));
    wattron(co->curpan,A_BOLD);
    waddch(co->curpan,chw);
}


void CuFlash(Co co,Let rate, Color cl){
    if (flash() == 0){
        //short ofg=0, obg=0;
        //pair_content(-1,&obg, &ofg);
        //init_pair(-1,obg,ofg);
    }
}

void CuSay(Co co,Let * msg, Cnt col, Cnt row, Color cl){
    wmove(co->curpan,row,col);
    wattrset(co->curpan,COLOR_PAIR(cl));
    waddstr(co->curpan,msg);
}



