from backend.Property import Property
from typing import List
from recommendations.Costs import Costs


class LightingRecommendations:

    def __init__(self, property_instance: Property, materials: List):
        """
        :param property_instance: Instance of the Property class, for the home associated to property_id
        :param materials: List of materials to be used in the recommendations
        """

        self.property = property_instance
        self.costs = Costs(self.property)

        material = [
            material for material in materials if material["type"] == "low_energy_lighting_installation"
        ]
        if len(material) != 1:
            raise ValueError("Incorrect number of low energy lighting materials specified")

        self.material = material[0]
        self.recommendation = []

    def recommend(self):
        """
        This method will check if there are any lighting fittings that aren't low energy.

        If there are, the will recommend fitting the rest of the outlets with low energy lighting fittings
        :return:
        """

        if self.property.lighting["low_energy_proportion"] == 100:
            return

        number_lighting_outlets = self.property.number_lighting_outlets

        # Number non lel outlets
        number_non_lel_outlets = number_lighting_outlets - (
            self.property.lighting["low_energy_proportion"] * number_lighting_outlets
        )

        number_non_lel_outlets = round(number_non_lel_outlets)

        if number_non_lel_outlets == 0:
            return

        # Get the cost of the fittings
        cost_result = self.costs.low_energy_lighting(
            number_of_lights=number_non_lel_outlets,
            number_current_lel_lights=number_lighting_outlets - number_non_lel_outlets,
            material=self.material
        )

        if number_non_lel_outlets == 1:
            description = "Install low energy lighting in 1 remaining outlet"
        else:
            description = "Install low energy lighting in %s outlets" % str(number_non_lel_outlets)

        self.recommendation = [
            {
                "parts": [],
                "type": "low_energy_lighting",
                "description": description,
                "starting_u_value": None,
                "new_u_value": None,
                # For SAP points, we use the fact that lighting is usually worth 2 points and we scale this to
                # the proportion of lights that will be set to low energy
                "sap_points": round(2 * (number_non_lel_outlets / number_lighting_outlets), 2),
                **cost_result
            }
        ]
