import psp2d
import vs_screen
from vs_card import *
from vs_buttons import *
from solitaire import *
import math

class tgame_type_update:
  card = None
  caption = ''
  x = 0
  y = 0
  
  def __init__(self):
    self.card = tcard()
    self.card.face_up = True
    
class tselect(vs_screen.tscreen):
  allow_continue = False
  completed = False
  game_type = tgame_type.medium
  restart = False
  font = None
  card_selection = None
  game_types = []
  draw_button = None
  title = []
  title_pos = 0.0
  finish_pos = 0.0
  show_quit = False
  quit_image = None

  def get_super(self):
    return vs_screen.tscreen

  def __init__(self, pgame_type, pallow_continue, pcompleted):
    self.super.__init__(self)
    self.allow_continue = pallow_continue
    self.completed = pcompleted
    self.game_type = pgame_type
    self.background_image = psp2d.Image('images/background.png')
    self.card_selection = tcard_selection()
    self.font = psp2d.Font('images/font_arial_10_white.png')
    self.draw_button = tdraw_button(self.screen, self.font)
    self.card_selection = tcard_selection()
    self.card_selection.frame_delay = 5
    self.quit_image = psp2d.Image('images/quit.png')
    self.init_game_types()
    self.init_klondike()
    
  def init_game_types(self):
    lx = 90
    ly = 150
    lby = 120

    # Easy
    lgame_type = tgame_type_update()
    lgame_type.card.x = lx
    lgame_type.card.y = ly
    lgame_type.card.suit = tsuits.clubs
    lgame_type.card.value = tcards.jack
    lgame_type.caption = 'Easy'
    self.game_types.append(lgame_type)
    
    lx = lx + lby
    
    # Medium
    lgame_type = tgame_type_update()
    lgame_type.card.x = lx
    lgame_type.card.y = ly
    lgame_type.card.suit = tsuits.diamonds
    lgame_type.card.value = tcards.queen
    lgame_type.caption = 'Medium'
    self.game_types.append(lgame_type)
    
    lx = lx + lby
    
    # Hard
    lgame_type = tgame_type_update()
    lgame_type.card.x = lx
    lgame_type.card.y = ly
    lgame_type.card.suit = tsuits.hearts
    lgame_type.card.value = tcards.king
    lgame_type.caption = 'Hard'
    self.game_types.append(lgame_type)
    
  def init_klondike(self):
    self.title = [psp2d.Image('images/title_01.png') # s
        , psp2d.Image('images/title_02.png')         # o
        , psp2d.Image('images/title_03.png')         # l
        , psp2d.Image('images/title_04.png')         # i
        , psp2d.Image('images/title_05.png')         # t
        , psp2d.Image('images/title_06.png')         # a
        , psp2d.Image('images/title_04.png')         # i
        , psp2d.Image('images/title_08.png')         # r
        , psp2d.Image('images/title_09.png')]        # e
    
  def button_down(self, pbuttons):
    if self.show_quit:
      if buttons.cross in pbuttons:
        self.quit = True
        self.close();
      elif buttons.circle in pbuttons:
        self.show_quit = False
    else:
      if buttons.cross in pbuttons:
        self.do_select()
      elif buttons.left in pbuttons:
        if self.game_type > tgame_type.easy:
          self.game_type = self.game_type - 1
      elif buttons.right in pbuttons:
        if self.game_type < tgame_type.hard:
          self.game_type = self.game_type + 1
      elif buttons.circle in pbuttons:
        if self.allow_continue:
          self.do_continue()
      elif buttons.select in pbuttons:
        self.show_quit = True

  def update_klondike(self):
    lx = 60
    ly = 60
    lindex = 0
    for limage in self.title:
      ld = lx - self.title_pos
      lda = abs(ld);
      lc = 0
      if lda < 70:
        if lda > 35:
          lc = (70 - lda)
        else:
          lc = lda
      lc = math.sqrt(lc)
      if ld < 0:
        lxa = lx + lc
      else:
        lxa = lx - lc
        
      self.screen.blit(limage, 0, 0, -1, -1, lxa, ly, True)
      lx = lx + limage.width + 10
      lindex = lindex + 1
      
    self.title_pos = self.title_pos + 2
    if self.title_pos > 480.0:
      self.title_pos = 0.0
        
  def update_game_options(self):
    for lgame_type_index in range(tgame_type.easy, tgame_type.hard + 1):
      lgame_type = self.game_types[lgame_type_index]
      lgame_type.card.update(self.screen)
      if lgame_type_index == self.game_type:
        self.card_selection.update(self.screen, lgame_type.card.x - 7, lgame_type.card.y - 8)
      self.font.drawText(self.screen, lgame_type.card.x + 45, lgame_type.card.y + 15, lgame_type.caption)
      
  def update_buttons(self):
    lx = 425
    ly = 250
    self.draw_button.draw_button(lx, ly, buttons.select, 'Quit')
    lx = lx - 60
    self.draw_button.draw_button(lx, ly, buttons.cross, 'New')
    if self.allow_continue:
      lx = lx - 80
      self.draw_button.draw_button(lx, ly, buttons.circle, 'Continue')
      
  def update_completed(self):
    lx = 90
    ly = 225
    lindex = 0.0
    for lchar in ['C', 'o', 'n', 'g', 'r', 'a', 't', 'u', 'l', 'a', 't', 'i', 'o', 'n', 's', '.'
        , ' ', 'Y', 'o', 'u', ' ', 'W', 'o', 'n', '!']:
      lya = ly + (5 * math.sin(self.finish_pos + (0.5 * lindex)))
      self.font.drawText(self.screen, lx, lya, lchar)
      if lchar == 'W':
        lx = lx + 18
      elif lchar == 'l' or lchar == 'i':
        lx = lx + 8
      else:
        lx = lx + 12
      lindex = lindex + 0.25

    self.finish_pos = self.finish_pos + 0.1


  def update_quit(self):
    if self.show_quit:
      lx = (480 - self.quit_image.width) / 2
      ly = (272 - self.quit_image.height) / 2
      self.screen.blit(self.quit_image, 0, 0, -1, -1, lx, ly, True)

  def update(self):
    self.screen.blit(self.background_image)
    self.update_klondike()
    self.update_game_options()
    if self.completed:
      self.update_completed()
    self.update_buttons()
    self.update_quit()
    self.super.update(self)
    
  def do_select(self):
    self.restart = True
    self.close()
    
  def do_continue(self):
    self.restart = False
    self.close()
    
  def show(self):
    self.super.show(self)
    
