from gui_aux import *
from gui_TextureDescription import *
from llm import *
from memory_Context import *

# Generate textures descriptions
#
# Conditions:
# 1. tileImage or tileImageCount or tileImageHeight or tileImageWidth has just changed
# 2. tileImage, tileImageCount, tileImageHeight, tileImageWidth are not empty
@llm_by_value
def gui_generateTextureDescriptions(
  c: memory_Context
) -> memory_Context:
  if (
    c.recentField == "tileImage" or
    c.recentField == "tileImageCount" or
    c.recentField == "tileImageHeight" or
    c.recentField == "tileImageWidth"
  ):
    tds: list[gui_TextureDescription] = []
    for id in range(0, c.tileImageCount):
      td = gui_createTextureDescription()
      td.fileName = c.tileImage
      td.height = c.tileImageHeight
      td.width = c.tileImageWidth
      td.x = id * c.tileImageWidth
      td.y = 0
      tds.append(td)
    #}
    c.textureDescriptions = tds
    c.recentField = "textureDescriptions"
    return c
  #}

  c.recentField = "none"
  return c
#}

# Generate tile positions
#
# Conditions:
# 1. cellSize, playField, windowHeight, or windowWidth has changed and none of them is zero
@llm_by_value
def gui_generateTilePositions(
  c: memory_Context
) -> memory_Context:
  if (
    (
      c.recentField != "cellSize" and
      c.recentField != "playfieldSize" and
      c.recentField != "windowHeight" and
      c.recentField != "windowWidth"
    ) or
    (
      c.cellSize == 0 or
      c.playfieldSize == 0 or
      c.windowHeight == 0 or
      c.windowWidth == 0
    )
  ):
    c.recentField = "none"
    return c
  #}

  positions = gui_aux_cellPositions(c.playfieldSize)

  ps = []
  for i in range(0, len(positions)):
    p = positions[i]
    x = p[0] * c.cellSize
    y = c.windowHeight - p[1] * c.cellSize
    ps.append([x, y])
  #}

  c.tilePositions = ps
  c.recentField = "tilePositions"
  return c
#}

