// TButton.cpp: implementation of the TButton class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "TButton.h"
#include "TElevator.h"
#include "TControlSystem.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

TButton::TButton()
{
   for( int i = 0; i < MAX_FLOOR_NUMBER; i++ )
   {
      pressedButton[i].up = 0;
      pressedButton[i].down = 0;
   }
}

void TButton::reset()
{
   for( int b = 0; b < MAX_FLOOR_NUMBER; b++ )
      pressedButton[b].reset();
}

TButton::~TButton()
{

}

void TButton::pressButton( int floor, int up ) 
{ 
   if( !checkButton( floor, up ) )   
   {
      TCSMPressButtonParm parm;
      parm.floor = floor;
      parm.up    = up;

      controlSystem->messageHandler( CSM_PRESS_BUTTON, &parm );
   }

   if( up )
   {
      if( floor < floorNumbers ) pressedButton[floor-1].up = 1; 
   }
   else
   {
      if( floor > 1 ) pressedButton[floor-1].down = 1; 
   }
}

void TButton::unpressButton( int floor, int up  ) 
{ 
   if( checkButton( floor, up ) )   
   {
      TCSMPressButtonParm parm;
      parm.floor = floor;
      parm.up    = up;

      controlSystem->messageHandler( CSM_UNPRESS_BUTTON, &parm );
   }

   if( up )
      pressedButton[floor-1].up = 0; 
   else
      pressedButton[floor-1].down = 0; 
}

int TButton::checkButton( int floor, int up ) 
{ 
   switch( up )
   {
      case 1:
         return pressedButton[floor-1].up;
      
      case 0:
         return pressedButton[floor-1].down;
      
      default:
         return pressedButton[floor-1].down + pressedButton[floor-1].up;
   }
}


void TButton::draw( CDialog *wnd, CPaintDC *dc )
{
   // get our client rectangle
   CRect clientRect;
   wnd->GetClientRect( &clientRect );

   int x1 = 20 + number*2*ELEVATOR_XDISTANCE - ELEVATOR_WIDE/2;

   if( number >= 3 ) 
   {
      x1 += ELEVATOR_GROUP_DISTANCE;
   }

   CBrush dbrush;
   dbrush.CreateSysColorBrush(COLOR_WINDOW);
   CPen dpen( PS_SOLID, 1, RGB(255,0,0) );
   CBrush* pOldBrush = dc->SelectObject( &dbrush );
   CPen* pOldPen = dc->SelectObject( &dpen );

   for( int i = 0; i < floorNumbers; i++ )
   {
      if( pressedButton[i].up )
      {
         CPoint points[4];

         points[0].x = x1 - 3;
         points[0].y = clientRect.bottom - ELEVATOR_YCONST - (i+1)*ELEVATOR_HEIGHT + ELEVATOR_HEIGHT/2;

         points[1].x = x1;
         points[1].y = clientRect.bottom - ELEVATOR_YCONST - (i+1)*ELEVATOR_HEIGHT + ELEVATOR_HEIGHT/2 - 4;

         points[2].x = x1 + 3;
         points[2].y = clientRect.bottom - ELEVATOR_YCONST - (i+1)*ELEVATOR_HEIGHT + ELEVATOR_HEIGHT/2;
         
         points[3] = points[0];

         dc->Polyline( points, 4 );
      }

      if( pressedButton[i].down )
      {
         CPoint points[4];

         points[0].x = x1 - 3;
         points[0].y = clientRect.bottom - ELEVATOR_YCONST - (i+1)*ELEVATOR_HEIGHT + ELEVATOR_HEIGHT/2;

         points[1].x = x1;
         points[1].y = clientRect.bottom - ELEVATOR_YCONST - (i+1)*ELEVATOR_HEIGHT + ELEVATOR_HEIGHT/2 + 4;

         points[2].x = x1 + 3;
         points[2].y = clientRect.bottom - ELEVATOR_YCONST - (i+1)*ELEVATOR_HEIGHT + ELEVATOR_HEIGHT/2;

         points[3] = points[0];

         dc->Polyline( points, 4 );
      }
//         dc->Ellipse( x1 - 3,
//                      clientRect.bottom - ELEVATOR_YCONST - (i+1)*ELEVATOR_HEIGHT + ELEVATOR_HEIGHT/2 - 3,
//                      x1 + 3,
//                      clientRect.bottom - ELEVATOR_YCONST - (i+1)*ELEVATOR_HEIGHT + ELEVATOR_HEIGHT/2 + 3 );
   }

   dc->SelectObject(pOldBrush);
   dc->SelectObject(pOldPen);
}

int TButton::checkButtons( int up )
{
   int result = 0;
   for( int f = 1; f <= MAX_FLOOR_NUMBER; f++ )
      if( checkButton( f, up ) ) result++;

   return result;
}

int TButton::checkButtons( int lowFloor, int upFloor, int up )
{
   if( lowFloor > upFloor ) return 0;
   for( int f = lowFloor; f <= upFloor; f++ )
      if( checkButton( f, up ) ) return f;

   return 0;
}

int TButton::getUpperFloor( int up )
{
   for( int f = MAX_FLOOR_NUMBER; f >= 1; f-- )
      if( checkButton( f, up ) ) return f;

   return 0;
}

int TButton::getLowerFloor( int up )
{
   for( int f = 1; f <= MAX_FLOOR_NUMBER; f++ )
      if( checkButton( f, up ) ) return f;

   return 100;
}