"""
This script contains standard tables which are defined in rdsap. The most recent version of sap/rdsap is
based on the 2012 version, however the government is currently working on releasing a new version, and there
we will need to re-visit this
"""

import pandas as pd

age_band_data = [
    {
        "age_band": "A",
        "England_Wales": "before 1900",
        "Scotland": "before 1919",
        "Northern_Ireland": "before 1919",
        "Park_home_UK": None,
    },
    {
        "age_band": "B",
        "England_Wales": "1900-1929",
        "Scotland": "1919-1929",
        "Northern_Ireland": "1919-1929",
        "Park_home_UK": None,
    },
    {
        "age_band": "C",
        "England_Wales": "1930-1949",
        "Scotland": "1930-1949",
        "Northern_Ireland": "1930-1949",
        "Park_home_UK": None,
    },
    {
        "age_band": "D",
        "England_Wales": "1950-1966",
        "Scotland": "1950-1964",
        "Northern_Ireland": "1950-1973",
        "Park_home_UK": None,
    },
    {
        "age_band": "E",
        "England_Wales": "1967-1975",
        "Scotland": "1965-1975",
        "Northern_Ireland": "1974-1977",
        "Park_home_UK": None,
    },
    {
        "age_band": "F",
        "England_Wales": "1976-1982",
        "Scotland": "1976-1983",
        "Northern_Ireland": "1978-1985",
        "Park_home_UK": "before 1983",
    },
    {
        "age_band": "G",
        "England_Wales": "1983-1990",
        "Scotland": "1984-1991",
        "Northern_Ireland": "1986-1991",
        "Park_home_UK": "1983-1995",
    },
    {
        "age_band": "H",
        "England_Wales": "1991-1995",
        "Scotland": "1992-1998",
        "Northern_Ireland": "1992-1999",
        "Park_home_UK": None,
    },
    {
        "age_band": "I",
        "England_Wales": "1996-2002",
        "Scotland": "1999-2002",
        "Northern_Ireland": "2000-2006",
        "Park_home_UK": "1996-2005",
    },
    {
        "age_band": "J",
        "England_Wales": "2003-2006",
        "Scotland": "2003-2007",
        "Northern_Ireland": None,
        "Park_home_UK": None,
    },
    {
        "age_band": "K",
        "England_Wales": "2007-2011",
        "Scotland": "2008-2011",
        "Northern_Ireland": "2007-2013",
        "Park_home_UK": "2006 onwards",
    },
    {
        "age_band": "L",
        "England_Wales": "2012 onwards",
        "Scotland": "2012 onwards",
        "Northern_Ireland": "2014 onwards",
        "Park_home_UK": None,
    },
    {
        "age_band": "L",
        "England_Wales": "2012-2021",
        "Scotland": "2012-2023",
        "Northern_Ireland": "2014-2022",
        "Park_home_UK": None,
    },
    {
        "age_band": "M",
        "England_Wales": "2022 onwards",
        "Scotland": "2024 onwards",
        "Northern_Ireland": "2023 onwards",
        "Park_home_UK": None,
    },
    {
        "age_band": "L",
        "England_Wales": "2012-2022",
        "Scotland": "2012 - 2023",
        "Northern_Ireland": "2014 -2022",
        "Park_home_UK": None,
    }
]

england_wales_age_band_lookup = {
    f"England and Wales: %s" % x["England_Wales"]: x["age_band"] for x in age_band_data
}

