import os
import sys
from logging import config as logging_config

from .logger import LOGGING

logging_config.dictConfig(LOGGING)

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

sys.path.append(BASE_DIR)

DATA_DIR = os.path.abspath(os.environ.get("DATA_DIR", "data"))

os.makedirs(DATA_DIR, exist_ok=True)

TQDM_DISABLE = os.environ.get("TQDM_DISABLE", True)

SCAN_SHOW_LAST_STEP_NUMBERS =  os.environ.get("SCAN_SHOW_LAST_STEP_NUMBERS", 7)

SCAN_SAMPLE_SIZE = os.environ.get("SCAN_SAMPLE_SIZE", 10)

SCAN_RANDOM_STATE = os.environ.get("SCAN_RANDOM_STATE", 42)

SCAN_BAYESIAN_OPTIMIZATION_N_CALLS = os.environ.get("SCAN_BAYESIAN_OPTIMIZATION_N_CALLS", 50)

SCAN_BAYESIAN_OPTIMIZATION_PENALTY = os.environ.get("SCAN_BAYESIAN_OPTIMIZATION_PENALTY", 10)

SCAN_BAYESIAN_OPTIMIZATION_MINIMIZE = os.environ.get("SCAN_BAYESIAN_OPTIMIZATION_MINIMIZE", True)

SCAN_LEAST_SQUARES_FITTING_PENALTY = os.environ.get("SCAN_LEAST_SQUARES_FITTING_PENALTY", 1)

SCAN_LEAST_SQUARES_FITTING_METHOD = os.environ.get("SCAN_LEAST_SQUARES_FITTING_METHOD", "trf")

SCAN_LEAST_SQUARES_FITTING_MAX_NFEV = os.environ.get("SCAN_LEAST_SQUARES_FITTING_MAX_NFEV", 3)

SCAN_LEAST_SQUARES_FITTING_MAX_STEPS = os.environ.get("SCAN_LEAST_SQUARES_FITTING_MAX_STEPS", 2)

SCAN_RESPONSE_MEASUREMENTS_NUM_SINGULAR_VALUES = os.environ.get("SCAN_RESPONSE_MEASUREMENTS_NUM_SINGULAR_VALUES", 15)

SCAN_MAX_TRIES = os.environ.get("SCAN_MAX_TRIES", 3)

SCAN_DELAY = os.environ.get("SCAN_DELAY", 0.1)

SCAN_TOLERANCE = os.environ.get("SCAN_TOLERANCE", 1e-3)

SCAN_PARALLEL = os.environ.get("SCAN_PARALLEL", False)

SCAN_REPEAT = os.environ.get("SCAN_REPEAT", 1)

SCAN_RESPONSE_MEASUREMENTS_RCOND = os.environ.get("SCAN_RESPONSE_MEASUREMENTS_RCOND", 1e-15)

SCAN_RESPONSE_MEASUREMENTS_MAX_ATTEMPTS = os.environ.get("SCAN_RESPONSE_MEASUREMENTS_MAX_ATTEMPTS", 10)

ELEGANT_SIMULATION_DIR = os.path.abspath(os.environ.get("ELEGANT_SIMULATION_DIR", "elegant"))

ELEGANT_SIMULATION_CONFIG_FILE = os.environ.get("ELEGANT_SIMULATION_CONFIG_FILE", os.path.join(ELEGANT_SIMULATION_DIR, "config.ele"))

ELEGANT_SIMULATION_LATTICES_FILE = os.environ.get("ELEGANT_SIMULATION_LATTICES_FILE", os.path.join(ELEGANT_SIMULATION_DIR, "config.lte"))

ELEGANT_SIMULATION_DATA_DIR = os.environ.get("ELEGANT_SIMULATION_DATA_DIR", os.path.join(ELEGANT_SIMULATION_DIR, "results"))

os.makedirs(ELEGANT_SIMULATION_DATA_DIR, exist_ok=True)

ELEGANT_SIMULATION_CONFIG_PARAMETERS_FILE = os.environ.get("ELEGANT_SIMULATION_CONFIG_PARAMETERS_FILE", os.path.join(ELEGANT_SIMULATION_DIR, "config.par"))

open(ELEGANT_SIMULATION_CONFIG_PARAMETERS_FILE, "a").close()

ELEGANT_BEAMLINE_DATA_FILE = os.environ.get("ELEGANT_BEAMLINE_DATA_FILE", os.path.join(ELEGANT_SIMULATION_DATA_DIR, "beamline.mag"))

open(ELEGANT_BEAMLINE_DATA_FILE, "a").close()

ELEGANT_TWISS_DATA_FILE = os.environ.get("ELEGANT_TWISS_DATA_FILE", os.path.join(ELEGANT_SIMULATION_DATA_DIR, "twiss.twi"))

open(ELEGANT_TWISS_DATA_FILE, "a").close()

ELEGANT_CENTROID_DATA_FILE = os.environ.get("ELEGANT_CENTROID_DATA_FILE", os.path.join(ELEGANT_SIMULATION_DATA_DIR, "beam.cen"))

open(ELEGANT_CENTROID_DATA_FILE, "a").close()

ELEGANT_PARAMETERS_DATA_FILE = os.environ.get("ELEGANT_PARAMETERS_DATA_FILE", os.path.join(ELEGANT_SIMULATION_DATA_DIR, "parameters.sdds"))

open(ELEGANT_PARAMETERS_DATA_FILE, "a").close()

ELEGANT_XYZ_DATA_FILE = os.environ.get("ELEGANT_PARAMETERS_DATA_FILE", os.path.join(ELEGANT_SIMULATION_DATA_DIR, "xyz.sdds"))

open(ELEGANT_XYZ_DATA_FILE, "a").close()

ELEGANT_BEAMLINE_DATA_COLUMNS = ["s", "Profile", "ElementName"]

ELEGANT_TWISS_DATA_COLUMNS = ["ElementName", "s", "betax", "betay", "etax", "etay", "pCentral0", "xAperture", "yAperture"]

ELEGANT_CENTROID_DATA_COLUMNS = ["ElementName", "s", "Cx", "Cy", "pCentral"]

ELEGANT_PARAMETERS_DATA_COLUMNS = ["ElementName", "ElementParameter", "ParameterValue", "ElementType", "ElementOccurence", "ElementGroup"]

ELEGANT_ELEMENT_EXIST_PARAMETERS = [*ELEGANT_BEAMLINE_DATA_COLUMNS, *ELEGANT_TWISS_DATA_COLUMNS, *ELEGANT_CENTROID_DATA_COLUMNS, *ELEGANT_PARAMETERS_DATA_COLUMNS]

ELEGANT_DATA_EXIST_FILES = [ELEGANT_BEAMLINE_DATA_FILE, ELEGANT_TWISS_DATA_FILE, ELEGANT_CENTROID_DATA_FILE, ELEGANT_PARAMETERS_DATA_FILE, ELEGANT_XYZ_DATA_FILE]

ELEGANT_EXIST_FILES_WITH_PARAMETERS = [ELEGANT_PARAMETERS_DATA_FILE, ELEGANT_SIMULATION_CONFIG_PARAMETERS_FILE]
