#!/usr/bin/env python3

import pyactr as actr


def memory_motor():
    playing_memory = actr.ACTRModel()

    actr.chunktype("playgame", "game, activity")

    initial_chunk = actr.makechunk(typename="playgame", game="memory")

    goal = playing_memory.set_goal("goal")

    playing_memory.productionstring(
        name="startplaying",
        string="""
    =goal>
    isa playgame
    game memory
    activity None
        ?manual>
        state free
    ==>
    =goal>
    isa playgame
    activity presskey
    """,
    )

    playing_memory.productionstring(
        name="presskey",
        string="""
    =goal>
    isa playgame
    game memory
    activity presskey
    ==>
    +manual>
    isa _manual
    cmd press_key
    key 1
    =goal>
        isa playgame
        activity None
    """,
    )

    simulation_game = playing_memory.simulation()
    simulation_game.run()


def memory_vision():
    environment = actr.Environment()
    playing_memory = actr.ACTRModel(environment=environment)

    memory = [{}, {"A": {"text": "A", "position": (100, 100)}}]

    goal = playing_memory.set_goal("goal")
    actr.chunktype("playgame", "game, activity, key, object")
    initial_chunk = actr.makechunk(typename="playgame", game="memory", key="1")
    goal.add(initial_chunk)

    playing_memory.productionstring(
        name="startplaying",
        string="""
    =goal>
    isa  playgame
    game memory
    activity None
    ==>
    =goal>
    isa playgame
    activity presskey""",
    )

    playing_memory.productionstring(
        name="presskey",
        string="""
    =goal>
    isa  playgame
    game memory
    activity presskey
    key =k
    ==>
    +manual>
    isa _manual
    cmd press_key
    key =k
    =goal>
    isa playgame
    activity attend""",
    )

    playing_memory.productionstring(
        name="attendobject",
        string="""
    =goal>
    isa  playgame
    game memory
    activity attend
    =visual_location>
    isa _visuallocation
    ?manual>
    state free
    ==>
    =goal>
    isa playgame
    activity storeobject
    +visual>
    isa _visual
    cmd     move_attention
    screen_pos =visual_location""",
    )

    playing_memory.productionstring(
        name="storeobject",
        string="""
    =goal>
    isa  playgame
    game memory
    activity storeobject
    =visual>
    isa _visual
    value =v
    ==>
    =goal>
    isa playgame
    activity None
    object =v""",
    )

    simulation = playing_memory.simulation(
        gui=False,
        environment_process=environment.environment_process,
        stimuli=memory,
        triggers="1",
    )
    simulation.run()

    print(goal)


def memory_memory():
    environment = actr.Environment()
    playing_memory = actr.ACTRModel(environment=environment)

    memory = [
        {},
        {"A": {"text": "A", "position": (100, 100)}},
        {"A": {"text": "A", "position": (100, 100)}},
    ]

    goal = playing_memory.set_goal("goal")
    actr.chunktype("playgame", "game, activity, key, object")
    initial_chunk = actr.makechunk(typename="playgame", game="memory", key="1")
    goal.add(initial_chunk)

    playing_memory.productionstring(
        name="startplaying",
        string="""
    =goal>
    isa  playgame
    game memory
    activity None
    object None
    ==>
    =goal>
    isa playgame
    activity presskey""",
    )

    playing_memory.productionstring(
        name="continueplaying",
        string="""
    =goal>
    isa  playgame
    game memory
    activity None
    object ~None
    ==>
    =goal>
    isa playgame
    game memory
    activity recall""",
    )

    playing_memory.productionstring(
        name="recallvalue",
        string="""
    =goal>
    isa  playgame
    game memory
    activity recall
    object =val
    ==>
    =goal>
    isa  playgame
    game memory
    activity checkrecalled
    +retrieval>
    isa playgame
    object =val""",
    )

    playing_memory.productionstring(
        name="recallsuccessful",
        string="""
    =goal>
    isa  playgame
    game memory
    activity checkrecalled
    object =val
    ?retrieval>
    buffer full
    =retrieval>
    isa playgame
    key =k
    ==>
    =goal>
    isa playgame
    key =k
    activity done""",
    )

    playing_memory.productionstring(
        name="recallfailed",
        string="""
    =goal>
    isa  playgame
    game memory
    activity checkrecalled
    key 1
    object =val
    ?retrieval>
    state error
    ==>
    ~goal>
    +goal>
    isa playgame
    game memory
    activity presskey
    key 2""",
    )

    playing_memory.productionstring(
        name="presskey",
        string="""
    =goal>
    isa  playgame
    game memory
    activity presskey
    key =k
    ==>
    +manual>
    isa _manual
    cmd press_key
    key =k
    =goal>
    isa playgame
    activity attend""",
    )

    playing_memory.productionstring(
        name="attendobject",
        string="""
    =goal>
    isa  playgame
    game memory
    activity attend
    =visual_location>
    isa _visuallocation
    ?manual>
    state free
    ==>
    =goal>
    isa playgame
    activity storeobject
    +visual>
    isa _visual
    cmd     move_attention
    screen_pos =visual_location""",
    )

    playing_memory.productionstring(
        name="storeobject",
        string="""
    =goal>
    isa  playgame
    game memory
    activity storeobject
    =visual>
    isa _visual
    value =v
    ==>
    =goal>
    isa playgame
    activity None
    object =v""",
    )

    simulation = playing_memory.simulation(
        gui=False,
        environment_process=environment.environment_process,
        stimuli=memory,
        triggers="1",
    )
    simulation.run(max_time=2)

    print(goal)
    print(playing_memory.decmem)