########################################################################################################################
# As defined in the rdsap documentation on page 9
# https://bregroup.com/wp-content/uploads/2019/09/RdSAP_2012_9.94-20-09-2019.pdf
########################################################################################################################
default_wall_thickness = [
    {
        "type": "stone",
        "A": 500,
        "B": 500,
        "C": 500,
        "D": 500,
        "E": 450,
        "F": 420,
        "G": 420,
        "H": 420,
        "I": 450,
        "J": 450,
        "K": 450,
        "L": 450,
        "M": 450,
    },
    {
        "type": "solid brick",
        "A": 220,
        "B": 220,
        "C": 220,
        "D": 220,
        "E": 240,
        "F": 250,
        "G": 270,
        "H": 270,
        "I": 300,
        "J": 300,
        "K": 300,
        "L": 300,
        "M": 300,
    },
    {
        "type": "cavity",
        "A": 250,
        "B": 250,
        "C": 250,
        "D": 250,
        "E": 250,
        "F": 260,
        "G": 270,
        "H": 270,
        "I": 300,
        "J": 300,
        "K": 300,
        "L": 300,
        "M": 300,
    },
    {
        "type": "timber frame",
        "A": 150,
        "B": 150,
        "C": 150,
        "D": 250,
        "E": 270,
        "F": 270,
        "G": 270,
        "H": 270,
        "I": 300,
        "J": 300,
        "K": 300,
        "L": 300,
        "M": 300,
    },
    {
        "type": "cob",
        "A": 540,
        "B": 540,
        "C": 540,
        "D": 540,
        "E": 540,
        "F": 540,
        "G": 560,
        "H": 560,
        "I": 590,
        "J": 590,
        "K": 590,
        "L": 590,
        "M": 590,
    },
    {
        "type": "system build",
        "A": 250,
        "B": 250,
        "C": 250,
        "D": 250,
        "E": 250,
        "F": 300,
        "G": 300,
        "H": 300,
        "I": 300,
        "J": 300,
        "K": 300,
        "L": 300,
        "M": 300,
    },
    {
        "type": "park home",
        "A": None,
        "B": None,
        "C": None,
        "D": None,
        "E": None,
        "F": 50,
        "G": 50,
        "H": None,
        "I": 75,
        "J": 100,
        "K": 100,
        "L": 100,
        "M": 100,
    },
]

########################################################################################################################
# This wall u-value table is defined in the rdsap documentation on page 19
# https://bregroup.com/wp-content/uploads/2019/09/RdSAP_2012_9.94-20-09-2019.pdf
########################################################################################################################
wall_types = [
    "Stone: granite or whinstone as built",
    "Stone: sandstone or limestone as built",
    "Solid brick as built",
    "Stone/solid brick with 50 mm external or internal insulation",
    "Stone/solid brick with 100 mm external or internal insulation",
    "Stone/solid brick with 150 mm external or internal insulation",
    "Stone/solid brick with 200 mm external or internal insulation",
    "Cob as built",
    "Cob with 50 mm external or internal insulation",
    "Cob with 100 mm external or internal insulation",
    "Cob with 150 mm external or internal insulation",
    "Cob with 200 mm external or internal insulation",
    "Cavity as built",
    "Unfilled cavity with 50 mm external or internal insulation",
    "Unfilled cavity with 100 mm external or internal insulation",
    "Unfilled cavity with 150 mm external or internal insulation",
    "Unfilled cavity with 200 mm external or internal insulation",
    "Filled cavity",
    "Filled cavity with 50 mm external or internal insulation",
    "Filled cavity with 100 mm external or internal insulation",
    "Filled cavity with 150 mm external or internal insulation",
    "Filled cavity with 200 mm external or internal insulation",
    "Timber frame as built",
    "Timber frame with internal insulation",
    "System build as built",
    "System build with 50 mm external or internal insulation",
    "System build with 100 mm external or internal insulation",
    "System build with 150 mm external or internal insulation",
    "System build with 200 mm external or internal insulation",
]

