# -*- 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)
        self.all_part_code_jit_lines = None

    def get_all_part_code_jit_lines(self):
        # type: () -> Dict

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

            cursor = conn.select(self._PartCodesOfJitLinesQuery)
            products_jit_lines = {}
            for row in cursor:
                products_jit_lines[int(row.get_entry("ProductCode"))] = row.get_entry("JITLines")

            return products_jit_lines

        if self.all_part_code_jit_lines is None:
            self.all_part_code_jit_lines = self.execute_with_connection(func)

        return self.all_part_code_jit_lines

    _PartCodesOfJitLinesQuery = """
        SELECT ProductCode, JITLines
        FROM Production 
        WHERE (Expiration IS NULL OR Expiration > DATETIME('now')) AND JITLInes IS NOT NULL
    """
