import sys

from prompt_toolkit import prompt
from prompt_toolkit.formatted_text import HTML
from atonatui.constants import README1, README2, README3, README4, README5


class InputMixin:

    def get_user_input(self):
        loop_count = 1

        request = self.request(help_text='Type "help" for more options, "q" to quit')
        self.check_valid_request(request)

        request = request.split(' ')

        if "loop" in request:
            loop_count = self.parse_loop_request(request)

        if "seq" in request:
            self.parse_seq_request(request, loop_count)

        if "debug" in request:
            breakpoint()

        if request[0] == "x" or request[0] == "regen":
            self.regenerate()
            self.loop()

        elif request[0] == "q" or request[0] == "quit":
            self.close()
            sys.exit()

        elif request[0] == "s" or request[0] == "settings":
            self.display_settings()

        elif request[0] == "h" or request[0] == "help":
            self.display_help()

        elif request[0][0] in ["P", "R", "I", "i"]:
            self.play_tone_row(request[0][0], request[0][1], loop=loop_count)

    def display_help(self):
        for readme in [README1, README2, README3, README4, README5]:
            self.clear_screen()
            print(readme)
            self.request("Press Enter to Continue")
        self.loop()

    def request(self, help_text):
        return prompt(
            "\n>>> ", placeholder=HTML(f'<style fg="#888888">{help_text}</style>')
        )

    def check_valid_request(self, request):
        if not request or request == "":
            self.loop()

    def parse_loop_request(self, request):
        loop_index = request.index("loop")
        loop_count = request[loop_index + 1]

        if not loop_count.isdigit():
            self.message("Not a valid loop count", self.loop)
        else:
            loop_count = int(loop_count)

        request.pop(loop_index + 1)
        request.pop(loop_index)
        return loop_count

    def parse_seq_request(self, request, loop_count):
        seq_index = request.index("seq")
        sequence = request[seq_index + 1].split(',')
        for seq in sequence:
            if seq[0] not in ["P", "R", "I", "i"] or int(seq[1:]) > 11:
                self.message(f"Invalid sequence at {seq}", self.loop)

        for loop in range(loop_count):
            for seq in sequence:
                self.play_tone_row(seq[0], seq[1:], loop=1)

    def play_tone_row(self, row_type, row_id, loop=1):
        reverse = True if row_type in ["R", "i"] else False

        if row_id in [str(x) for x in range(0, 12)]:

            if not reverse:
                if row_type == "I":
                    target_row = self.inv_map[row_type + row_id]
                else:
                    target_row = self.pr_map[row_type + row_id]
            else:
                row_type = "P" if row_type == "R" else "I"
                if row_type == "I":
                    target_row = self.inv_map[row_type + row_id][::-1]
                else:
                    target_row = self.pr_map[row_type + row_id][::-1]

            for i in range(loop):
                self.play_row(target_row)
