"""This file contains all calculations related
to spatial explicit calculations of technology/innovation
penetration."""
import logging
from collections import defaultdict
import numpy as np

def spatial_diffusion_values(
        regions,
        real_values,
        speed_con_max,
        low_congruence_crit,
        p_outlier
    ):
    """Generate spatial diffusion values from real data

    Arguments
    ---------
    regions : dict
        Regions
    p_outlier : float (percentage)
        Percentage of outliers which are capped at both
        ends of the value spectrum of the real data

    Returns
    -------
    diffusion_values : dict
        Spatial diffusion values based on speed assumptions

    Example
    -------
    This function calculates the values which already
    incorporate different speeds in diffusion.

    For example based on real values (e.g. population density)
    congruence values are calculated. Then, the congruence
    values are linked to diffusion speed differentes.
    """
    diffusion_values = {}

    # Diffusion speed assumptions
    speed_con_min = 1               # Speed at val_con == 0
    speed_con_max = speed_con_max   # Speed at con_val == 1

    if speed_con_max == 1:

        # No regional difference
        for region in regions:
            diffusion_values[region] = 1 #100% congruence
    else:
        # ----------------
        # plot real values to check for outliers
        # ----------------

        # Select number of outliers to remove lower and higher extremes
        nr_of_outliers = int(100 / len(regions) * p_outlier)

        sorted_vals = list(real_values.values())
        sorted_vals.sort()

        # Get value of largest outlier
        treshold_upper_real_value = sorted_vals[-nr_of_outliers]
        treshold_lower_real_value = sorted_vals[nr_of_outliers]

        for reg, val in real_values.items():
            if val > treshold_upper_real_value:
                real_values[reg] = treshold_upper_real_value
            if val < treshold_lower_real_value:
                real_values[reg] = treshold_lower_real_value

        # ---------------------------------
        # Congruence calculations
        # ----------------------------------
        # Max congruence value
        con_max = max(real_values.values())

        for region in regions:

            # Multiply speed of diffusion of concept with concept congruence value
            try:
                real_value = real_values[region]
            except KeyError:
                real_value = np.average(real_values.values())
                logging.warning("Set average real data for region %s", region)

            # Calculate congruence value
            congruence_value = real_value / con_max

            # If the assignement is thoe other way round (lowest value has highest congruence value)
            if low_congruence_crit:
                congruence_value = 1 - congruence_value
            else:
                pass

            # Calculate diffusion value
            lower_concept_val = (1 - congruence_value) * speed_con_min
            higher_concept_val = congruence_value * speed_con_max

            diffusion_values[region] = lower_concept_val + higher_concept_val

    return diffusion_values

def calc_diffusion_f(regions, f_reg, spatial_diff_values, fuels):
    """From spatial diffusion values calculate diffusion
    factor for every region (which needs to sum up to one
    across all regions) and end use. With help of these calculation diffusion
    factors, a spatial explicit diffusion of innovations can be
    implemented.

    Arguments
    ----------
    regions : dict
        Regions
    f_reg : dict
        Regional not weighted diffusion factors
    spatial_diff_values : dict
        Spatial diffusion index values
    fuels : array
        Fuels per enduse or fuel per sector and enduse

    Example
    -------
    If the national assumption of a technology diffusion
    of 50% is defined (e.g. 50% of service are heat pumps), this
    percentage can be changed per region, i.e. in some regions
    with higher diffusion factors, a larger percentage adopt
    the technology on the expense of other regions, where a
    lower percentage adopt this technology. In sum however,
    for all regions, the total service still sums up to 50%.

    Note
    -----
    The total sum can be higher than 1 in case of high values.
    Therefore the factors need to be capped. TODO MORE INFO
    """
    # Calculate fraction of energy demand of every region of total demand
    reg_enduse_p = defaultdict(dict)
    fuels_enduse = {}

    for fuel_submodel in fuels:

        # -----------------------------------
        # Sum fuel across sectors
        # -----------------------------------
        fuel_submodel_new = defaultdict(dict)
        for region, entries in fuel_submodel.items():
            enduses = entries.keys()
            try:
                for enduse in entries:
                    for sector in entries[enduse]:
                        fuel_submodel_new[region][enduse] = 0

                for enduse in entries:
                    for sector in entries[enduse]:
                        fuel_submodel_new[region][enduse] += np.sum(entries[enduse][sector])

                fuel_submodel = fuel_submodel_new
            except IndexError:
                enduses = entries.keys()
                break

        # --------------------
        # Calculate fraction of fuel for each region
        # --------------------
        for enduse in enduses:
            fuels_enduse[enduse] = 0

            # Total uk fuel of enduse
            tot_enduse_uk = 0
            for region in regions:
                tot_enduse_uk += np.sum(fuel_submodel[region][enduse])

            # Calculate regional % of enduse
            for region in regions:
                reg_enduse_p[enduse][region] = np.sum(fuel_submodel[region][enduse]) / tot_enduse_uk
                fuels_enduse[enduse] += np.sum(fuel_submodel[region][enduse])

        # ----------
        # Norm spatial factor (f_reg_norm) with population (does not sum upt to 1.p Eq. 7 Appendix)
        # ----------
        f_reg_norm = {}

        for enduse, regions_fuel_p in reg_enduse_p.items():

            # Sum across all regs (factor * fuel_p)
            sum_p_f_all_regs = 0
            for region in regions:
                sum_p_f_all_regs += f_reg[region] * regions_fuel_p[region]

            f_reg_norm[enduse] = {}
            for region, fuel_p in regions_fuel_p.items():
                f_reg_norm[enduse][region] = f_reg[region] / sum_p_f_all_regs

        # ----------
        # Norm which sums up to 1 (f_reg_norm_abs) (e.g. distriubte 200 units across space)
        # ----------
        f_reg_norm_abs = {}
        for enduse, regions_fuel_p in reg_enduse_p.items():
            f_reg_norm_abs[enduse] = {}
            for region, fuel_p in regions_fuel_p.items():
                f_reg_norm_abs[enduse][region] = fuel_p * spatial_diff_values[region]

    #-----------
    # Normalize f_reg_norm_abs
    #-----------
    for enduse in f_reg_norm_abs:
        sum_enduse = sum(f_reg_norm_abs[enduse].values())
        for region in f_reg_norm_abs[enduse]:
            f_reg_norm_abs[enduse][region] = f_reg_norm_abs[enduse][region] / sum_enduse

    # Testing
    for enduse in f_reg_norm_abs:
        np.testing.assert_almost_equal(
            sum(f_reg_norm_abs[enduse].values()),
            1,
            decimal=2)

    return f_reg_norm_abs, f_reg_norm