u_values = [
    [
        "a",
        "a",
        "a",
        "a",
        "1.7b",
        "1.0",
        "0.6",
        "0.60",
        "0.45",
        "0.35",
        "0.30",
        "0.28",
        "0.26",
    ],
    [
        "a",
        "a",
        "a",
        "a",
        "1.7b",
        "1.0",
        "0.6",
        "0.60",
        "0.45",
        "0.35",
        "0.30",
        "0.28",
        "0.26",
    ],
    [
        "1.7",
        "1.7",
        "1.7",
        "1.7",
        "1.7",
        "1.0",
        "0.60",
        "0.60",
        "0.45",
        "0.35",
        "0.30",
        "0.28",
        "0.26",
    ],
    [
        "0.55",
        "0.55",
        "0.55",
        "0.55",
        "0.55",
        "0.45",
        "0.35",
        "0.35",
        "0.30",
        "0.25",
        "0.21",
        "0.21",
        "0.20",
    ],
    [
        "0.32",
        "0.32",
        "0.32",
        "0.32",
        "0.32",
        "0.28",
        "0.24",
        "0.24",
        "0.21",
        "0.19",
        "0.17",
        "0.16",
        "0.15",
    ],
    [
        "0.23",
        "0.23",
        "0.23",
        "0.23",
        "0.23",
        "0.21",
        "0.18",
        "0.18",
        "0.17",
        "0.15",
        "0.14",
        "0.14",
        "0.13",
    ],
    [
        "0.18",
        "0.18",
        "0.18",
        "0.18",
        "0.18",
        "0.17",
        "0.15",
        "0.15",
        "0.14",
        "0.13",
        "0.12",
        "0.12",
        "0.11",
    ],
    [
        "0.80",
        "0.80",
        "0.80",
        "0.80",
        "0.80",
        "0.80",
        "0.60",
        "0.60",
        "0.45",
        "0.35",
        "0.30",
        "0.28",
        "0.26",
    ],
    [
        "0.40",
        "0.40",
        "0.40",
        "0.40",
        "0.40",
        "0.40",
        "0.35",
        "0.35",
        "0.30",
        "0.25",
        "0.21",
        "0.21",
        "0.20",
    ],
    [
        "0.26",
        "0.26",
        "0.26",
        "0.26",
        "0.26",
        "0.26",
        "0.24",
        "0.24",
        "0.21",
        "0.19",
        "0.17",
        "0.16",
        "0.15",
    ],
    [
        "0.20",
        "0.20",
        "0.20",
        "0.20",
        "0.20",
        "0.20",
        "0.18",
        "0.18",
        "0.17",
        "0.15",
        "0.14",
        "0.14",
        "0.13",
    ],
    [
        "0.16",
        "0.16",
        "0.16",
        "0.16",
        "0.16",
        "0.16",
        "0.15",
        "0.15",
        "0.14",
        "0.13",
        "0.12",
        "0.12",
        "0.11",
    ],
    [
        "1.5",
        "1.5",
        "1.5",
        "1.5",
        "1.5",
        "1.0",
        "0.60",
        "0.60",
        "0.45",
        "0.35",
        "0.30",
        "0.28",
        "0.26",
    ],
    [
        "0.53",
        "0.53",
        "0.53",
        "0.53",
        "0.53",
        "0.45",
        "0.35",
        "0.35",
        "0.30",
        "0.25",
        "0.21",
        "0.21",
        "0.20",
    ],
    [
        "0.32",
        "0.32",
        "0.32",
        "0.32",
        "0.32",
        "0.30",
        "0.24",
        "0.24",
        "0.21",
        "0.19",
        "0.17",
        "0.16",
        "0.15",
    ],
    [
        "0.23",
        "0.23",
        "0.23",
        "0.23",
        "0.23",
        "0.21",
        "0.18",
        "0.18",
        "0.17",
        "0.15",
        "0.14",
        "0.14",
        "0.13",
    ],
    [
        "0.18",
        "0.18",
        "0.18",
        "0.18",
        "0.18",
        "0.17",
        "0.15",
        "0.15",
        "0.14",
        "0.13",
        "0.12",
        "0.12",
        "0.11",
    ],
    [
        "0.7",
        "0.7",
        "0.7",
        "0.7",
        "0.7",
        "0.40",
        "0.35",
        "0.35",
        "0.45",
        "0.35",
        "0.30",
        "0.28",
        "0.26",
    ],
    [
        "0.37",
        "0.37",
        "0.37",
        "0.37",
        "0.37",
        "0.27",
        "0.25",
        "0.25",
        "0.25",
        "0.25",
        "0.21",
        "0.21",
        "0.20",
    ],
    [
        "0.25",
        "0.25",
        "0.25",
        "0.25",
        "0.25",
        "0.20",
        "0.19",
        "0.19",
        "0.19",
        "0.19",
        "0.17",
        "0.16",
        "0.15",
    ],
    [
        "0.19",
        "0.19",
        "0.19",
        "0.19",
        "0.19",
        "0.16",
        "0.15",
        "0.15",
        "0.15",
        "0.15",
        "0.14",
        "0.14",
        "0.13",
    ],
    [
        "0.16",
        "0.16",
        "0.16",
        "0.16",
        "0.16",
        "0.13",
        "0.13",
        "0.13",
        "0.13",
        "0.13",
        "0.12",
        "0.12",
        "0.11",
    ],
    [
        "2.5",
        "1.9",
        "1.9",
        "1.0",
        "0.80",
        "0.45",
        "0.40",
        "0.40",
        "0.40",
        "0.35",
        "0.30",
        "0.28",
        "0.26",
    ],
    [
        "0.60",
        "0.55",
        "0.55",
        "0.40",
        "0.40",
        "0.40",
        "0.40",
        "0.40",
        "0.40",
        "0.35",
        "0.30",
        "0.28",
        "0.26",
    ],
    [
        "2.0",
        "2.0",
        "2.0",
        "2.0",
        "1.7",
        "1.0",
        "0.60",
        "0.60",
        "0.45",
        "0.35",
        "0.30",
        "0.28",
        "0.26",
    ],
    [
        "0.60",
        "0.60",
        "0.60",
        "0.60",
        "0.55",
        "0.45",
        "0.35",
        "0.35",
        "0.30",
        "0.25",
        "0.21",
        "0.21",
        "0.20",
    ],
    [
        "0.35",
        "0.35",
        "0.35",
        "0.35",
        "0.35",
        "0.32",
        "0.24",
        "0.24",
        "0.21",
        "0.19",
        "0.17",
        "0.16",
        "0.15",
    ],
    [
        "0.25",
        "0.25",
        "0.25",
        "0.25",
        "0.25",
        "0.21",
        "0.18",
        "0.18",
        "0.17",
        "0.15",
        "0.14",
        "0.14",
        "0.13",
    ],
    [
        "0.18",
        "0.18",
        "0.18",
        "0.18",
        "0.18",
        "0.17",
        "0.15",
        "0.15",
        "0.14",
        "0.13",
        "0.12",
        "0.12",
        "0.11",
    ],
]

