#!/usr/bin/env python3

import sys, logging, os, json

version = (3,7)
assert sys.version_info >= version, "This script requires at least Python {0}.{1}".format(version[0],version[1])

logging.basicConfig(format='[%(filename)s:%(lineno)d] %(message)s', level=logging.INFO)
logger = logging.getLogger(__name__)




# Game loop functions
def render(game,current,moves):
    ''' Displays the current room, moves, and '''
    r = game['rooms']
    c = r[current]

    print('\n\nMoves: {moves}'.format(moves=moves))
    print('\n\nYou are in the {name}'.format(name=c['name']))
    print(c['desc'])
    if len(c['inventory']):
        print('You see the following items:')
        for i in c['inventory']:
            print('\t{i}'.format(i=i))

def getInput(game,current,verbs):
    ''' Asks the user for input and normalizes the inputted value. Returns a list of commands '''

    toReturn = input('\nWhat would you like to do? ').strip().upper().split()
    if (len(toReturn)):
        #assume the first word is the verb
        toReturn[0] = normalizeVerb(toReturn[0],verbs)
    return toReturn


def update(selection,game,current,inventory):
    ''' Process the input and update the state of the world '''
    s = list(selection)[0]  #We assume the verb is the first thing typed
    if s == "":
        print("\nSorry, I don't understand.")
        return current
    elif s == 'EXITS':
        printExits(game,current)
        return current
    else:
        for e in game['rooms'][current]['exits']:
            if s == e['verb'] and e['target'] != 'NoExit':
                if e['target'] in game['rooms']:
                    return e['target']
                else:
                    print('\n\n' + e['target'])

    print("\nYou can't go that way!")
    return current


# Helper functions

def printExits(game,current):
    e = ", ".join(str(x['verb']) for x in game['rooms'][current]['exits'])
    print('\nYou can go the following directions: {directions}'.format(directions = e))

def normalizeVerb(selection,verbs):
    for v in verbs:
        if selection == v['v']:
            return v['map']
    return ""

def end_game(winning,moves):
    if winning:
        print('You have won! Congratulations')
        print('You found Mothman in {moves} moves! Nicely done!'.format(moves=moves))
    else:
        print('Thanks for playing!')
        print("You didn't find Mothman in {moves} moves. See you next time!".format(moves=moves))





def main():
    game = {}
    with open('game.json') as json_file:
        game = json.load(json_file)

    current = 'BASEMENT'
    win = ['LAMPLOVER']
    lose = []
    moves = 0
    inventory = []

    while True:

        render(game,current,moves)

        selection = getInput(game,current,game['verbs'])

        if selection[0] == 'QUIT':
            end_game(False,moves)
            break

        current = update(selection,game,current,inventory)

        if current in win:
            end_game(True,moves)
            break
        if current in lose:
            end_game(False,moves)
            break

        moves += 1





if __name__ == '__main__':
	main()