#ifndef INCLUDED_DealerLogic_h_
#define INCLUDED_DealerLogic_h_
 
#include <stdlib.h>
#include "Behaviors/StateNode.h"
#include "Behaviors/Nodes/SoundNode.h"
#include "Behaviors/Nodes/LedNode.h"
#include "Behaviors/Transitions/CompletionTrans.h"
#include "Behaviors/Transitions/EventTrans.h"
#include "Behaviors/Transitions/TimeOutTrans.h"
#include "Events/EventRouter.h"
#include "Motion/WalkMC.h"
#include "Behaviors/Nodes/WalkNode.h"

class DealerLogic : public StateNode {
private:
  double weights[21];
  int lastVal;
  int numMoves;

public:
  int lastChoice;

  DealerLogic() : StateNode("Dealer Logic"),
	  lastVal(0), numMoves(0), lastChoice(0){}
 
  virtual void setup() {
    StateNode::setup();
    std::cout << getName() << " is setting up the state machine." << std::endl;
	 for(int i=0; i<21; i++){
		 weights[i] = 0.5;
	 }
  }

  virtual void DoStart() {
    StateNode::DoStart();
    std::cout << getName() << " is starting up." << std::endl;  
	 erouter->addTimer(this, 1000, 20, false);
  }


  int value(int val){
	  cout<<"dealerLogic value queried.\n";
	  numMoves++;
	  lastVal = val;
	  double num = rand();
	  if(num < weights[val]){
		  lastChoice = 1;
		  postCompletionEvent();
		  return 1;
	  }
	  else{
		  lastChoice = 2;
		  postCompletionEvent();
		  return 2;
	  }
  }

  void record(boolean isOver){
	  if(isOver && lastChoice){
		  weights[lastVal] = weights[lastVal] - weights[lastVal]/numMoves;
	  }
	  if(!isOver && lastChoice){
		  weights[lastVal] = weights[lastVal] + (1-weights[lastVal]/numMoves);
	  }
  }

  virtual void DoStop(){
	  std::cout<<getName()<<" is stopping"<<std::endl;
	  StateNode::DoStop();
  }
	  

protected:  // Dummy functions to satisfy the compiler
  DealerLogic(const DealerLogic&);
  DealerLogic& operator=(const DealerLogic&);

};




#endif