age_bands = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M"]

wall_uvalues = []
for i, wall_type in enumerate(wall_types):
    row = {"Wall_type": wall_type}
    for j, age_band in enumerate(age_bands):
        row[age_band] = u_values[i][j]
    wall_uvalues.append(row)

parkhome_wall_uvalues = [
    {
        "Wall_type": "Park home as built",
        "F": "1.7",
        "G": "1.2",
        "I": "0.7",
        "K": "0.6",
        "L": "0.6",
        "M": "0.6",
    },
    {
        "Wall_type": "Park home with additional insulation",
        "F": "s1.1.2",
        "G": "s1.1.2",
        "I": "s1.1.2",
        "K": "s1.1.2",
        "L": "s1.1.2",
        "M": "s1.1.2",
    },
]

wall_uvalues.extend(parkhome_wall_uvalues)

wall_uvalues_df = pd.DataFrame(wall_uvalues)

# This maps the descriptions in the EPC data to the descriptions in the RdSAP table
epc_wall_description_map = {
    ############################
    # Cavity wall mappings
    ############################
    "Cavity wall, as built, partial insulation": "Filled cavity",
    "Cavity wall, filled cavity": "Filled cavity",
    "Cavity wall, as built, no insulation": "Cavity as built",
    "Cavity wall, as built, insulated": "Filled cavity",
    "Cavity wall, with external insulation": "Unfilled cavity with 100 mm external or internal insulation",
    "Cavity wall, insulated": "Filled cavity",
    "Cavity wall, partial insulation": "Filled cavity",
    "Cavity wall,": "Cavity as built",  # General case of cavity wall without further details
    "Cavity wall, filled cavity and external insulation": "Filled cavity with 100 mm external or internal insulation",
    "Cavity wall, filled cavity and internal insulation": "Filled cavity with 100 mm external or internal insulation",
    "Cavity wall, with internal insulation": "Unfilled cavity with 100 mm external or internal insulation",
    "Cavity wall, no insulation": "Cavity as built",
    ############################
    # Solid brick wall mappings
    ############################
    "Solid brick, as built, no insulation": "Solid brick as built",
    "Solid brick, with internal insulation": "Stone/solid brick with 100 mm external or internal insulation",
    "Solid brick, as built, insulated": "Stone/solid brick with 100 mm external or internal insulation",
    "Solid brick, with external insulation": "Stone/solid brick with 100 mm external or internal insulation",
    "Solid brick, as built, partial insulation": "Stone/solid brick with 50 mm external or internal insulation",
    ############################
    # Timber frame wall mappings
    ############################
    # These mappings are perhaps the most dubious due to the lack of timber options in the RdSAP table
    "Timber frame, as built, insulated": "Timber frame with internal insulation",
    "Timber frame, with additional insulation": "Timber frame with internal insulation",
    "Timber frame, as built, partial insulation": "Timber frame as built",
    "Timber frame, as built, no insulation": "Timber frame as built",
    "Timber frame, with external insulation": "Timber frame with internal insulation",
    "Timber frame, with internal insulation": "Timber frame with internal insulation",
    ############################
    # Sandstone/limestones wall mappings
    ############################
    "Sandstone or limestone, as built, no insulation": "Stone: sandstone or limestone as built",
    "Sandstone or limestone, with internal insulation": "Stone/solid brick with 100 mm external or internal insulation",
    "Sandstone or limestone, as built, partial insulation": "Stone/solid brick with 50 mm external or internal "
                                                            "insulation",
    "Sandstone, as built, no insulation": "Stone: sandstone or limestone as built",
    "Sandstone or limestone, as built, insulated": "Stone/solid brick with 100 mm external or internal insulation",
    "Sandstone, as built, insulated": "Stone/solid brick with 100 mm external or internal insulation",
    "Sandstone, with internal insulation": "Stone/solid brick with 100 mm external or internal insulation",
    "Sandstone or limestone, with external insulation": "Stone/solid brick with 100 mm external or internal "
                                                        "insulation",
    "Sandstone, with external insulation": "Stone/solid brick with 100 mm external or internal insulation",
    "Sandstone, as built, partial insulation": "Stone/solid brick with 50 mm external or internal insulation",
    ############################
    # Granite/whinstone wall mappings
    ############################
    "Granite or whinstone, as built, no insulation": "Stone: granite or whinstone as built",
    "Granite or whinstone, with internal insulation": "Stone/solid brick with 100 mm external or internal insulation",
    "Granite or whin, with internal insulation": "Stone/solid brick with 100 mm external or internal insulation",
    "Granite or whinstone, as built, partial insulation": "Stone/solid brick with 50 mm external or internal "
                                                          "insulation",
    "Granite or whinstone, as built, insulated": "Stone/solid brick with 100 mm external or internal insulation",
    "Granite or whin, as built, insulated": "Stone/solid brick with 100 mm external or internal insulation",
    "Granite or whinstone, with external insulation": "Stone/solid brick with 100 mm external or internal insulation",
    "Granite or whin, with external insulation": "Stone/solid brick with 100 mm external or internal insulation",
    ############################
    # System built wall mappings
    ############################
    "System built, as built, no insulation": "System build as built",
    "System built, as built, partial insulation": "System build with 50 mm external or internal insulation",
    "System built, with internal insulation": "System build with 100 mm external or internal insulation",
    "System built, with external insulation": "System build with 100 mm external or internal insulation",
    "System built, as built, insulated": "System build with 100 mm external or internal insulation",
    ############################
    # Cob wall mappings
    ############################
    "Cob, as built, no insulation": "Cob as built",
    "Cob, as built": "Cob as built",
    "Cob, with external insulation": "Cob with 100 mm external or internal insulation",
    "Cob, with internal insulation": "Cob with 100 mm external or internal insulation",
    "Cob,": "Cob as built",
    ############################
    # Park home mappings
    ############################
    "Park home wall, as built": "Park home as built",
    "Park home wall, with external insulation": "Park home with additional insulation",
    "Park home wall, with internal insulation": "Park home with additional insulation",
}

