# -*- coding: utf-8 -*-

import logging
from typing import Dict


class DeliveryDiscounts(object):
    def __init__(
        self,
        logger,     # type: logging
    ):
        # type: (...) -> None

        self.logger = logger

    def get_discount_amount_by_type(
        self,
        vouchers,           # type: Dict
        discount_type,      # type: str
    ):
        # type: (...) -> float

        if not vouchers:
            return 0

        discount_vouchers = vouchers.get(discount_type)
        if not discount_vouchers:
            return 0

        discount_amount = 0

        for voucher_discount_type in discount_vouchers:
            if voucher_discount_type == 'delivery_fee':
                continue

            try:
                discount_amount += float(discount_vouchers.get(voucher_discount_type))
            except Exception as _:
                self.logger.info("Unhandled discount {}".format(voucher_discount_type))

        return discount_amount

    def get_delivery_fee_discounts(
        self,
        vouchers,   # type: Dict
    ):
        # type: (...) -> float

        if not vouchers:
            return 0

        discount_amount = 0
        for _, value in vouchers.items():
            delivery_fee = value.get('delivery_fee')
            if delivery_fee:
                try:
                    discount_amount += float(value.get('delivery_fee', 0))
                except Exception as _:
                    self.logger.info("Unhandled discount delivery_fee")

        return discount_amount
