from actions.models import Navigation, NavigationProduct
from old_helper import BaseRepository
from persistence import Connection
from typing import List


class NavigationRepository(BaseRepository):
    def __init__(self, mb_context):
        super(NavigationRepository, self).__init__(mb_context)

    def get_navigation_list(self):
        # type: () -> List[Navigation]

        def inner_func(conn):
            # type: (Connection) -> List[Navigation]

            query = """
                        SELECT 
                           NV.NavId, 
                           Name, 
                           ParentNavId, 
                           SortOrder, 
                           ButtonText, 
                           COALESCE(ButtonColor, '#CCCCCC') AS ButtonColor,
                           NC.CustomParamValue AS RestrictedServicePeriod
                       FROM Navigation NV
                       LEFT JOIN NavigationCustomParams NC ON NC.NavId = NV.NavId 
                        AND CustomParamId = 'RESTRICTED_TO_SERVICE_PERIOD'
                       ORDER BY SortOrder
                   """

            res = list()
            for row in conn.select(query):
                nav = Navigation(nav_id=row.get_entry("NavId"),
                                 name=row.get_entry("Name"),
                                 parent_nav_id=row.get_entry("ParentNavId"),
                                 sort_order=row.get_entry("SortOrder"),
                                 button_text=row.get_entry("ButtonText"),
                                 background_color=row.get_entry("ButtonColor"),
                                 restricted_period=row.get_entry("RestrictedServicePeriod"))
                res.append(nav)

            return res

        return self.execute_with_connection(inner_func)

    def get_navigation_products(self):
        # type: () -> List[NavigationProduct]
        def inner_func(conn):
            # type: (Connection) -> List[NavigationProduct]

            query = """
                       SELECT PN.NavId, 
                              PN.ClassCode, 
                              PN.ProductCode, 
                              P.ProductName, 
                              COALESCE(N.ButtonColor, '#CCCCCC') AS ButtonColor
                       FROM ProductNavigation PN
                       INNER JOIN Product P ON PN.ProductCode = P.ProductCode
                       LEFT JOIN Navigation N ON PN.NavId = N.NavId
                       JOIN ProductKernelParams PKP ON P.ProductCode = PKP.ProductCode
                       ORDER BY PKP.ProductPriority
                    """

            res = list()
            for row in conn.select(query):
                nav = NavigationProduct(row.get_entry("NavId"),
                                        row.get_entry("ClassCode"),
                                        row.get_entry("ProductCode"),
                                        row.get_entry("ProductName"),
                                        row.get_entry("ButtonColor"))
                res.append(nav)

            return res

        return self.execute_with_connection(inner_func)

    def get_navigations_restricted_to_service_period(self, service_period_id):
        # type: () -> List[str]

        def inner_func(conn):
            # type: (Connection) -> List[str]

            query = """
                        SELECT NavId,
                               CustomParamValue
                          FROM NavigationCustomParams
                         WHERE CustomParamId = 'RESTRICTED_TO_SERVICE_PERIOD'
                   """

            res = []
            for row in conn.select(query):
                period_ids = row.get_entry("CustomParamValue").split(",")
                if str(service_period_id) in period_ids:
                    res.append(str(row.get_entry("NavId")))

            return res

        return self.execute_with_connection(inner_func)