########################################################################################################################
# These following tables define table s9 and s10 which are used to assign roofs with their assumed u-values.
# The tables can be found on pages 23 and 24 of the BRE document
# https://bregroup.com/wp-content/uploads/2019/09/RdSAP_2012_9.94-20-09-2019.pdf
########################################################################################################################

s9_list = [
    {
        "Insulation_thickness_mm": None,
        "Slates_or_tiles_U_value_W_m2K": 2.3,
        "Thatched_roof_U_value_W_m2K": 0.35,
    },
    {
        "Insulation_thickness_mm": 12,
        "Slates_or_tiles_U_value_W_m2K": 1.5,
        "Thatched_roof_U_value_W_m2K": 0.32,
    },
    {
        "Insulation_thickness_mm": 25,
        "Slates_or_tiles_U_value_W_m2K": 1.0,
        "Thatched_roof_U_value_W_m2K": 0.30,
    },
    {
        "Insulation_thickness_mm": 50,
        "Slates_or_tiles_U_value_W_m2K": 0.68,
        "Thatched_roof_U_value_W_m2K": 0.25,
    },
    {
        "Insulation_thickness_mm": 75,
        "Slates_or_tiles_U_value_W_m2K": 0.50,
        "Thatched_roof_U_value_W_m2K": 0.22,
    },
    {
        "Insulation_thickness_mm": 100,
        "Slates_or_tiles_U_value_W_m2K": 0.40,
        "Thatched_roof_U_value_W_m2K": 0.20,
    },
    {
        "Insulation_thickness_mm": 150,
        "Slates_or_tiles_U_value_W_m2K": 0.30,
        "Thatched_roof_U_value_W_m2K": 0.17,
    },
    {
        "Insulation_thickness_mm": 200,
        "Slates_or_tiles_U_value_W_m2K": 0.21,
        "Thatched_roof_U_value_W_m2K": 0.14,
    },
    {
        "Insulation_thickness_mm": 250,
        "Slates_or_tiles_U_value_W_m2K": 0.17,
        "Thatched_roof_U_value_W_m2K": 0.12,
    },
    {
        "Insulation_thickness_mm": 270,
        "Slates_or_tiles_U_value_W_m2K": 0.16,
        "Thatched_roof_U_value_W_m2K": 0.12,
    },
    {
        "Insulation_thickness_mm": 300,
        "Slates_or_tiles_U_value_W_m2K": 0.14,
        "Thatched_roof_U_value_W_m2K": 0.11,
    },
    {
        "Insulation_thickness_mm": 350,
        "Slates_or_tiles_U_value_W_m2K": 0.12,
        "Thatched_roof_U_value_W_m2K": 0.10,
    },
    {
        "Insulation_thickness_mm": 400,
        "Slates_or_tiles_U_value_W_m2K": 0.11,
        "Thatched_roof_U_value_W_m2K": 0.09,
    },
]

