#!python
# coding: utf-8
import sys
import re
import random

try:
    # Use the system PRNG if possible
    random = random.SystemRandom()
except NotImplementedError:
    import warnings
    warnings.warn("A secure pseudo-random number generator is not available "
                  "on your system. Falling back to Mersenne Twister.")  # Mersenne twister is the default Python PRNG


# Takes in a string with dice and returns the rolled dice and the sum
def dice_roller(input_text):
    # Function Variables
    r_dice = re.compile('\d*d\d+')               # Regex for detecting dice notation
    rolls = []                                   # List for keeping track of the rolls

    # dice rolling code
    def roll_dice(d):
        result = 0
        dice = d.group(0)                        # Zeroth group of regex match is the match itself

        # Split the string at the letter d
        dice_split = dice.split("d")

        times = int(dice_split[0]) if dice_split[0] is not '' else 1  # reads the number of rolls, falls back to one
        sides = int(dice_split[1])

        # Actually rolling the dice
        for t in range(times):
            roll = random.randrange(1, sides+1)
            rolls.append(str(roll) + "/" + str(sides))
            result += roll

        return str(result)

    # Regex the input and automatically run the dice rolling code when dice notation is detected and evaluate the output
    output = eval(r_dice.sub(roll_dice, input_text))

    return output, rolls


# Prints helpful information (Its own function for easier expansion later)
def info():
    print('Syntax is: roll <dice_code> \nExample: 2d8 + 6 + d8')


# Gets called whenever an error occurs
def error():
    info()
    sys.exit(2)


# Main function
def main():
    # If no arguments are given
    if len(sys.argv) is 1:
        error()

    # Commandline input handling
    input_list = sys.argv[1:]                    # Command line input (is a list)
    input_string = ''.join(input_list)           # Push everything together in a string

    # Check if help flag i is passed
    if "-h" in input_string.lower():
        info()
        sys.exit(0)

    # Execute the function and handle errors
    run_dice_roller = []
    try:
        run_dice_roller = dice_roller(input_string)
    except (SyntaxError, NameError):
        error()
    except ZeroDivisionError:
        print("You can not divide by zero")
        sys.exit(2)

    # Print the results
    print("Total: " + str(run_dice_roller[0]))
    print("Rolls: " + str(run_dice_roller[1]))


if __name__ == "__main__":
    main()
