# -*- coding: utf-8 -*-
"""
Some constants for carbatpy usage are set here

* he directory, where the results shall be written.

Created on Sun Nov  5 16:01:02 2023

@author: atakan
"""

import os
import copy
import platform
from pathlib import Path

# Global Constants
_T_SURROUNDING = 288.15  # K
_P_SURROUNDING = 1.013e5  # Pa

# Directories
try:
    script_path = Path(__file__).resolve()
    script_dir = script_path.parent
except NameError:
    script_dir = Path.cwd()

parent_dir = script_dir.parent
grandparent_dir = parent_dir.parent

# Base dir
try:
    _CARBATPY_BASE_DIR = Path(os.environ["CARBATPY_BASE_DIR"]).expanduser().resolve()
except KeyError:
    if "carbatpy" in script_dir.parts:
        _CARBATPY_BASE_DIR = script_dir
    else:
        _CARBATPY_BASE_DIR = grandparent_dir

# Results dir
try:
    _RESULTS_DIR = Path(os.environ["CARBATPY_RES_DIR"]).expanduser()
except KeyError:
    temp = os.environ.get("TEMP") or os.environ.get("TMP")
    if temp:
        _RESULTS_DIR = Path(temp).expanduser()
    else:
        _RESULTS_DIR = Path.cwd() / "tests" / "test_files"

# Data dir
_CARBATPY_DATA_DIR = _CARBATPY_BASE_DIR / "data"


# REFPROP
if platform.system() == "Windows":
    os.environ["RPPREFIX"] = r"C:\Program Files (x86)\REFPROP"

RPPREFIX = os.environ["RPPREFIX"]

# TREND
TREND = {
    "TREND_INSTALLED": True,
    "USE_TREND": False,
    "TREND_DLL": "",
    "TREND_PATH": "",
}

if TREND["TREND_INSTALLED"]:
    try:
        TREND["TREND_DLL"] = os.environ["TREND_DLL"]
        TREND["TREND_PATH"] = os.environ["TREND_PATH"]
        TREND["TREND_PATH_BASE"] = os.environ["TREND_PATH"]

    except KeyError:
        TREND_DLL = ""
        TREND_PATH = ""
        TREND["TREND_PATH_BASE"] = ""
        TREND["TREND_INSTALLED"] = False

# Default values:-------------------------------------------------
fl_properties_names = (
    "Temperature",
    "Pressure",
    "spec_Enthalpy",
    "spec_Volume",
    "spec_Entropy",
    "quality_mass",
    "spec_internal_Energy",
    "viscosity",
    "thermal_conductivity",
    "Prandtl_number",
    "isobaric_heat_capacity",
    "speed_of_sound",
    "molecular_mass",
)

fl_pr_variable_names = {
    "temperature": "T",
    "pressure": "p",
    "enthalpy": "h",
    "sp_volume": "v",
    "entropy": "s",
    "quality": "q",
    "int_energy": "u",
    "viscosity": "eta",
    "thermal_conductivity": "k",
    "prandtl": "Pr",
    "cp": "cp",
    "speed_of_sound": "w_s",
    "molecular_mass": "M",
}

fl_properties_names_trend = (
    "Temperature",
    "Pressure",
    "spec_Enthalpy",
    "spec_Volume",
    "spec_Entropy",
    "quality_mass",
    "spec_internal_Energy",
    "viscosity",
    "thermal_conductivity",
    "isobaric_heat_capacity",
    "speed_of_sound",
)

fl_pr_variable_names_trend = {
    "temperature": "T",
    "pressure": "p",
    "enthalpy": "h",
    "sp_volume": "v",
    "entropy": "s",
    "quality": "q",
    "int_energy": "u",
    "viscosity": "eta",
    "thermal_conductivity": "k",
    "cp": "cp",
    "speed_of_sound": "w_s",
}

THERMO_STRING = "T;P;H;V;S;QMASS;E"
TRANS_STRING = THERMO_STRING + ";VIS;TCX;PRANDTL;CP;W;M"
THERMO_TREND = request = [
    "T",
    "P",
    "H",
    "D",
    "S",
    "QEOS",
    "U",
]  # careful density not volume
TRANS_TREND = copy.copy(THERMO_TREND)
TRANS_TREND.extend(["ETA", "TCX", "CP", "WS"])  # no molecular mass


TRANS_TREND_MIX = copy.copy(THERMO_TREND)
TRANS_TREND_MIX.extend(["ETA_ECS", "TCX_ECS", "CP", "WS"])  # no molecular mass

CB_FLUID_DEFAULT = {
    "PROPS": "REFPROP",  # choice: "TREND" or "REFPROP"
    "DLL_SELECT": "2dll",  # choice: "2dll" or "dll"
    "UNITS": "MASS BASE SI",
    "Property_Names": fl_properties_names,
    "Property_Names_Short": fl_pr_variable_names,
    "Property_Names_Trend": fl_properties_names_trend,
    "Property_Names_Short_Trend": fl_pr_variable_names_trend,
    "THERMO_STRING": THERMO_STRING,
    "TRANS_STRING": TRANS_STRING,
    "THERMO_TREND": THERMO_TREND,
    "TRANS_TREND": TRANS_TREND,
    "TRANS_TREND_MIX": TRANS_TREND_MIX,
    "TREND": TREND,
    "RPPREFIX": RPPREFIX,
}

PLOT_INFO = {
    "fig": None,
    "ax": None,
    "what": [2, 0],
    "col": ["r:", "k"],
    "label": ["compressor", "xx"],
    "x-shift": [0, 0],
}

COMPONENT_DEFAULTS = {"Plot": PLOT_INFO, "n_points": 50}

CB_DEFAULTS = {
    "Fluid_Defaults": CB_FLUID_DEFAULT,
    "Components": COMPONENT_DEFAULTS,
    "General": {
        "T_SUR": _T_SURROUNDING,
        "P_SUR": _P_SURROUNDING,
        "RES_DIR": str(_RESULTS_DIR),
        "CB_DIR": str(_CARBATPY_BASE_DIR),
        "CB_DATA": str(_CARBATPY_DATA_DIR),
    },
}