def calc_regional_services(
        enduse,
        uk_techs_service_p,
        regions,
        spatial_factors,
        fuel_disaggregated,
        techs_affected_spatial_f,
        capping_val=1
    ):
    """Calculate regional specific end year service shares
    of technologies (rs_reg_enduse_tech_p_ey)

    Arguments
    ---------
    uk_techs_service_p : dict
        Service shares per technology for future year
    regions : dict
        Regions
    spatial_factors : dict
        Spatial factor per enduse and region
    fuel_disaggregated : dict
        Fuels per region
    techs_affected_spatial_f : list
        List with technologies where spatial diffusion is affected
    capping_val : float
        Maximum service share (1.0). This is needed in case
        of spatial explicit diffusion modelling where the diffusion
        speed is very large and thus would lead to areas with
        largher shares than 1

    Returns
    -------
    rs_reg_enduse_tech_p_ey : dict
        Regional specific model end year service shares of techs

    Modelling steps
    -----
    A.) Calculation national end use service to reduce
        (e.g. 50% heat pumps for all regions) (uk_tech_service_ey_p)

    B.) Distribute this service according to spatial index for
        techs where the spatial explicit diffusion applies (techs_affected_spatial_f).
        Otherwise disaggregated according to fuel

    C.) Convert regional service reduction to ey % in region
    """
    reg_enduse_tech_p_ey = defaultdict(dict)

    # ------------------------------------
    # Calculate national total enduse fuel and service
    # ------------------------------------
    uk_enduse_fuel = 0
    for region in regions:
        reg_enduse_tech_p_ey[region] = {}
        uk_enduse_fuel += np.sum(fuel_disaggregated[region][enduse])

    # ----
    # Service of enduse for all regions
    # ----
    for region in regions:

        # Disaggregation factor
        f_fuel_disagg = np.sum(fuel_disaggregated[region][enduse]) / uk_enduse_fuel

        # Calculate fraction of regional service
        for tech, uk_tech_service_ey_p in uk_techs_service_p.items():

            global_tech_service_ey_p = uk_tech_service_ey_p

            # ---------------------------------------------
            # B.) Calculate regional service for technology
            # ---------------------------------------------
            if tech in techs_affected_spatial_f:
                # Use spatial factors
                reg_service_tech = global_tech_service_ey_p * spatial_factors[enduse][region]
            else:
                # If not specified, use fuel disaggregation for enduse factor
                reg_service_tech = global_tech_service_ey_p #* f_fuel_disagg

            reg_enduse_tech_p_ey[region][tech] = reg_service_tech

        # ---------------------------------------------
        # C.) Calculate regional fraction
        # ---------------------------------------------
        for tech, service_tech in reg_enduse_tech_p_ey[region].items():

            # ----------------------------------
            # Capping value in case larger than 1.0
            # ----------------------------------
            service_share = service_tech

            if service_share > capping_val: