import os
import sys
import numpy as np
import pandas as pd
import seaborn as sns
import global_variables as gv

### conversion factors
energy_densities_dict = {"NMC": .25, "LFP": .16} # [kWh/kg] Source: Fig. 16 in Schöberl, J., Ank, M., Schreiber, M., Wassiliadis, N. & Lienkamp, M. Thermal runaway propagation in automotive lithium-ion batteries with NMC-811 and LFP cathodes: Safety requirements and impact on system integration. eTransportation 19, 100305 (2024).

colors_dict = dict()
# col_palette = sns.color_palette("tab10")
colors_dict["all"] = "black"
colors_dict["CN"] = "#FF6666"
colors_dict["US"] = "#71BEB6"
colors_dict["EU"] = "#224B89"
colors_dict["KRJP"] = "#FFA633"

csv_file_names_dict = {
    "LFP_Ni0_Li1_Co0_Gr1_BG0_GE1_PDFlin_N11308"         : "LFP_Ni0_Li1_Co0_Gr1_BG0_GE1_PDFlin_N11308_03-20_13-16.csv",
    "LFP_Ni0_Li1_Co0_Gr0_BG0_GE0_PDFlin_N11308"         : "LFP_Ni0_Li1_Co0_Gr0_BG0_GE0_PDFlin_N11308_03-20_19-45.csv",
    "LFP_Ni0_Li0_Co0_Gr1_BG0_GE0_PDFlin_N11308"         : "LFP_Ni0_Li0_Co0_Gr1_BG0_GE0_PDFlin_N11308_03-20_22-10.csv",
    "LFP_Ni0_Li0_Co0_Gr0_BG0_GE1_PDFlin_N11308"         : "LFP_Ni0_Li0_Co0_Gr0_BG0_GE1_PDFlin_N11308_03-20_17-20.csv",

    "LFP_Ni0_Li1_Co0_Gr1_BG0_GE1_PDFlin_N11308_hydro0"  : "LFP_Ni0_Li1_Co0_Gr1_BG0_GE1_PDFlin_N11308_03-21_15-14.csv",
    "LFP_Ni0_Li1_Co0_Gr0_BG0_GE0_PDFlin_N11308_hydro0"  : "LFP_Ni0_Li1_Co0_Gr0_BG0_GE0_PDFlin_N11308_03-21_21-43.csv",
    "LFP_Ni0_Li0_Co0_Gr1_BG0_GE0_PDFlin_N11308_hydro0"  : "LFP_Ni0_Li0_Co0_Gr1_BG0_GE0_PDFlin_N11308_03-22_00-07.csv",
    "LFP_Ni0_Li0_Co0_Gr0_BG0_GE1_PDFlin_N11308_hydro0"  : "LFP_Ni0_Li0_Co0_Gr0_BG0_GE1_PDFlin_N11308_03-21_19-19.csv",

    "LFP_Ni0_Li1_Co0_Gr1_BG0_GE1_PDFlin_N11308_hydro1"  : "LFP_Ni0_Li1_Co0_Gr1_BG0_GE1_PDFlin_N11308_03-21_02-15.csv",
    "LFP_Ni0_Li1_Co0_Gr0_BG0_GE0_PDFlin_N11308_hydro1"  : "LFP_Ni0_Li1_Co0_Gr0_BG0_GE0_PDFlin_N11308_03-21_08-44.csv",
    "LFP_Ni0_Li0_Co0_Gr1_BG0_GE0_PDFlin_N11308_hydro1"  : "LFP_Ni0_Li0_Co0_Gr1_BG0_GE0_PDFlin_N11308_03-21_11-07.csv",
    "LFP_Ni0_Li0_Co0_Gr0_BG0_GE1_PDFlin_N11308_hydro1"  : "LFP_Ni0_Li0_Co0_Gr0_BG0_GE1_PDFlin_N11308_03-21_06-20.csv",

    "NMC_Ni1_Li1_Co1_Gr1_BG0_GE1_PDFlin_N11308"         : "NMC_Ni1_Li1_Co1_Gr1_BG0_GE1_PDFlin_N11308_05-10_15-23.csv",
    "NMC_Ni1_Li0_Co0_Gr0_BG0_GE0_PDFlin_N11308"         : "NMC_Ni1_Li0_Co0_Gr0_BG0_GE0_PDFlin_N11308_05-10_22-06.csv",
    "NMC_Ni0_Li1_Co0_Gr0_BG0_GE0_PDFlin_N11308"         : "NMC_Ni0_Li1_Co0_Gr0_BG0_GE0_PDFlin_N11308_05-11_00-29.csv",
    "NMC_Ni0_Li0_Co1_Gr0_BG0_GE0_PDFlin_N11308"         : "NMC_Ni0_Li0_Co1_Gr0_BG0_GE0_PDFlin_N11308_05-11_02-54.csv",
    "NMC_Ni0_Li0_Co0_Gr1_BG0_GE0_PDFlin_N11308"         : "NMC_Ni0_Li0_Co0_Gr1_BG0_GE0_PDFlin_N11308_05-11_05-18.csv",
    "NMC_Ni0_Li0_Co0_Gr0_BG0_GE1_PDFlin_N11308"         : "NMC_Ni0_Li0_Co0_Gr0_BG0_GE1_PDFlin_N11308_05-10_19-42.csv",

    "NMC_Ni1_Li1_Co1_Gr1_BG0_GE1_PDFlin_Nilat_N11308"   : "NMC_Ni1_Li1_Co1_Gr1_BG0_GE1_PDFlin_Nilat_N11308_05-09_20-55.csv",
    "NMC_Ni1_Li0_Co0_Gr0_BG0_GE0_PDFlin_Nilat_N11308"   : "NMC_Ni1_Li0_Co0_Gr0_BG0_GE0_PDFlin_Nilat_N11308_05-10_03-36.csv",
    "NMC_Ni0_Li1_Co0_Gr0_BG0_GE0_PDFlin_Nilat_N11308"   : "NMC_Ni0_Li1_Co0_Gr0_BG0_GE0_PDFlin_Nilat_N11308_05-10_05-59.csv",
    "NMC_Ni0_Li0_Co1_Gr0_BG0_GE0_PDFlin_Nilat_N11308"   : "NMC_Ni0_Li0_Co1_Gr0_BG0_GE0_PDFlin_Nilat_N11308_05-10_08-22.csv",
    "NMC_Ni0_Li0_Co0_Gr1_BG0_GE0_PDFlin_Nilat_N11308"   : "NMC_Ni0_Li0_Co0_Gr1_BG0_GE0_PDFlin_Nilat_N11308_05-10_10-46.csv",
    "NMC_Ni0_Li0_Co0_Gr0_BG0_GE1_PDFlin_Nilat_N11308"   : "NMC_Ni0_Li0_Co0_Gr0_BG0_GE1_PDFlin_Nilat_N11308_05-10_01-13.csv",

    # others
    "NMC_Ni1_Li1_Co1_Gr1_BG0_GE1_PDF_N1000"             : r"other\NMC_Ni1_Li1_Co1_Gr1_BG0_GE1_PDF_N1000_02-22_13-52.csv",
    "NMC_Ni0_Li0_Co0_Gr0_BG1_GE0_PDFlin_N11308"         : r"other\NMC_Ni0_Li0_Co0_Gr0_BG1_GE0_PDFlin_N11308_02-22_04-38.csv",
    "NMC_Ni1_Li1_Co1_Gr1_BG1_GE1_PDFlin_N11308"         : r"other\NMC_Ni1_Li1_Co1_Gr1_BG1_GE1_PDFlin_N11308_02-21_23-59.csv",


    # regions
    "LFP_Europe_Ni0_Li1_Co0_Gr1_BG0_GE1_PDFlin_hydroshare(0.0)_N1000":  r"regions\LFP_Europe_Ni0_Li1_Co0_Gr1_BG0_GE1_PDFlin_hydroshare(0.0)_N1000_03-24_18-41.csv",
    "LFP_Europe_Ni0_Li1_Co0_Gr1_BG0_GE1_PDFlin_hydroshare(0.1)_N1000":  r"regions\LFP_Europe_Ni0_Li1_Co0_Gr1_BG0_GE1_PDFlin_hydroshare(0.1)_N1000_03-24_17-39.csv",
    "LFP_Europe_Ni0_Li1_Co0_Gr1_BG0_GE1_PDFlin_hydroshare(1.0)_N1000":  r"regions\LFP_Europe_Ni0_Li1_Co0_Gr1_BG0_GE1_PDFlin_hydroshare(1.0)_N1000_03-24_19-43.csv",
    "LFP_KRJP_Ni0_Li1_Co0_Gr1_BG0_GE1_PDFlin_hydroshare(0.0)_N1000":    r"regions\LFP_KRJP_Ni0_Li1_Co0_Gr1_BG0_GE1_PDFlin_hydroshare(0.0)_N1000_03-24_19-20.csv",
    "LFP_KRJP_Ni0_Li1_Co0_Gr1_BG0_GE1_PDFlin_hydroshare(0.1)_N1000":    r"regions\LFP_KRJP_Ni0_Li1_Co0_Gr1_BG0_GE1_PDFlin_hydroshare(0.1)_N1000_03-24_18-18.csv",
    "LFP_KRJP_Ni0_Li1_Co0_Gr1_BG0_GE1_PDFlin_hydroshare(1.0)_N1000":    r"regions\LFP_KRJP_Ni0_Li1_Co0_Gr1_BG0_GE1_PDFlin_hydroshare(1.0)_N1000_03-24_20-22.csv",
    "LFP_US_Ni0_Li1_Co0_Gr1_BG0_GE1_PDFlin_hydroshare(0.0)_N1000":      r"regions\LFP_US_Ni0_Li1_Co0_Gr1_BG0_GE1_PDFlin_hydroshare(0.0)_N1000_03-24_19-02.csv",
    "LFP_US_Ni0_Li1_Co0_Gr1_BG0_GE1_PDFlin_hydroshare(0.1)_N1000":      r"regions\LFP_US_Ni0_Li1_Co0_Gr1_BG0_GE1_PDFlin_hydroshare(0.1)_N1000_03-24_18-00.csv",
    "LFP_US_Ni0_Li1_Co0_Gr1_BG0_GE1_PDFlin_hydroshare(1.0)_N1000":      r"regions\LFP_US_Ni0_Li1_Co0_Gr1_BG0_GE1_PDFlin_hydroshare(1.0)_N1000_03-24_20-04.csv",
    "NMC_Europe_Ni1_Li1_Co1_Gr1_BG0_GE1_PDFlin_N1000":                  r"regions\NMC_Europe_Ni1_Li1_Co1_Gr1_BG0_GE1_PDFlin_N3000_05-14_11-45.csv",
    "NMC_Europe_Ni1_Li1_Co1_Gr1_BG0_GE1_PDFlin_Nilat_N1000":            r"regions\NMC_Europe_Ni1_Li1_Co1_Gr1_BG0_GE1_PDFlin_Nilat_N3000_05-24_18-14.csv",
    "NMC_KRJP_Ni1_Li1_Co1_Gr1_BG0_GE1_PDFlin_N1000":                    r"regions\NMC_KRJP_Ni1_Li1_Co1_Gr1_BG0_GE1_PDFlin_N3000_05-14_13-44.csv",
    "NMC_KRJP_Ni1_Li1_Co1_Gr1_BG0_GE1_PDFlin_Nilat_N1000":              r"regions\NMC_KRJP_Ni1_Li1_Co1_Gr1_BG0_GE1_PDFlin_Nilat_N3000_05-24_20-12.csv",
    "NMC_US_Ni1_Li1_Co1_Gr1_BG0_GE1_PDFlin_N1000":                      r"regions\NMC_US_Ni1_Li1_Co1_Gr1_BG0_GE1_PDFlin_N3000_05-14_12-49.csv",
    "NMC_US_Ni1_Li1_Co1_Gr1_BG0_GE1_PDFlin_Nilat_N1000":                r"regions\NMC_US_Ni1_Li1_Co1_Gr1_BG0_GE1_PDFlin_Nilat_N3000_05-24_19-17.csv",

}

