from cfgtools import Group
from typing import List
from util import (
    find_key_value,
    find_key_values,
)

from ._AutoOrderCommand import AutoOrderCommand
from ._Statistics import Statistics
from ._Threshold import Threshold
from ._ZoomLevel import ZoomLevel


class KdsView(object):
    def __init__(self, group):
        # type: (Group) -> None

        self.name = ''
        self.production_view = ''
        self.selected = False
        self.selected_order_id = -1
        self.display_mode = ''
        self.title = ''
        self.header_text_macro = ''
        self.body_text_macro = ''
        self.body_sale_line_macro = ''
        self.footer_text_macro = ''
        self.carousel = True
        self.on_double_click = ''
        self.on_sell_click = 'served'
        self.show_items = True
        self.zoom_levels = []
        self.selected_zoom = 0
        self.thresholds = []
        self.statistics = []
        self.auto_order_command = None
        self.colored_area = 'body'
        self.view_with_actions = True
        self.show_in_preparation_items = False
        self.display_type = ''
        self.custom_font_size = None
        self.custom_font_color = None
        self.sound_notification_for_new_orders = None

        self.parse_kds_view(group)

    def parse_kds_view(self, view_group):
        # type: (Group) -> ()
        self.name = view_group.name

        self.production_view = find_key_value(view_group, "ProductionView")
        self.header_text_macro = find_key_value(view_group, "CellHeader")
        self.body_text_macro = find_key_value(view_group, "CellBody")
        default_sale_line_body = "#{itemIcon} #{itemLevel} #{itemQuantity} #{itemModifier} #{itemPrefix} #{itemName}"
        self.body_sale_line_macro = find_key_value(view_group, "SaleLineBody", default_sale_line_body)
        self.footer_text_macro = find_key_value(view_group, "CellFooter")
        self.on_double_click = find_key_value(view_group, "CellDoubleClick")
        self.on_sell_click = find_key_value(view_group, "OnSellClick")

        self.carousel = find_key_value(view_group, "Carousel", "true").lower() == 'true'
        self.show_items = find_key_value(view_group, "ShowItems", "true").lower() == 'true'

        for group in view_group.groups:
            if group.name == 'ZoomLevels':
                self.zoom_levels = self._get_zoom_levels(group)

            if group.name == 'Statistics':
                self.statistics = self._set_statistics(group)

            if group.name == 'Customization':
                self._set_custom_parameters(customization_group=group)

        for value in find_key_values(view_group, 'Thresholds'):
            self.thresholds.append(Threshold(value))

        auto_order_command_config = find_key_value(view_group, "AutoOrderCommand")
        if auto_order_command_config:
            self.auto_order_command = AutoOrderCommand(auto_order_command_config)

        colored_area_config = find_key_value(view_group, "ColoredArea")
        if colored_area_config:
            self.colored_area = colored_area_config

        self.view_with_actions = find_key_value(view_group, "ViewWithActions", "true").lower() == 'true'

    @staticmethod
    def _get_zoom_levels(group):
        zoom_levels = []
        for zoom_level_group in group.groups:
            zoom_levels.append(ZoomLevel(zoom_level_group))

        return zoom_levels

    def _set_statistics(self, group):
        for statistic_group in group.groups:
            name = statistic_group.name
            variable = statistic_group.find_value("variable")
            statistics_format = statistic_group.find_value("format")
            goal = statistic_group.find_value("goal.value")
            comparison = statistic_group.find_value("goal.comparison")

            statistic = Statistics(name, variable, statistics_format, goal, comparison)
            self.statistics.append(statistic)

        return self.statistics

    def _set_custom_parameters(
        self,                       # type: KdsView
        customization_group,        # type: List[Group]
    ):
        self.custom_font_size = find_key_value(
            view_group=customization_group,
            key_name="CustomFontSize",
            default_value=None,
        )
        self.custom_font_color = find_key_value(
            view_group=customization_group,
            key_name="CustomFontColor",
            default_value=None,
        )

        sound_notification_for_new_orders = find_key_value(
            view_group=customization_group,
            key_name="soundNotificationForNewOrders",
            default_value=False,
        )
        self.sound_notification_for_new_orders = str(sound_notification_for_new_orders.lower() == 'true')