s10_list = [
    {
        "Age_band": "A, B, C, D",
        "Insulation_Thickness": "none",
        "Pitched_slates_or_tiles_insulation_between_joists_or_unknown": 2.3,
        "Pitched_slates_or_tiles_insulation_at_rafters": 2.3,
        "Flat_roof": 2.3,
        "Room_in_roof_slates_or_tiles": 2.3,
        "Thatched_roof": 0.35,
        "Thatched_roof_room_in_roof": 0.25,
        "Park_home": None,
    },
    {
        "Age_band": "E",
        "Insulation_Thickness": 12,
        "Pitched_slates_or_tiles_insulation_between_joists_or_unknown": 1.5,
        "Pitched_slates_or_tiles_insulation_at_rafters": 1.5,
        "Flat_roof": 1.5,
        "Room_in_roof_slates_or_tiles": 1.5,
        "Thatched_roof": 0.35,
        "Thatched_roof_room_in_roof": 0.25,
        "Park_home": None,
    },
    {
        "Age_band": "F",
        "Insulation_Thickness": 50,
        "Pitched_slates_or_tiles_insulation_between_joists_or_unknown": 0.68,
        "Pitched_slates_or_tiles_insulation_at_rafters": 0.68,
        "Flat_roof": 0.68,
        "Room_in_roof_slates_or_tiles": 0.80,
        "Thatched_roof": 0.35,
        "Thatched_roof_room_in_roof": 0.25,
        "Park_home": 1.7,
    },
    {
        "Age_band": "G",
        "Insulation_Thickness": 100,
        "Pitched_slates_or_tiles_insulation_between_joists_or_unknown": 0.40,
        "Pitched_slates_or_tiles_insulation_at_rafters": 0.40,
        "Flat_roof": 0.40,
        "Room_in_roof_slates_or_tiles": "0.50",
        "Thatched_roof": 0.35,
        "Thatched_roof_room_in_roof": 0.25,
        "Park_home": 0.6,
    },
    {
        "Age_band": "H",
        "Insulation_Thickness": 150,
        "Pitched_slates_or_tiles_insulation_between_joists_or_unknown": 0.30,
        "Pitched_slates_or_tiles_insulation_at_rafters": 0.35,
        "Flat_roof": 0.35,
        "Room_in_roof_slates_or_tiles": 0.35,
        "Thatched_roof": 0.35,
        "Thatched_roof_room_in_roof": 0.25,
        "Park_home": None,
    },
    {
        "Age_band": "I",
        "Insulation_Thickness": 150,
        "Pitched_slates_or_tiles_insulation_between_joists_or_unknown": 0.26,
        "Pitched_slates_or_tiles_insulation_at_rafters": 0.35,
        "Flat_roof": 0.35,
        "Room_in_roof_slates_or_tiles": 0.35,
        "Thatched_roof": 0.35,
        "Thatched_roof_room_in_roof": 0.25,
        "Park_home": 0.35,
    },
    {
        "Age_band": "J",
        "Insulation_Thickness": 270,
        "Pitched_slates_or_tiles_insulation_between_joists_or_unknown": 0.16,
        "Pitched_slates_or_tiles_insulation_at_rafters": 0.20,
        "Flat_roof": 0.25,
        "Room_in_roof_slates_or_tiles": 0.30,
        "Thatched_roof": 0.30,
        "Thatched_roof_room_in_roof": 0.25,
        "Park_home": None,
    },
    {
        "Age_band": "K",
        "Insulation_Thickness": 270,
        "Pitched_slates_or_tiles_insulation_between_joists_or_unknown": 0.16,
        "Pitched_slates_or_tiles_insulation_at_rafters": 0.20,
        "Flat_roof": 0.25,
        "Room_in_roof_slates_or_tiles": 0.25,
        "Thatched_roof": 0.25,
        "Thatched_roof_room_in_roof": 0.25,
        "Park_home": 0.30,
    },
    {
        "Age_band": "L",
        "Insulation_Thickness": 270,
        "Pitched_slates_or_tiles_insulation_between_joists_or_unknown": 0.16,
        "Pitched_slates_or_tiles_insulation_at_rafters": 0.18,
        "Flat_roof": 0.18,
        "Room_in_roof_slates_or_tiles": 0.18,
        "Thatched_roof": 0.18,
        "Thatched_roof_room_in_roof": 0.18,
        "Park_home": None,
    },
]

