import arcade
from gui_aux import *
from desktop_Platform import *

def desktop_createDeselectedTiles(p):
  for (id, pos) in enumerate(p.c.tilePositions):
    tile = arcade.AnimatedTimeBasedSprite()
    p.deselectedTiles.append(tile)
    p.deselectedSprites.append(tile)

    tile.guid = id
    tile.texture = p.textures[0]
    # Animation between two textures.
    a1 = arcade.sprite.AnimationKeyframe(0, 700, p.textures[0])
    a2 = arcade.sprite.AnimationKeyframe(1, 700, p.textures[1])
    tile.frames.append(a1)
    tile.frames.append(a2)
    # Position.
    tile.left = pos[0]
    tile.top = pos[1]
  #}
#}

def desktop_createSelectedTiles(p):
  for (id, pos) in enumerate(p.c.tilePositions):
    tile = arcade.Sprite()
    p.selectedTiles.append(tile)
    p.selectedSprites.append(tile)

    tile.guid = id
    tile.texture = p.textures[2 + p.c.playfieldItems[id]]
    # Position.
    tile.left = pos[0]
    tile.top = pos[1]
    # Invisible by default.
    tile.visible = False
  #}
#}

def desktop_createTitle(p):
  p.title = arcade.Sprite()
  p.titleSprites.append(p.title)
  p.title.texture = p.titleTextures[0]
  # Position.
  pos = gui_aux_cellScreenPosition(p.c, p.c.titlePosition)
  p.title.left = pos[0]
  p.title.top = pos[1]
  # Invisible by default.
  p.title.visible = False
#}

# Deselect mismatched tiles
#
# Conditions:
# 1. Time to deselect mismatched items
def desktop_deselectMismatchedTiles(p):
  if (
    p.c.recentField == "deselectMismatchedTiles"
  ):
    for id in p.c.mismatchedItems:
      p.deselectedTiles[id].visible = True
      p.selectedTiles[id].visible = False
    #}
  #}
#}

# Hide deselected tile and show selected one
#
# Conditions:
# 1. tile has just been selected
def desktop_displaySelectedTile(p):
  if (
    p.c.recentField == "selectedId"
  ):
    id = p.c.selectedId
    p.deselectedTiles[id].visible = False
    p.selectedTiles[id].visible = True
  #}
#}

# Display title for the first selected tile
#
# Conditions:
# 1. tile has just been selected and it's the first one in pair
def desktop_displayTitle(p):
  if (
    p.c.recentField == "selectedId" and
    (
      len(p.c.selectedItems) == 0 or
      len(p.c.selectedItems) == 2
    )
  ):
    gid = p.c.playfieldItems[p.c.selectedId]
    p.title.texture = p.titleTextures[gid]
    p.title.visible = True
  #}
#}

# Hide matching tiles
#
# Conditions:
# 1. Time to hide matching items
def desktop_hideMatchingTiles(p):
  if (
    p.c.recentField == "hideMatchingTiles"
  ):
    for id in p.c.selectedItems:
      p.deselectedTiles[id].visible = False
      p.selectedTiles[id].visible = False
    #}
  #}
#}

# Load tile textures
def desktop_loadTextures(p):
  texs = []
  for (id, td) in enumerate(p.c.textureDescriptions):
    tex = arcade.load_texture(
      td.fileName,
      x = td.x,
      y = td.y,
      width = td.width,
      height = td.height
    )
    texs.append(tex)
  #}
  p.textures = texs
#}

# Load tiTLe textures
def desktop_loadTitleTextures(p):
  texs = []
  for (id, td) in enumerate(p.c.titleTextureDescriptions):
    tex = arcade.load_texture(
      td.fileName,
      x = td.x,
      y = td.y,
      width = td.width,
      height = td.height
    )
    texs.append(tex)
  #}
  p.titleTextures = texs
#}

# Postpone deselection of mismatched tiles for better UX
#
# Conditions:
# 1. a pair of tiles has been mismatched
def desktop_scheduleDeselectionOfMismatchedTiles(p):
  if (
    p.c.recentField == "mismatchedItems"
  ):
    p.sequentialTimer.schedule("deselectMismatchedTiles", True, p.c.deselectMismatchedTilesDelay)
  #}
#}

# Postpone hiding of matching tiles for better UX
#
# Conditions:
# 1. a pair of tiles has been matched
def desktop_scheduleHidingOfMatchingTiles(p):
  if (
    p.c.recentField == "hiddenItems"
  ):
    p.sequentialTimer.schedule("hideMatchingTiles", True, p.c.hideMatchingTilesDelay)
  #}
#}
