from memory_Context import *
from llm import *

# Exit
#
# Conditions:
# 1. `e`, `exit`, `q`, or `quit` was entered
# 2. Victory has just been reported
@llm_by_value
def cli_exit(
  c: memory_Context
) -> memory_Context:
  if (
    c.recentField == "input" and
    (
      c.input == "e" or
      c.input == "exit" or
      c.input == "q" or
      c.input == "quit"
    )
  ):
    c.exit = True
    c.recentField = "exit"
    return c
  #}
  if (
    c.recentField == "outputVictory"
  ):
    c.exit = True
    c.recentField = "exit"
    return c
  #}
  c.recentField = "none"
  return c
#}

# Ask user to go on
#
# Conditions:
# 1. Items have just matched and there are still items left to select
@llm_by_value
def cli_goOn(
  c: memory_Context
) -> memory_Context:
  if (
    c.recentField == "outputMatchedItems" and
    len(c.hiddenItems) != len(c.playfieldItems)
  ):
    c.outputGoOn = "Go on:"
    c.recentField = "outputGoOn"
    return c
  #}
  c.recentField = "none"
  return c
#}

# Greet the user
#
# Conditions:
# 1. Just launched
@llm_by_value
def cli_greetUser(
  c: memory_Context
) -> memory_Context:
  if (
    c.recentField == "didLaunch" and
    c.didLaunch == True
  ):
    c.outputGreeting = "OGS Memory Command Line Interface"
    c.recentField = "outputGreeting"
    return c
  #}
  c.recentField = "none"
  return c
#}

# Ask user to select second item to have a pair of selected items
#
# Conditions:
# 1. Single item has just been selected
@llm_by_value
def cli_promptSecondItemSelection(
  c: memory_Context
) -> memory_Context:
  if (
    c.recentField == "selectedItems" and
    len(c.selectedItems) == 1
  ):
    c.outputPromptSelection = "Select the second item now:"
    c.recentField = "outputPromptSelection"
    return c
  #}
  c.recentField = "none"
  return c
#}

# Report matched items
#
# Conditions:
# 1. Items were hidden (i.e., they matched)
@llm_by_value
def cli_reportMatchedItems(
  c: memory_Context
) -> memory_Context:
  if (
    c.recentField == "hiddenItems"
  ):
    c.outputMatchedItems = "Items matched!"
    c.recentField = "outputMatchedItems"
    return c
  #}
  c.recentField = "none"
  return c
#}

# Report victory
@llm_by_value
def cli_reportVictory(
  c: memory_Context
) -> memory_Context:
  if (
    c.recentField == "victory"
  ):
    c.outputVictory = "VICTORY! The game is over now"
    c.recentField = "outputVictory"
    return c
  #}
  c.recentField = "none"
  return c
#}

# Select item
#
# Conditions:
# 1. Id is digit
@llm_by_value
def cli_selectItem(
  c: memory_Context
) -> memory_Context:
  if (
    c.recentField == "input" and
    llm_isDigit(c.input)
  ):
    # CLI ids start with 1 while memory module has ids starting with 0
    # Convert CLI id to memory id
    c.selectedId = llm_strToInt(c.input) - 1
    c.recentField = "selectedId"
    return c
  #}
  c.recentField = "none"
  return c
#}

# Show help (aka commands)
#
# Conditions:
# 1. Just launched
# 1. `h` or `help` was entered
@llm_by_value
def cli_showHelp(
  c: memory_Context
) -> memory_Context:
  if (
    (
      c.recentField == "didLaunch" and
      c.didLaunch == True
    ) or
    (
      c.recentField == "input" and
      c.input == "h"
    ) or
    (
      c.recentField == "input" and
      c.input == "help"
    )
  ):
    c.outputHelp = "Commands:\n\te, exit, q, quit\n\t\tExit\n\th, help\n\t\tList commands\n\t1, 2, 3, ...\n\t\tSelect item\nEnter your choice:"
    c.recentField = "outputHelp"
    return c
  #}
  c.recentField = "none"
  return c
#}

# Report mismatched items
@llm_by_value
def cli_reportMismatchedItems(
  c: memory_Context
) -> memory_Context:
  if (
    c.recentField == "mismatchedItems"
  ):
    c.outputMismatchedItems = "Wrong! Try again:"
    c.recentField = "outputMismatchedItems"
    return c
  #}
  c.recentField = "none"
  return c
#}

## Report selection of invalid item ids
##
## Conditions:
## 1. Index out of bounds: less than minimum
## 2. Index out of bounds: greater than maximum
## 3. Item is already hidden
##@llm_by_value
##def cli_shouldReportInvalidItemSelection(
##  c: cli_Context
##) -> cli_Context:
##  if (
##    c.recentField == "cMemory" and
##    c.cMemory.recentField == "selectedItems" and
##    len(c.cMemory.selectedItems) == 1
##  ):
##    c.outputPromptSelection = "Select the second item now:"
##    c.recentField = "outputPromptSelection"
##    return c
##  #}
##  c.recentField = "none"
##  return c
###}
