///////////////////////////////////////////////////////////////////////////////
// $Id: MainDeck.cxx,v 1.1 1995/01/08 06:44:41 bmott Exp $
///////////////////////////////////////////////////////////////////////////////
//
// MainDeck.hxx - Main oonsoo deck of cards (Dealer's deck)
//
//
// Bradford W. Mott
// Copyright (C) 1994
// December 13,1994
//
///////////////////////////////////////////////////////////////////////////////
// $Log: MainDeck.cxx,v $
// Revision 1.1  1995/01/08  06:44:41  bmott
// Initial revision
//
///////////////////////////////////////////////////////////////////////////////

#include "UIApplication.hxx"
#include "Sprite.hxx"
#include "SpriteCollection.hxx"
#include "Card.hxx"
#include "MainDeck.hxx"
#include "Command.hxx"

// Structure to describe standard cards
typedef struct {
  int suite;
  int priority;
  char* name;
} StandardCard;

// Standard deck of oonsoo cards 
static StandardCard standardDeckOfCards[] = {
  {  1,  2,  "BushOne" },     
  {  1,  2,  "BushTwo" },
  {  1,  3,  "BushThree" },  
  {  1,  4,  "BushFour" },
  {  2,  2,  "HillOne" },
  {  2,  2,  "HillTwo" },
  {  2,  3,  "HillThree" },
  {  2,  4,  "HillFour" },
  {  3,  2,  "LeavesOne" },
  {  3,  2,  "LeavesTwo" },
  {  3,  3,  "LeavesThree" },
  {  3,  4,  "LeavesFour" },
  {  4,  2,  "MoonOne" },
  {  4,  2,  "MoonTwo" },
  {  4,  3,  "MoonThree" },
  {  4,  4,  "MoonFour" },
  {  5,  2,  "BlueStarOne" },
  {  5,  2,  "BlueStarTwo" },
  {  5,  3,  "BlueStarThree" },
  {  5,  4,  "BlueStarFour" },
  {  6,  2,  "RedBushOne" },
  {  6,  2,  "RedBushTwo" },
  {  6,  3,  "RedBushThree" },
  {  6,  4,  "RedBushFour" },
  {  7,  2,  "LilyOne" },
  {  7,  2,  "LilyTwo" },
  {  7,  3,  "LilyThree" },
  {  7,  4,  "LilyFour" },
  {  8,  2,  "RoseOne" },
  {  8,  2,  "RoseTwo" },
  {  8,  3,  "RoseThree" },
  {  8,  4,  "RoseFour" },
  {  9,  2,  "DaisyOne" },
  {  9,  2,  "DaisyTwo" },
  {  9,  3,  "DaisyThree" },
  {  9,  4,  "DaisyFour" },
  { 10,  2,  "MumOne" },
  { 10,  2,  "MumTwo" },
  { 10,  3,  "MumThree" },
  { 10,  4,  "MumFour" },
  { 11,  2,  "AzaleaOne" },
  { 11,  2,  "AzaleaTwo" },
  { 11,  3,  "AzaleaThree" },
  { 11,  4,  "AzaleaFour" },
  { 12,  1,  "HairOne" },
  { 12,  2,  "HairTwo" },
  { 12,  3,  "HairThree" },
  { 12,  4,  "HairFour" }
};


///////////////////////////////////////////////////////////////////////////////
// Construct a complete deck of oonsoo cards
///////////////////////////////////////////////////////////////////////////////
MainDeck::MainDeck(ContainerWidget* parent, const char *const widgetName,
    int x, int y, int width, int maxHeight, 
    SpriteCollection* sprites, Command* command)
    : Deck(parent, widgetName, x, y, width, maxHeight, sprites),
      myCommand(command)
{
  // Create each of the cards
  for(int t=0; t < (sizeof(standardDeckOfCards)/sizeof(StandardCard)); ++t)
  {
    Sprite* face = sprites->getByName(standardDeckOfCards[t].name);

    Card* card = new Card(face, standardDeckOfCards[t].suite,
        standardDeckOfCards[t].priority, FaceDown);

    myListOfCards.append(card);
  }

  // Update my view
  updateView();
}

///////////////////////////////////////////////////////////////////////////////
// Destructor
///////////////////////////////////////////////////////////////////////////////
MainDeck::~MainDeck()
{ 
  delete myCommand;
}

///////////////////////////////////////////////////////////////////////////////
// Update graphical view of myself
///////////////////////////////////////////////////////////////////////////////
void MainDeck::updateView()
{
  Pixmap pixmap;

  Sprite* back = mySprites->getByName("StandardBack");
  Sprite* emptyDeck = mySprites->getByName("EmptyDeck");

  int numberOfCards = myListOfCards.size();
  int cardWidth = back->width();
  int cardHeight = back->height();
  int drawingAreaHeight;
  int drawingAreaWidth;


  // See if I have any cards
  if(numberOfCards > 0)
  {
    int cardOffset;

    cardOffset = (myMaximumHeight - cardHeight) / 
        ((sizeof(standardDeckOfCards) / sizeof(StandardCard)) - 1); 
    drawingAreaHeight = cardHeight + (numberOfCards-1) * cardOffset;
    drawingAreaWidth = cardWidth;

    // Build pixmap to hold my view
    pixmap = XCreatePixmap(application->display(), myWindow,
        drawingAreaWidth, drawingAreaHeight, 8);

    int pixmapOffset = 0;
    for(Card* card = myListOfCards.first(); card != (Card*)0 ;
        card = myListOfCards.next())
    {
      Sprite* sprite;

      if(card != myListOfCards.tail())
        XPutImage(application->display(), pixmap, application->gc(),
            back->image(), 0, 0, 0, pixmapOffset, 
            back->width(), cardOffset);
      else
        XPutImage(application->display(), pixmap, application->gc(),
            back->image(), 0, 0, 0, pixmapOffset, 
            back->width(), back->height());

      pixmapOffset += cardOffset;
    }
  }
  else
  {
    Sprite* sprite = emptyDeck;
    drawingAreaWidth = sprite->width();
    drawingAreaHeight = sprite->height();

    // Build pixmap to hold my view
    pixmap = XCreatePixmap(application->display(), myWindow, 
        drawingAreaWidth, drawingAreaHeight, 8);
    
    XPutImage(application->display(), pixmap, application->gc(), 
        sprite->image(), 0, 0, 0, 0, sprite->width(), sprite->height());
  }

  // Install the pixmap on my drawing area
  XSetWindowBackgroundPixmap(application->display(), myWindow, pixmap);

  // Make sure the window refreshes
  XClearWindow(application->display(), myWindow);

  // Resize the window to fit the pixmap
  resize(drawingAreaWidth, drawingAreaHeight);

  // Free the pixmap
  XFreePixmap(application->display(), pixmap);
}

///////////////////////////////////////////////////////////////////////////////
// Called whenever an event arrives for me (I need to override the default)
///////////////////////////////////////////////////////////////////////////////
void MainDeck::handleEvent(XEvent* event)
{
  if ((event->type == ButtonPress) && (event->xbutton.button == Button1))
  {
    myCommand->execute(NULL);
  }
}

