/* indent:4  tabsize:8  font:fixed-width */

#import "Klondike.h"
#import "localstrings.h"
#import "KlondikePrefs.h"
#import "DrawPileDelegate.h"
#import "GamePileDelegate.h"
#import "SuitPileDelegate.h"
#import "DiscardPileDelegate.h"

@implementation Klondike


/*---------------------------------------------------------------------------
|
|    - startGame:
|
|    returns:    (id)  self
|
|----------------------------------------------------------------------------
|
|    Start a new game.
|			
\----------------------------------------------------------------------------*/

- startGame:sender
{
    [super startGame:sender];
    return [self setupGame:YES];
}


/*---------------------------------------------------------------------------
|
|    - restartGame:
|
|    returns:  (id) self
|
|----------------------------------------------------------------------------
|
|    Restart the game in progress (i.e. don't shuffle the cards).
|			
\----------------------------------------------------------------------------*/

- restartGame:sender
{
    [super restartGame:sender];
    return [self setupGame:NO];    
}


/*---------------------------------------------------------------------------
|
|    - setupGame:(BOOL)redeal
|
|    returns:  (id) self
|
|----------------------------------------------------------------------------
|
|    Setup a new game.  If "redeal" is true, deal a new deck, otherwise
|    use the same cards as the previous game.
|			
\----------------------------------------------------------------------------*/
- setupGame:(BOOL)redeal
{
    int pileIndex, cardIndex , i;
    id  suitCardPiles[4];
    id  gameCardPiles[7];
    id  drawCardPile;

    [gameWindow disableFlushWindow];

    [drawPileDelegate setDrawCardCount:[prefs cardsToDraw]];
    [discardPileDelegate setDrawCardCount:[prefs cardsToDraw]];
    
    //----------------------------------------------------------------
    //    Sync pile id's with current game window and set 
    //    preferences and delegates
    //----------------------------------------------------------------
    
    suitCardPiles[0] = suitPileView1;
    suitCardPiles[1] = suitPileView2;
    suitCardPiles[2] = suitPileView3;
    suitCardPiles[3] = suitPileView4;
    for (i = 0; i < 4; i++)
    {
        [suitCardPiles[i] setBackgroundColor:desktopColor];
        [suitCardPiles[i] setCardSize:cardSize];
        [suitCardPiles[i] setDelegate:suitPileDelegate];
    }
    
    gameCardPiles[0] = gamePileView1;
    gameCardPiles[1] = gamePileView2;
    gameCardPiles[2] = gamePileView3;
    gameCardPiles[3] = gamePileView4;
    gameCardPiles[4] = gamePileView5;
    gameCardPiles[5] = gamePileView6;
    gameCardPiles[6] = gamePileView7;
    for (i = 0; i < 7; i++)
    {
        [gameCardPiles[i] setBackgroundColor:desktopColor];
        [gameCardPiles[i] setCardSize:cardSize];
        [gameCardPiles[i] setDelegate:gamePileDelegate];
    }
    
    [gamePileDelegate setSuitCardPileViews:suitCardPiles];
    
    [drawPileView setBackgroundColor:desktopColor];
    [drawPileView setCardSize:cardSize];
    drawCardPile = [drawPileView cardPile];
    [drawPileDelegate setDiscardCardPileView:discardPileView];
    [drawPileView setDelegate:drawPileDelegate];
    
    [discardPileView setBackgroundColor:desktopColor];
    [discardPileView setCardSize:cardSize];
    [discardPileView setDelegate:discardPileDelegate];
    [discardPileDelegate setSuitCardPileViews:suitCardPiles];

    [gameWindow display];
    [gameWindow reenableFlushWindow];
    [gameWindow flushWindow];


    //-----------------------------------------------------------------------
    //    Empty the recycle pile
    //-----------------------------------------------------------------------
    
    [recycleCardPile freeCards];

    //-----------------------------------------------------------------------
    //    Initialize the drawPileView to have a shuffled
    //    deck
    //-----------------------------------------------------------------------
    [drawCardPile freeCards];
    if (redeal)
    {
        [[drawCardPile addDeck] shuffle];
	
	// make a copy of the CardPile for restart option
	if (!prevDeck)
	{
	    prevDeck = [[CardPile allocFromZone:[self zone]]
	                           initForCardSize:cardSize];
	}
	else
	{
	    [prevDeck freeCards];
	    [prevDeck setCardSize:cardSize];
	}
	[prevDeck addCopyOfPile:drawCardPile];
    }
    else
    {
        if (prevDeck)
	{
	    // copy the saved deck back to the game deck
	    [prevDeck setCardSize:cardSize];
	    [drawCardPile addCopyOfPile:prevDeck];
	}
	else
	{
	    // this shouldn't happen, but just in case...
            [[[drawCardPile freeCards] addDeck] shuffle];
	}
    }

    
    //----------------------------------------------------------------------
    //    Initialize the discardPileView as empty
    //----------------------------------------------------------------------

    [[discardPileView cardPile] freeCards];
    [discardPileView display];

    //-----------------------------------------------------------------------
    //    Initialize the four "suit piles" as empty
    //-----------------------------------------------------------------------

    for (pileIndex = 0; pileIndex < 4; pileIndex++)
    {
        [[suitCardPiles[pileIndex] cardPile] freeCards];
        [suitCardPiles[pileIndex] display];
    }

    //-----------------------------------------------------------------------
    //    Initialize and deal cards to the seven "user piles"
    //-----------------------------------------------------------------------

    for (pileIndex = 0; pileIndex < 7; pileIndex++)
    {
        id userPile = [gameCardPiles[pileIndex] cardPile];
        
        [userPile freeCards];
	[gameCardPiles[pileIndex] display];
    }

    for (pileIndex = 0; pileIndex < 7; pileIndex++)
    {
        id userPile = [gameCardPiles[pileIndex] cardPile];
        
        for (cardIndex = 0; cardIndex <= pileIndex; cardIndex++)
        {
            id tempCard = [drawCardPile cardAt:CS_TOP];
            
            [drawCardPile removeCard:tempCard];
            [userPile insertCard:tempCard at:CS_TOP];
	    [gameCardPiles[pileIndex] display];
        }
        [[userPile cardAt:CS_TOP] flip];
        [gameCardPiles[pileIndex] display];
    }

    //-----------------------------------------------------------------------
    //    Draw the remaining cards on the
    //    drawPileView
    //-----------------------------------------------------------------------

    //[drawPileView display];
    
    [gameWindow display];

    [gameWindow makeKeyAndOrderFront:self];
    return self;
}


/*---------------------------------------------------------------------------
|
|    - endGame:sender
|
|    returns:  (id) self
|
|----------------------------------------------------------------------------
|
|    End the game in progress.  Discard the game window.
|			
\----------------------------------------------------------------------------*/

- endGame:sender
{
    [super endGame:sender];

    // ****custom code here****
    
    return self;
}


/*---------------------------------------------------------------------------
|
|    - checkForWin
|
|    returns: (id) self
|
|----------------------------------------------------------------------------
|
|    Called to check the state of the game.
|
\----------------------------------------------------------------------------*/

- checkForWin
{
    if ([[suitPileView1 cardPile] cardCount] +
            [[suitPileView2 cardPile] cardCount] +
            [[suitPileView3 cardPile] cardCount] +
            [[suitPileView4 cardPile] cardCount] == 52)
    {
    	[self win];
    }
    return self;
}


@end

