from iso8601 import iso8601
from typing import Dict

from application.model.benefit._RequiredItemOnSale import RequiredItemOnSale
from application.model.benefit._RequiredGroupOnSale import RequiredGroupOnSale
from application.model.benefit._ValidHours import ValidHours


class Conditions(object):
    def __init__(self, obj):
        # type: (Dict) -> None

        self.user_min_level = obj.get("userMinLevel")
        self.unique_for_sale = bool(obj.get("uniqueForSale"))
        self.ignore_burned_items = bool(obj.get("ignoreBurnedItems"))
        self.single_use = bool(obj.get("singleUse")) or self.ignore_burned_items
        self.burn_items = bool(obj.get("burnItems", True))
        self.pod_type = list(obj.get("podType")) if obj.get("podType") else None
        self.min_sale_amount = float(obj.get("minSaleAmount")) if obj.get("minSaleAmount") else None
        self.max_sale_amount = float(obj.get("maxSaleAmount")) if obj.get("maxSaleAmount") else None
        start_period = obj.get("validStartPeriod")
        self.valid_start_period = iso8601.parse_date(start_period) if start_period else None
        valid_end_period = obj.get("validEndPeriod")
        self.valid_end_period = iso8601.parse_date(valid_end_period) if valid_end_period else None
        valid_hours = obj.get("validHours")
        start_hour = valid_hours.get("startHour") if valid_hours else None
        end_hour = valid_hours.get("endHour") if valid_hours else None
        self.valid_hours = ValidHours(start_hour, end_hour)
        self.valid_days = list(obj.get("validDays")) if obj.get("validDays") else None
        self.required_items_on_sale = [RequiredItemOnSale(x) for x in obj.get("requiredItemsOnSale", [])]
        self.service_type = list(obj.get("serviceType")) if obj.get("serviceType") else None
        self.sale_type = list(obj.get("saleType")) if obj.get("saleType") else None
        self.sector = list(obj.get("sector")) if obj.get("sector") else None
        self.min_seats = obj.get("minSeats")
        self.discount_max_value = obj.get("discountMaxValue")
        self.service_period = list(obj.get("servicePeriods")) if obj.get("servicePeriods") else None
        self.brands = list(obj.get("brands")) if obj.get("brands") else None
        self.required_groups_on_sale = [RequiredGroupOnSale(x) for x in obj.get("requiredGroupsOnSale", [])]
