# -*- coding: utf-8 -*-
from typing import List, Dict

from msgbus import MBEasyContext
from old_helper import BaseRepository
from persistence import Connection


class ProductRepository(BaseRepository):

    def __init__(self, mb_context):
        # type: (MBEasyContext) -> None

        super(ProductRepository, self).__init__(mb_context)

    def get_items_names(self, items):
        # type: (List[int]) -> Dict

        def inner_func(conn):
            # type: (Connection) -> Dict

            query = self._ProductNamesQuery
            product_codes = ','.join(map(str, items))
            query = query.format(product_codes)
            product_names = [(int(x.get_entry(0)), x.get_entry(1)) for x in conn.select(query)]
            return dict(product_names)

        return self.execute_with_connection(inner_func)

    _ProductNamesQuery = """\
        SELECT *
        FROM Product
        WHERE ProductCode IN ({0})
"""