table_s9 = pd.DataFrame(s9_list)

table_s10 = pd.DataFrame(s10_list)

########################################################################################################################
# Table s11 is used for assigning the u-values of floors when the insulation thickness is unknown
# which can be found on page 25 of the BRE document
# https://bregroup.com/wp-content/uploads/2019/09/RdSAP_2012_9.94-20-09-2019.pdf
#
# The thickness values are in mm
########################################################################################################################

s11_list = [
    {
        "Age_band": "A, B",
        "Floor_construction": "suspended timber",
        "England_Wales": 0,
        "Scotland": 0,
        "Northern_Ireland": 0,
        "Park_home": 0,
    },
    {
        "Age_band": "C to F",
        "Floor_construction": "solid",
        "England_Wales": 0,
        "Scotland": 0,
        "Northern_Ireland": 0,
        "Park_home": 0,
    },
    {
        "Age_band": "G",
        "Floor_construction": "solid",
        "England_Wales": 0,
        "Scotland": 0,
        "Northern_Ireland": 0,
        "Park_home": 25,
    },
    {
        "Age_band": "H",
        "Floor_construction": "solid",
        "England_Wales": 0,
        "Scotland": 25,
        "Northern_Ireland": 25,
        "Park_home": 0,
    },
    {
        "Age_band": "I",
        "Floor_construction": "solid",
        "England_Wales": 25,
        "Scotland": 50,
        "Northern_Ireland": 50,
        "Park_home": 50,
    },
    {
        "Age_band": "J",
        "Floor_construction": "solid",
        "England_Wales": 75,
        "Scotland": 75,
        "Northern_Ireland": 0,
        "Park_home": 0,
    },
    {
        "Age_band": "K",
        "Floor_construction": "solid",
        "England_Wales": 100,
        "Scotland": 100,
        "Northern_Ireland": 100,
        "Park_home": 70,
    },
    {
        "Age_band": "L",
        "Floor_construction": "solid",
        "England_Wales": 100,
        "Scotland": 120,
        "Northern_Ireland": 100,
        "Park_home": 0,
    },
]