### import MC df from exported CSV

def f_return_lcia_df_and_lists(csv_dict_key, functional_unit, reduce_sample_size_to=None):

    file_name_csv = os.path.join(gv.path_project, "data", "MC_runs", csv_file_names_dict[csv_dict_key])
    if reduce_sample_size_to == None:
        # default case for statistic analyses
        df_LCIA = pd.read_csv(file_name_csv).sort_values("LCIA_cell_kg", ascending=True).reset_index(drop=True)
    else:
        # selected head() instead of sample() for reproducibility of figures
        df_LCIA = pd.read_csv(file_name_csv).head(reduce_sample_size_to).sort_values("LCIA_cell_kg", ascending=True).reset_index(drop=True)
    df_LCIA["LCIA_cell_kWh"] = df_LCIA["LCIA_cell_kg"]/energy_densities_dict[csv_dict_key[0:3]]
    datapoints_all_kg = np.array(df_LCIA["LCIA_cell_kg"].to_list())
    datapoints_all_kWh = np.array(df_LCIA["LCIA_cell_kWh"].to_list())

    datapoints_per_region_dict_kg = dict()
    datapoints_per_region_dict_kg["CN"] = np.array(df_LCIA.loc[df_LCIA["production_geography"].str.contains("CN")]["LCIA_cell_kg"].to_list())
    datapoints_per_region_dict_kg["US"] = np.array(df_LCIA.loc[df_LCIA["production_geography"].str.contains("US")]["LCIA_cell_kg"].to_list())
    datapoints_per_region_dict_kg["EU"] = np.array(
        df_LCIA.loc[~df_LCIA["production_geography"].str.contains("US|CN|KR|JP")]["LCIA_cell_kg"].to_list())
    datapoints_per_region_dict_kg["KRJP"] = np.array(
        df_LCIA.loc[(df_LCIA["production_geography"] == "KR") | (df_LCIA["production_geography"] == "JP")][
            "LCIA_cell_kg"].to_list())

    datapoints_per_region_dict_kWh = dict()
    datapoints_per_region_dict_kWh["CN"] = np.array(df_LCIA.loc[df_LCIA["production_geography"].str.contains("CN")]["LCIA_cell_kWh"].to_list())
    datapoints_per_region_dict_kWh["US"] = np.array(df_LCIA.loc[df_LCIA["production_geography"].str.contains("US")]["LCIA_cell_kWh"].to_list())
    datapoints_per_region_dict_kWh["EU"] = np.array(
        df_LCIA.loc[~df_LCIA["production_geography"].str.contains("US|CN|KR|JP")]["LCIA_cell_kWh"].to_list())
    datapoints_per_region_dict_kWh["KRJP"] = np.array(
        df_LCIA.loc[(df_LCIA["production_geography"] == "KR") | (df_LCIA["production_geography"] == "JP")][
            "LCIA_cell_kWh"].to_list())

    if functional_unit == "kg":
        return df_LCIA, datapoints_all_kg, datapoints_per_region_dict_kg, colors_dict
    elif functional_unit == "kWh":
        return df_LCIA, datapoints_all_kWh, datapoints_per_region_dict_kWh, colors_dict
    else:
        return -1