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

#import "Tenpile.h"
#import "localstrings.h"
#import "TenpileTempDelegate.h"
#import "TenpileSuitDelegate.h"
#import "TenpileGameDelegate.h"
#import "TenpilePrefs.h"

@implementation Tenpile


/*---------------------------------------------------------------------------
|
|    - startGame:
|
|    returns:    (id)  self
|
|----------------------------------------------------------------------------
|
|    Start a new game.  Get confirmation from the user before aborting a 
|    game in progress.
|			
\----------------------------------------------------------------------------*/

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


/*---------------------------------------------------------------------------
|
|    - restartGame:
|
|    returns:  (id) self
|
|----------------------------------------------------------------------------
|
|    Restart the game in progress.
|			
\----------------------------------------------------------------------------*/

- 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, i;
    int cycle;
    int hiddenCards = [prefs hiddenCards];
    id  suitCardPiles[4];
    id  gameCardPiles[10];
    id  tempCardPiles[4];

    [gameWindow disableFlushWindow];

    //----------------------------------------------------------------
    //    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;
    gameCardPiles[7] = gamePileView8;
    gameCardPiles[8] = gamePileView9;
    gameCardPiles[9] = gamePileView10;
    for (i = 0; i < 10; i++)
    {
        [gameCardPiles[i] setBackgroundColor:desktopColor];
        [gameCardPiles[i] setCardSize:cardSize];
        [gameCardPiles[i] setDelegate:gamePileDelegate];
    }
    
    tempCardPiles[0] = tempPileView1;
    tempCardPiles[1] = tempPileView2;
    tempCardPiles[2] = tempPileView3;
    tempCardPiles[3] = tempPileView4;
    for (i = 0; i < 4; i++)
    {
        [tempCardPiles[i] setBackgroundColor:desktopColor];
        [tempCardPiles[i] setCardSize:cardSize];
        [tempCardPiles[i] setDelegate:tempPileDelegate];
    }

    [gamePileDelegate setSuitCardPileViews:suitCardPiles];
    [gamePileDelegate setTempCardPileViews:tempCardPiles];
    [tempPileDelegate setSuitCardPileViews:suitCardPiles];

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

    //-----------------------------------------------------------------------
    //    Initialize the gamePile to have a 1 shuffled deck.
    //-----------------------------------------------------------------------

    // Initialize the initial cardPile.
    if (!drawCardPile)
    {
	drawCardPile = [[CardPile allocFromZone:[self zone]]
	                initForCardSize:cardSize];
    }
    else
        [drawCardPile setCardSize:cardSize];

    [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];
	    [drawCardPile addDeck];
	    [[drawCardPile addDeck] shuffle];
	}
    }
    
    //-----------------------------------------------------------------------
    //    Initialize the "suit piles" as empty
    //-----------------------------------------------------------------------

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

    //-----------------------------------------------------------------------
    //    Initialize the "temp piles" as empty
    //-----------------------------------------------------------------------

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

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

    for (pileIndex = 0; pileIndex < 10; pileIndex++)
    {
        [[gameCardPiles[pileIndex] cardPile] freeCards];
	[gameCardPiles[pileIndex] display];
    }
    
    for (cycle = 0; cycle < 5; cycle++)
    {
	for (pileIndex = 0; pileIndex < 10; pileIndex++)
	{
	    id userPile = [gameCardPiles[pileIndex] cardPile];
	    id tempCard = [drawCardPile cardAt:CS_TOP];
	    [drawCardPile removeCard:tempCard];
	    [userPile insertCard:tempCard at:CS_TOP];
	    if (cycle >= hiddenCards)
	      [[userPile cardAt:CS_TOP] flip];
	    [gameCardPiles[pileIndex] display];
	}
    }
    for (pileIndex = 0; pileIndex < 2; pileIndex++)
	{
	    id userPile = [tempCardPiles[pileIndex] cardPile];
	    id tempCard = [drawCardPile cardAt:CS_TOP];
	    [drawCardPile removeCard:tempCard];
	    [userPile insertCard:tempCard at:CS_TOP];
	    [[userPile cardAt:CS_TOP] flip];
	    [gameCardPiles[pileIndex] display];
	}

    [gameWindow display];
    [gameWindow makeKeyAndOrderFront:self];
    return self;
}


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

- endGame:sender
{
    // close the game window
    [super endGame:sender];

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

/*---------------------------------------------------------------------------
|
|    - win
|
|     returns: (id) self
|
|----------------------------------------------------------------------------
|
|    Called when the game has been won.  This is where you can insert fancy
|    winning routines, or just call the default (boring) routine.
|
\----------------------------------------------------------------------------*/

- win
{
    [super win];  // replace this with something wonderful
    return self;
}


/*---------------------------------------------------------------------------
|
|    - checkForWin
|
|    returns: (id) self
|
|----------------------------------------------------------------------------
|
|    Called to check the state of the game.  Always override (unless your
|    game is impossible to win).
|
\----------------------------------------------------------------------------*/

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

@end