table_s11 = pd.DataFrame(s11_list)

########################################################################################################################
# Table s12 is used for assigning the u-values of floors to unheated spaces or external air
# which can be found on page 26 of the BRE document, section 5.6
# https://bregroup.com/wp-content/uploads/2019/09/RdSAP_2012_9.94-20-09-2019.pdf
#
# the insulation_{thickness} fields indicate the u-value at that insulation thickness
########################################################################################################################

s12_list = [
    {
        "age_band": "A",
        "insulation_0": 1.2,
        "insulation_50": 0.5,
        "insulation_100": 0.3,
        "insulation_150": 0.22,
    },
    {
        "age_band": "B",
        "insulation_0": 1.2,
        "insulation_50": 0.5,
        "insulation_100": 0.3,
        "insulation_150": 0.22,
    },
    {
        "age_band": "C",
        "insulation_0": 1.2,
        "insulation_50": 0.5,
        "insulation_100": 0.3,
        "insulation_150": 0.22,
    },
    {
        "age_band": "D",
        "insulation_0": 1.2,
        "insulation_50": 0.5,
        "insulation_100": 0.3,
        "insulation_150": 0.22,
    },
    {
        "age_band": "E",
        "insulation_0": 1.2,
        "insulation_50": 0.5,
        "insulation_100": 0.3,
        "insulation_150": 0.22,
    },
    {
        "age_band": "F",
        "insulation_0": 1.2,
        "insulation_50": 0.5,
        "insulation_100": 0.3,
        "insulation_150": 0.22,
    },
    {
        "age_band": "G",
        "insulation_0": 1.2,
        "insulation_50": 0.5,
        "insulation_100": 0.3,
        "insulation_150": 0.22,
    },
    {
        "age_band": "H",
        "insulation_0": 0.51,
        "insulation_50": 0.5,
        "insulation_100": 0.3,
        "insulation_150": 0.22,
    },
    {
        "age_band": "I",
        "insulation_0": 0.51,
        "insulation_50": 0.5,
        "insulation_100": 0.3,
        "insulation_150": 0.22,
    },
    {
        "age_band": "J",
        "insulation_0": 0.25,
        "insulation_50": 0.25,
        "insulation_100": 0.25,
        "insulation_150": 0.22,
    },
    {
        "age_band": "K",
        "insulation_0": 0.22,
        "insulation_50": 0.22,
        "insulation_100": 0.22,
        "insulation_150": 0.22,
    },
    {
        "age_band": "L",
        "insulation_0": 0.22,
        "insulation_50": 0.22,
        "insulation_100": 0.22,
        "insulation_150": 0.22,
    },
]

table_s12 = pd.DataFrame(s12_list)


########################################################################################################################
#
# this map is used to clean the floor value field we see in EPCs
#
########################################################################################################################
def ordinal(n):
    if 10 <= n % 100 <= 20:
        suffix = "th"
    else:
        suffix = {1: "st", 2: "nd", 3: "rd"}.get(n % 10, "th")

    return str(n) + suffix


FLOOR_LEVEL_MAP = {
    "Basement": -1,
    "Ground": 0,
    "ground floor": 0,
    "mid floor": 1,
    "top floor": 5,
    "20+": 20,
    "21st or above": 21,
    **{str(i).zfill(2): i for i in range(0, 51)},
    **{ordinal(i): i for i in range(-1, 51)},
    **{str(i): i for i in range(-1, 51)},
    **{i: i for i in range(-1, 51)},
}
