from production.box import OrderCostAdderBox
from production.treebuilder import BoxConfiguration
from sqliterepository import ProductRepository

from ._BoxBuilder import BoxBuilder


class OrderCostAdderBoxBuilder(BoxBuilder):
    def __init__(
        self,                   # type: OrderCostAdderBoxBuilder
        product_repository,     # type: ProductRepository
        loggers,
    ):
        super(OrderCostAdderBoxBuilder, self).__init__(loggers)
        self.product_repository = product_repository

    def build(
        self,       # type: OrderCostAdderBoxBuilder
        config,     # type:
    ):
        # type: (BoxConfiguration) -> OrderCostAdderBox

        production_weights = self.product_repository.get_products_with_custom_params(custom_params=["ProductionWeight"])
        per_item_cost = {}
        for part_code in list(production_weights.get("ProductionCost", {}).keys()):
            weight = int(production_weights["ProductionCost"][part_code])

            per_item_cost[part_code] = weight

        default_cost = int(self.get_extra(config, "DefaultCost", 3))
        return OrderCostAdderBox(
            name=config.name,
            sons=config.sons,
            per_item_cost=per_item_cost,
            default_cost=default_cost,
            logger=self.get_logger(config),
        )
