/*
        game.cpp (pokeri peli olio - yhteinen Windowsille & AmigaOS:lle)

        V1.00 - 181196  Kimmo Teräväinen
        -----   ------  ----------------
        Look game.h

*/

#include "game.h"
#include "handprfs.h"
#include "betprefs.h"
#include "double.h"
#include "savefile.h"

void ShowHandEvaluation(TWindow *,int);

const int Winnings[2][11] = {
//  0 1 2  3  4  5  6  7  8  9  10
//  - - - -- -- -- -- -- -- -- ---
  { 0,0,5,10,20,30,40,80,140,160,320 },
  { 0,0,5,10,20,30,40,80,180,160,320 }
};

void cGamePoker::CreateCards()
{
  for(int i=0; i<CARDS_MAX_LKM ; i++) {
    cards[i]=new cIMGCard(wnd,gfx,cPoint(0,0),cCard(i));
  }
}

void cGamePoker::DeleteCards()
{
  for(int i=0; i<CARDS_MAX_LKM ; i++) {
    if(cards[i]) delete cards[i]; cards[i]=NULL;
  }
}

void cGamePoker::MenuOld()
{
  cSaveFile file(req.Load());
  if(file.Load()==0) {
    InitPiles();
    dealpile.Shuffle();
    money=file.Points();
  }
}

void cGamePoker::MenuSave()
{
  cSaveFile file(req.Save());
  file.Points(money);
  if(file.Save()==0) {
    MenuNew();
    money=0;
  }
  req.Clear();
}

void cGamePoker::InitPiles()
{
  dealpile.Erase();     // Take cards away from piles
  junkpile.Erase();     //
  handpile.Erase();     //

  for(int i=0; i<52+jokers ; i++)   // and put them (cards) all
    dealpile.Insert(cards[i]);      // to the deal pile
}


void cGamePoker::GadgetBet()
{
  MenuPrefsBet();
}

void cGamePoker::GadgetShuffle()
{
  if(changed==-1) dealpile.Shuffle();
}


void cGamePoker::GadgetFinish()
{
  cIMGCard *card; int i=0;
  if(changed==-1) {
    if((int)money-jokers-changes<0) {
#ifdef _Windows
      wnd->MessageBox("Not enough money!","Poker Note");
#else
      EasyStruct myES = {
        sizeof(struct EasyStruct),0,
        (UBYTE*)"Poker Note",
        (UBYTE*)"Not enough money!",
        (UBYTE*)"OK"
      };
      EasyRequest(wnd, &myES, NULL, NULL);
#endif
      return;
    }
    money-=(jokers+changes);
  }
  changed++;                               // CHANGE:
  junkpile.Insert(handpile);               // turned -> to junkpile
  while(handpile.Can_Insert()) {           //
    card=dealpile.Deal(); if(!card) break; // deal new cards to empty
    handpile.Insert(card);                 // place in handpile
    i++;
  }

  if(i==0 || changed>=changes) {
    int win=0;

    handpile.FreezeAll();       // Prevent turning of handcards

    if(handpile.GetPrefs_5ofkind()) win=1;    // Calculate WinPot
    win=Winnings[win][handpile.Evaluate()];   //

    ShowHandEvaluation(wnd,handpile.Evaluate());   // Show Evaluation
    if(win) TDoubleDialog(this,wnd,win).Execute(); // If Win Pot -> Double

    dealpile.Insert(junkpile);  // Take cards to dealpile
    handpile.TurnAll();         // and shuffle
    dealpile.Insert(handpile);  //
    dealpile.Shuffle();         //
    changed=-1;
  }
}

void cGamePoker::MenuNew()
{
  money=40;
  InitPiles();
  dealpile.Shuffle();
  req.Clear();
}

void cGamePoker::MenuAbout()
{
#ifdef _Windows
  TDialog(wnd,DIALOG_ABOUT).Execute();
#else
  EasyStruct myES = {
    sizeof(struct EasyStruct),0,
    (UBYTE*)"About Destructive Poker",
    (UBYTE*)"       2nd April 1997\n"\
            ""\
            " Destructive Poker V1.01\n"\
            "            by\n"\
            "   Kimmo Teräväinen\n"\
            "\n"\
            "e-mail: kihete@cc.jyu.fi\n"\
            "\n"\
            "snail-mail:\n"\
            "Roninmäentie 6 L 28 B\n"\
            "40500 JYVÄSKYLÄ\n"\
            "FINLAND",
    (UBYTE*)"OK"
  };
  EasyRequestArgs(wnd, &myES, NULL, NULL);
#endif
}


void cGamePoker::MenuPrefsHand()
{
  dealpile.Insert(junkpile);
  handpile.TurnAll();
  dealpile.Insert(handpile);

  TPrefsHandDialog(&handpile,wnd).Execute();

  dealpile.Shuffle();
  changed=-1;
}


void cGamePoker::MenuPrefsBet()
{
  dealpile.Insert(junkpile);
  handpile.TurnAll();
  dealpile.Insert(handpile);

  TPrefsBetDialog(this,wnd).Execute();

  InitPiles();
  dealpile.Shuffle();
  changed=-1;
}

void cGamePoker::MenuHelp()
{
#ifdef _Windows
  wnd->WinHelp("poker.hlp",HELP_INDEX, 0);
#else
  Execute((UBYTE*)"run >NIL: SYS:Utilities/MultiView poker.guide",NULL,NULL);
#endif
}
