from vs_card import *

class tgame_type:
  easy = 0
  medium = 1
  hard = 2
  
class tgame_state:
  deck = None
  stacks = None
  selected_stack_index = None
  selected_index = None
  game_type = None
  deal_stack = None
  spades_stack = None
  clubs_stack = None
  diamonds_stack = None
  hearts_stack = None
  
  def __init__(self):
    self.deck = tstack()
    self.deal_stack = tstack()
    self.spades_stack = tstack()
    self.clubs_stack = tstack()
    self.diamonds_stack = tstack()
    self.hearts_stack = tstack()
    
    self.stacks = []
    self.selected_stack_index = None
    self.selected_index = None
    self.game_type = tgame_type.easy
    
  def copy_to(self, pgame_state):
    self.deck.copy_to(pgame_state.deck)
    self.deal_stack.copy_to(pgame_state.deal_stack)
    self.spades_stack.copy_to(pgame_state.spades_stack)
    self.clubs_stack.copy_to(pgame_state.clubs_stack)
    self.diamonds_stack.copy_to(pgame_state.diamonds_stack)
    self.hearts_stack.copy_to(pgame_state.hearts_stack)

    pgame_state.stacks = []
    for lstack in self.stacks:
      lnew_stack = tstack()
      lstack.copy_to(lnew_stack)
      pgame_state.stacks.append(lnew_stack)
      
    pgame_state.selected_stack_index = self.selected_stack_index
    pgame_state.selected_index = self.selected_index
    pgame_state.game_type = self.game_type
    
  def duplicate(self):
    lresult = tgame_state()
    self.copy_to(lresult)
    return lresult
    
  def save_to_file(self, pfile_name):
    lfile = file(pfile_name, 'w')
    lfile.write(self.get_as_string())
    lfile.close
    
  def load_from_file(self, pfile_name):
    lfile = file(pfile_name, 'r')
    lstring = lfile.read()
    self.set_as_string(lstring)
    lfile.close
    
  def get_as_string(self):
    lresult = self.deck.get_as_string() + '\n' \
        + self.deal_stack.get_as_string() + '\n' \
        + self.spades_stack.get_as_string() + '\n' \
        + self.clubs_stack.get_as_string() + '\n' \
        + self.diamonds_stack.get_as_string() + '\n' \
        + self.hearts_stack.get_as_string()
    
    for lstack in self.stacks:
      lresult = lresult + '\n' + lstack.get_as_string()
    lresult = lresult + '\n' + `self.selected_stack_index` \
         + '\n' + `self.selected_index` \
         + '\n' + `self.game_type`
    return lresult

  def set_as_string(self, pstring):
    llines = pstring.split('\n')
    self.deck.set_as_string(llines.pop(0))
    self.deal_stack.set_as_string(llines.pop(0))
    self.spades_stack.set_as_string(llines.pop(0))
    self.clubs_stack.set_as_string(llines.pop(0))
    self.diamonds_stack.set_as_string(llines.pop(0))
    self.hearts_stack.set_as_string(llines.pop(0))

    for lstack in self.stacks:
      lstack.set_as_string(llines.pop(0))

    self.selected_stack_index = int(llines.pop(0))
    self.selected_index = int(llines.pop(0))
    self.game_type = int(llines.pop(0))
    
  def calculate_positions(self):
    self.deck.calculate_positions()
    self.deal_stack.calculate_positions()
    self.spades_stack.calculate_positions()
    self.clubs_stack.calculate_positions()
    self.diamonds_stack.calculate_positions()
    self.hearts_stack.calculate_positions()

    for lstack in self.stacks:
      lstack.calculate_positions()
    

