# cli_solver.py
import argparse
import os

from boggled import BoggleBoard, BoggleSolver, BoggleWords


def solve_board(board, words):
    solver = BoggleSolver(board, words)
    solver.solve()
    return solver


def display_board_details(board):
    print("Board details:")
    print("Columns: ", board.columns)
    print("Rows: ", board.rows)
    s = '\n'
    for pos in board.tiles:
        s += '  ' if len(board.tiles[pos]) == 2 else '   '
        s += board.tiles[pos]
        if (pos % board.columns) == 0:
            s += '\n'
    print(s)


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument("letters", type=str,
                        help="Board letters")
    parser.add_argument("dictionary", type=str,
                        help="The text file containing the dictionary word list.")
    parser.add_argument("-m", "--min", type=int,
                        help="The minimum word size.")
    parser.add_argument("-p", "--paths", action="store_true",
                        help="Include the path followed for each word found.")

    args = parser.parse_args()

    if os.path.isfile(args.dictionary):
        if isinstance(args.min, int):
            words = BoggleWords(args.min)
        else:
            words = BoggleWords()
        words.loadFromFile(args.dictionary)

        board = BoggleBoard(args.letters)
        display_board_details(board)
        solved_board = solve_board(board, words)

        print('Found:', len(solved_board.found))
        if args.paths:
            for word in solved_board.found:
                print('{} : {}'.format(word, solved_board.found[word]))
        else:
            print(solved_board.foundWords)
    else:
        print("Error: Unable to find the dictionary.")

[((148, 174), 'boggled.BoggleSolver', 'BoggleSolver', (['board', 'words'], {}), '(board, words)\n', (160, 174), False, 'from boggled import BoggleBoard, BoggleSolver, BoggleWords\n'), ((592, 617), 'argparse.ArgumentParser', 'argparse.ArgumentParser', ([], {}), '()\n', (615, 617), False, 'import argparse\n'), ((1125, 1156), 'os.path.isfile', 'os.path.isfile', (['args.dictionary'], {}), '(args.dictionary)\n', (1139, 1156), False, 'import os\n'), ((1347, 1372), 'boggled.BoggleBoard', 'BoggleBoard', (['args.letters'], {}), '(args.letters)\n', (1358, 1372), False, 'from boggled import BoggleBoard, BoggleSolver, BoggleWords\n'), ((1216, 1237), 'boggled.BoggleWords', 'BoggleWords', (['args.min'], {}), '(args.min)\n', (1227, 1237), False, 'from boggled import BoggleBoard, BoggleSolver, BoggleWords\n'), ((1272, 1285), 'boggled.BoggleWords', 'BoggleWords', ([], {}), '()\n', (1283, 1285), False, 'from boggled import BoggleBoard, BoggleSolver, BoggleWords\n')]