import cfgtools

from util import find_key_value, find_key_values

from ._BumpBarCommand import BumpBarCommand


class BumpBar(object):
    def __init__(
        self,
        group,      # type: cfgtools.Group
    ):
        # type: (...) -> BumpBar

        self.name = ''
        self.simulate_bump_bar = True
        self.commands = []

        self.parse_bump_bar(group)
        self.add_default_bump_bars()

    def parse_bump_bar(
        self,
        bump_bar_group,     # type: cfgtools.Group
    ):
        # type: (...) -> None

        self.name = bump_bar_group.name
        self.simulate_bump_bar = find_key_value(bump_bar_group, "SimulateBumpBar").lower() == 'true'
        command_group = next((x for x in bump_bar_group.groups if x.name == "Commands"), [])

        for command in command_group.keys:
            self.commands.append(BumpBarCommand(command.name, command.values[0].split("|")))

    def add_default_bump_bars(
        self
    ):
        # type: (...) -> None

        default_command_list = [u"refreshscreen", u"consolidateditems"]
        for command_name in default_command_list:
            if not any(x for x in self.commands if x.name.lower() == command_name):
                self.commands.append(BumpBarCommand(command_name, [command_name]))


