#!/usr/bin/env python3

import sys

import wndrng.auth as auth
import wndrng.term as term
import wndrng.wandering as wndr

import wndrng.i18n as i18n

i18n.setup_i18n()
auth.setup_key()
wndr.init()

term.print_hint("Welcome to the game!")
term.print_hint("Please give the genre of the game")
genre = sys.stdin.readline().strip()
term.print_hint("Please give the background")
wndr.background(sys.stdin.readline(), omit=False)
term.print_hint("Please give the number of the key characters")
num = int(sys.stdin.readline())
assert num >= 2
assert num < len(term.colors) - 1

term.print_hint("Please give the name of the key characters in the game (separated by comma)")
names = sys.stdin.readline().strip().split(",")
if len(names) == 1:
    names = names[0].split("，")   # for Chinese

assert len(names) == num
for name in names:
    wndr.people.insert(0, name.strip())
colors = term.colors[1:num + 1] + term.colors[-1:]

term.print_hint("Please give each character's role in the game")
for person in wndr.people:
    if person != i18n.default("Narrator"):
        print()
        term.cprint("%s:" % person, "light_grey", attrs=["bold"])
        sys.stdout.flush()
        role = "".join(sys.stdin.readline().strip().split(": ")[1:])
        wndr.roles.insert(0, role)

wndr.background("And the %d people have a wonderful adventure, they are %s" % (num, ", ".join(names)))
for person in wndr.people:
    wndr.background("%s: %s" % (person, wndr.roles[wndr.people.index(person)]))

term.print_hint("Please give the name of the only human player")
wndr.human = sys.stdin.readline().strip()

if __name__ == "__main__":
    term.print_hint("Game started!")
    print()
    term.cprint(wndr.__doc__, "light_grey", attrs=["bold"])
    print()
    for item in wndr.timeline:
        term.tidy_print(item, colors[wndr.people.index(item.split(":")[0].strip()) % len(colors)])
        print()
    wndr.background("And the %s story begins..." % genre)
    print()

    wndr.game_loop()
