#include <windows.h>
#include <windowsx.h>
#include <stdio.h>
#include "../kernel/include/kernel.h"
#include "winscol.h"
#include "terminal.h"

int HtFONT=-1;
int VtFONT;

void Trepaint(terminal t)
{
    HDC DC;
    int i;

    DC=GetDC(t->hwnd);
    SetBkColor(DC,GetSysColor(COLOR_WINDOW));
    SelectFont(DC,GetStockFont(MYtFONT));
  
    for(i=0;i<VtTERM;i++)
        TextOut(DC,0,i*VtFONT,&t->term[i*HtTERM],HtTERM);
    
    if (t->hcomm<HtTERM)
        TextOut(DC,0,VtTERM*VtFONT+2,t->comm,HtTERM);
    else
        TextOut(DC,0,VtTERM*VtFONT+2,&t->comm[t->hcomm-HtTERM+1],HtTERM);
}

void Tsetpos(terminal t,int x,int y)
{
    HDC DC=GetDC(t->hwnd);

    SetBkColor(DC,GetSysColor(COLOR_WINDOW));
    SelectFont(DC,GetStockFont(MYtFONT));

    if ((x<0)||(x>=HtTERM)||(y<0)||(y>=VtTERM)) return;
    t->term[t->y*HtTERM+t->x]=t->curs;
    TextOut(DC,t->x*HtFONT,t->y*VtFONT,&t->term[t->y*HtTERM+t->x],1);
    t->x=x; t->y=y;
    t->curs=t->term[t->y*HtTERM+t->x];
    if (t->state) t->term[t->y*HtTERM+t->x]=(char)tCURSEUR;
    TextOut(DC,t->x*HtFONT,t->y*VtFONT,&t->term[t->y*HtTERM+t->x],1);

//    ReleaseDC(t->hwnd,DC);
}

void Tsettitle(terminal t,char *title)
{
  SetWindowText(t->hwnd,title);
}

void Techostr(terminal t,char *p,int l)
{
    HDC DC=GetDC(t->hwnd);
    int i,ch,k;
    int x1,y1,x2,y2;

    SetBkColor(DC,GetSysColor(COLOR_WINDOW));
    SelectFont(DC,GetStockFont(MYtFONT));

    t->term[t->y*HtTERM+t->x]=t->curs;
    x1=x2=t->x;
    y1=y2=t->y;
    
    for(k=0;k<l;k++)
    {
        ch=*(p++);
        if (ch>=32)
        {
            t->term[HtTERM*t->y+t->x]=ch;
            t->x++;
        }
        else if (ch==13)
        {
            t->x=0;
        }
        else if (ch==8)
        {
            if (t->x) t->x--;
            else if (t->y)
            {
                t->x=HtTERM-1;
                t->y--;
            }
        }
        else if (ch==9)
        {
            t->x++;
        }
        else if (ch==10)
        {
            t->y++;
            t->x=0;
        }
        else if (ch==11)
        {
            if (t->y) t->y--;
        }
        else if (ch==12)
        {
            for(i=0;i<VtTERM*HtTERM;i++) t->term[i]=32;
            t->x=t->y=0;
            x1=y1=0;
            x2=HtTERM-1; y2=VtTERM-1;
        }        
        else if (ch==17) t->state=1;
        else if (ch==20) t->state=0;
        if (t->x>=HtTERM)
        {
            t->x=0;
            t->y++;
        }
        if (t->y>=VtTERM)
        {
            t->y=VtTERM-1;
            for(i=HtTERM;i<HtTERM*VtTERM;i++) t->term[i-HtTERM]=t->term[i];
            for(i=0;i<HtTERM;i++) t->term[t->y*HtTERM+i]=32;
            x1=y1=0;
            x2=HtTERM-1; y2=VtTERM-1;
        }
        if (t->y<y1) { x1=t->x; y1=t->y; }
        else if ((t->y==y1)&&(t->x<x1)) x1=t->x;
        if (t->y>y2) { x2=t->x; y2=t->y; }
        else if ((t->y==y2)&&(t->x>x2)) x2=t->x;        
    }

   t->curs=t->term[t->y*HtTERM+t->x];
   if (t->state) t->term[t->y*HtTERM+t->x]=(char)tCURSEUR;
   if (y1==y2)
   {
       TextOut(DC,x1*HtFONT,y1*VtFONT,&t->term[y1*HtTERM+x1],x2-x1+1);
   }
   else
   {
       TextOut(DC,x1*HtFONT,y1*VtFONT,&t->term[y1*HtTERM+x1],HtTERM-x1);
       for(i=y1+1;i<y2;i++)
           TextOut(DC,0,i*VtFONT,&t->term[i*HtTERM],HtTERM);
       TextOut(DC,0,y2*VtFONT,&t->term[y2*HtTERM],x2+1);
   }
//   ReleaseDC(t->hwnd,DC);
}

