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

#include "stdafx.h"
#include "ElevatorsDlg.h"
#include "TEffCriteriaCounter.h"

#include <math.h>

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

#include "TConfig.h"

void TEffCriteriaCounter::prestep()
{
   if( object.elev.checkButtons() )
   {
      for( int i = 0; i < MAX_FLOOR_NUMBER; i++ )
      {
         floorInfo[i][0].startFloorNumber = 0;
         floorInfo[i][1].startFloorNumber = 0;
      }
   }
}

void TEffCriteriaCounter::pressButton( int floor, int up )
{
   if( !object.elev.checkButtons() &&
       !object.button.checkButtons(0,MAX_FLOOR_NUMBER) )
   {
      floorInfo[floor-1][up].startFloorNumber = object.elev.getMovedFloorsNumber();
   }
   else
   {
      floorInfo[floor-1][up].startFloorNumber = 0;
   }
}

void TEffCriteriaCounter::unpressButton( int floor, int up )
{
//  полезная работа в том, что вообще доехал до вызова
//  затраченная работа - количество пройденных этажей

   if( floorInfo[floor-1][up].startFloorNumber )
   {
      int floors = object.elev.getMovedFloorsNumber() - 
                   floorInfo[floor-1][up].startFloorNumber;

      effCriteria = ((double)(MAX_FLOOR_NUMBER-floors))/MAX_FLOOR_NUMBER;

      floorInfo[floor-1][up].startFloorNumber = 0;

      for( int i = 0; i < config.elemToCheckNeedToAdapt*2 - 1; i++ )
         effCriteriaHistory[i] = effCriteriaHistory[i+1];

      effCriteriaHistory[config.elemToCheckNeedToAdapt*2 - 1] = effCriteria;
      elevatorsDlg->effCriteriaGraph.draw();

      countWhenAdapt++;
   }
}

void TEffCriteriaCounter::getOldAndNewKE( double* oldKE, double* newKE )
{
   for( int i = 0; i < config.elemToCheckNeedToAdapt; i++ )
      *oldKE += effCriteriaHistory[i]; 

   *oldKE /= (double)config.elemToCheckNeedToAdapt;

   for( i = config.elemToCheckNeedToAdapt; i < config.elemToCheckNeedToAdapt*2; i++ )
      *newKE += effCriteriaHistory[i];

   *newKE /= (double)config.elemToCheckNeedToAdapt;

   config.efficiencyCriteria = *newKE;
}

bool TEffCriteriaCounter::isNeedToAdapt()
{
   if( countWhenAdapt >= config.elemToCheckNeedToAdapt )
   {
      countWhenAdapt = 0;
      return true;
   }
   else
      return false;
}

void TEffCriteriaCounter::getECP( TEffCriteriaParms *ecp )
{
//   int effCritWhereChange; // (1 - упал, 2 - не менялся, 3 - вырос)
//   int isLongTimeAgoNotChange; //(1 - не менялся давно, 2 - менялся недавно)

   double oldKE = 0.0, newKE = 0.0;
   getOldAndNewKE( &oldKE, &newKE );
   
   if( fabs( newKE - oldKE ) > config.tresholdToCheckNeedToAdapt )
   {
      if( newKE > oldKE )
         ecp->effCritWhereChange = 3;
      else
         ecp->effCritWhereChange = 1;
   }
   else
      ecp->effCritWhereChange = 2;
}

void TEffCriteriaCounter::reset()
{
   for( int i = 0; i < config.elemToCheckNeedToAdapt*2; i++ )
      effCriteriaHistory[i] = config.efficiencyCriteria;
}

void TEffCriteriaCounter::init()
{
   effCriteriaHistory = new double[config.elemToCheckNeedToAdapt*2];
   reset();
}

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

TEffCriteriaCounter::TEffCriteriaCounter()
{
}

TEffCriteriaCounter::~TEffCriteriaCounter()
{

}

