#include <cassert>
#include "controller.h"
#include "create_window.h"

void Controller::RysujKlawisz(HDC hdc, int n, bool obwodka)
{
   assert (n >= 0 && n < 3);
   UINT stan = _pressed[n] ? EDGE_SUNKEN : EDGE_RAISED;
   if (_n_buttons == 2 && n == 3)
      n = 2;

   RECT rect = {_x + _button_width*n, _y, _x + _button_width*(n+1), _y + _button_heigth};
   DrawEdge (hdc, &rect, stan, BF_MIDDLE | BF_RECT);
   if (obwodka)
      RysujObwodke(hdc);
}

void Controller::RysujUchwyt (HDC hdc, bool obwodka)
{
   RECT rect = 
   {
      _x, 
      _y + _button_heigth, 
      _x + _button_width*_n_buttons, 
      _y + _button_heigth*2
   };
   DrawEdge (hdc, &rect, EDGE_RAISED, BF_MIDDLE | BF_RECT);
   SetBkColor(hdc, RGB(128+64,128+64,128+64));
   DrawText (hdc, &_znak, 1, &rect, DT_VCENTER | DT_CENTER | DT_SINGLELINE);
   if (obwodka)
      RysujObwodke(hdc);
}

void Controller::RysujObwodke(HDC hdc)
{
    RECT rect = 
    {
       _x, 
       _y, 
       _x + _button_width*_n_buttons, 
       _y + _button_heigth*2
    };
    FrameRect (hdc, &rect, (HBRUSH) GetStockObject(BLACK_BRUSH));
}


void Controller::OnPaint(HDC hdc)
{
   if (_x < 0 || _y < 0)
   {
      int x = 9;
      x++;
   }
   for (int j = 0; j < _n_buttons; j++)
      RysujKlawisz(hdc, j, false);  
   RysujUchwyt(hdc, false);
   RysujObwodke(hdc);
}  




int Controller::OnMouseMove(short x, short y, HWND hwnd)
{
   RECT rect0 = {_x, _y, _x + _mouse_szer, _y + _mouse_wys};
   _x = x;
   _y = y;
   RECT rect1 = {_x, _y, _x + _mouse_szer, _y + _mouse_wys};
   RECT rect_suma;
   UnionRect(&rect_suma, &rect0, &rect1);

   HDC hdc = GetDC(hwnd);
   OnPaint(hdc);
   ReleaseDC(hwnd, hdc);
   InvalidateRect(hwnd, &rect_suma, true);
   ValidateRect(hwnd, &rect1);
   return 0;
}

int  Controller::OnButtonDown (int n)
{
   assert (n >= 0 && n < 3);
   SetCapture(_hwnd);
   _pressed[n] = true;
   HDC hdc = GetDC (_hwnd);
      RysujKlawisz(hdc, n);
   ReleaseDC(_hwnd, hdc);
   return 0;
}

int  Controller::OnButtonUp (int n)
{
   assert (n >= 0 && n < 3);
   ReleaseCapture();
   _pressed[n] = false;
   HDC hdc = GetDC (_hwnd);
      RysujKlawisz(hdc, n);
   ReleaseDC(_hwnd, hdc);
   return 0;
}

int Controller::OnChar (TCHAR znak)
{
   if (znak == VK_ESCAPE)
      SendMessage(_hwnd, WM_CLOSE,0,0);
   else if (znak == VK_SPACE)
      NoweOkno(0);
   else 
   {
      if (isgraph(znak))
         _znak = znak;
      else
         _znak = '?';
      HDC hdc = GetDC(_hwnd);
         RysujUchwyt(hdc);
      ReleaseDC(_hwnd, hdc);
   }
   
   return 0;
}

int Controller::OnKeyDown (int znak)
{
   RECT rect;
   GetWindowRect(_hwnd, &rect);
   SHORT n = GetKeyState(VK_CONTROL);
   if (n >= 0)  // ctrl not pressed
   {
      switch(znak)
      { 
      case VK_RIGHT: InflateRect(&rect,  1,  0); break;
      case VK_LEFT:  InflateRect(&rect, -1,  0); break;
      case VK_DOWN:  InflateRect(&rect,  0,  1); break;
      case VK_UP:    InflateRect(&rect,  0, -1); break;
      }
   }
   else
   {
      switch(znak)
      { 
      case VK_RIGHT: rect.left++; rect.right++  ; break;
      case VK_LEFT:  rect.left--; rect.right--  ; break;
      case VK_DOWN:  rect.top++;  rect.bottom++ ; break;
      case VK_UP:    rect.top--;  rect.bottom-- ; break;
      }
   }

   MoveWindow(_hwnd, 
              rect.left, 
              rect.top, 
              rect.right - rect.left, 
              rect.bottom - rect.top, 
              true);
   return 0;
}