void Techochar(terminal t,int ch)
{
    char buf[2];

    buf[0]=ch;
    buf[1]=0;
    Techostr(t,buf,1);
}

void Tinchar(terminal t,int ch,char *res)
{
    HDC DC=GetDC(t->hwnd);
    int i;

    if (res) res[0]=0;
    
    SetBkColor(DC,GetSysColor(COLOR_WINDOW));
    SelectFont(DC,GetStockFont(MYtFONT));
    t->comm[t->hcomm]=32;

    if (ch==8)
    {
        if (t->hcomm>1) t->hcomm--;
        t->comm[t->hcomm]=32;
    }
    else if ((ch==13)||(ch==10))
    {
        if (res)
        {
            for(i=1;i<t->hcomm;i++) res[i-1]=t->comm[i];
            res[t->hcomm-1]=0;
        }
        for(i=0;i<HtCOMM;i++) t->comm[i]=32;
        t->hcomm=0;
        t->comm[t->hcomm++]=']';
    } 
    else if ((ch>=32)&&(t->hcomm<HtCOMM-1))
    {
        t->comm[t->hcomm]=ch;
        t->hcomm++;
    }
    t->comm[t->hcomm]=(char)tCURSEUR;
    
    if (t->hcomm<HtTERM)
        TextOut(DC,0,VtTERM*VtFONT+2,t->comm,HtTERM);
    else
        TextOut(DC,0,VtTERM*VtFONT+2,&t->comm[t->hcomm-HtTERM+1],HtTERM);

//    ReleaseDC(t->hwnd,DC);
}
    

void Tiniterm(terminal t,char *title,int cmdshow)
{
    int i;
    HDC DC;
    TEXTMETRIC tm;
    RECT r;

    if (HtFONT==-1)
    {
        DC=GetDC(hscol);
        SelectFont(DC,GetStockFont(MYtFONT));
        GetTextMetrics(DC,&tm);
        HtFONT=tm.tmAveCharWidth;
        VtFONT=tm.tmHeight+1;
        ReleaseDC(hscol,DC);
    }
	r.left=0;
	r.top=0;
	r.right=HtFONT*HtTERM;
	r.bottom=VtFONT*(VtTERM+1)+1;
    AdjustWindowRect(&r,WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_THICKFRAME|WS_MINIMIZEBOX,FALSE);
  
    t->hwnd=NULL;
    t->hwnd = CreateWindow(
        "GenericClass",         /* class */
        title,                  /* caption */
        WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_THICKFRAME|WS_MINIMIZEBOX,    /* style */
        CW_USEDEFAULT,          /* init. x pos */
        CW_USEDEFAULT,          /* init. y pos */
		r.right-r.left,
		r.bottom-r.top,
        NULL,                   /* parent window */
        NULL,                   /* menu handle */
        thisinst,               /* program handle */
        NULL                    /* create parms */
        );
    if (!t->hwnd) return;
    
    for(i=0;i<VtTERM*HtTERM;i++) t->term[i]=32;
    for(i=0;i<HtCOMM;i++) t->comm[i]=32;

    t->x=t->y=0;
    t->curs=32;
    t->term[0]=(char)tCURSEUR;
    t->state=1;
    
    t->hcomm=0;
    t->comm[t->hcomm++]=']';
    t->comm[t->hcomm]=(char)tCURSEUR;

    ShowWindow(t->hwnd,cmdshow);
    UpdateWindow(t->hwnd);

//    GetClientRect(t->hwnd,&r);

}