def memory_memory_loop():
    environment = actr.Environment()
    playing_memory = actr.ACTRModel(environment=environment)

    goal = playing_memory.set_goal("goal")
    actr.chunktype("playgame", "game, activity, key, object")
    initial_chunk = actr.makechunk(typename="playgame", game="memory", key="1")

    memory = [{}] + [{i: {"text": i, "position": (100, 100)}} for i in "BCDEFGHDIJ"]
    goal.add(initial_chunk)

    playing_memory.productionstring(
        name="startplaying",
        string="""
    =goal>
    isa  playgame
    game memory
    activity None
    object None
    ==>
    =goal>
    isa playgame
    activity presskey""",
    )

    playing_memory.productionstring(
        name="continueplaying",
        string="""
    =goal>
    isa  playgame
    game memory
    activity None
    object ~None
    ==>
    =goal>
    isa playgame
    game memory
    activity recall""",
    )

    playing_memory.productionstring(
        name="recallvalue",
        string="""
    =goal>
    isa  playgame
    game memory
    activity recall
    object =val
    ==>
    =goal>
    isa  playgame
    game memory
    activity checkrecalled
    +retrieval>
    isa playgame
    object =val""",
    )

    playing_memory.productionstring(
        name="recallsuccessful",
        string="""
    =goal>
    isa  playgame
    game memory
    activity checkrecalled
    object =val
    ?retrieval>
    buffer full
    =retrieval>
    isa playgame
    key =k
    ==>
    =goal>
    isa playgame
    key =k
    activity done""",
    )

    for i in range(1, 9):
        playing_memory.productionstring(
            name="recallfailed" + str(i),
            string="""
        =goal>
        isa  playgame
        game memory
        activity checkrecalled
        key """
            + str(i)
            + """
        object =val
        ?retrieval>
        state error
        ==>
        ~goal>
        +goal>
        isa playgame
        game memory
        activity presskey
        key """
            + str((i + 1) % 10),
        )

    playing_memory.productionstring(
        name="recallfailed9",
        string="""
    =goal>
    isa  playgame
    game memory
    activity checkrecalled
    key 0
    object =val
    ?retrieval>
    state error
    ==>
    ~goal>""",
    )

    playing_memory.productionstring(
        name="presskey",
        string="""
    =goal>
    isa  playgame
    game memory
    activity presskey
    key =k
    ==>
    +manual>
    isa _manual
    cmd press_key
    key =k
    =goal>
    isa playgame
    activity attend""",
    )

    playing_memory.productionstring(
        name="attendobject",
        string="""
    =goal>
    isa  playgame
    game memory
    activity attend
    =visual_location>
    isa _visuallocation
    ?manual>
    state free
    ==>
    =goal>
    isa playgame
    activity storeobject
    +visual>
    isa _visual
    cmd     move_attention
    screen_pos =visual_location""",
    )

    playing_memory.productionstring(
        name="storeobject",
        string="""
    =goal>
    isa  playgame
    game memory
    activity storeobject
    =visual>
    isa _visual
    value =v
    ==>
    =goal>
    isa playgame
    activity None
    object =v""",
    )
    playing_memory.model_parameters["subsymbolic"] = True

    goal.add(initial_chunk)
    # playing_memory.retrieval.pop()
    # for i in playing_memory.decmem.copy():
    #     playing_memory.decmem.pop(i)
    simulation = playing_memory.simulation(
        gui=False,
        environment_process=environment.environment_process,
        stimuli=memory,
        triggers=[str(i % 10) for i in range(1, 11)] + ["0"],
    )

    while True:
        simulation.step()
        if simulation.current_event.action == "RULE FIRED: recallsuccessful":
            break
        if simulation.show_time() > 10:
            print("Nothing found, breaking")
            break

    print(goal)
    print(simulation.show_time())
    print(playing_memory.decmem)


memory_memory_loop()