# -*- coding: utf-8 -*-
from astropy import units as u


class Environment(object):
    """This is Class is for to share all the object along all the files
    Tabels,figures and variable will be assigned here.
    """

    def __init__(self):
        # ---------------------------Light Curve Param----------------------------- #
        self.default_cluster = "mode_selection"
        self.sigma = 5
        self.plot_nearby = -1
        self.draw_nearby_names = 1

        self.selection_program = None
        self.text_custom_star_ra = None
        self.text_custom_star_dec = None
        self.text_custom_star_sector = None
        self.custom_star_download_button = None
        self.selection_program_text = None

        self.text_banner = None

        self.v_flag_duplicate = None
        self.v_flag_source = None
        self.v_flag_check = None

        self.text_flag_duplicate = None
        self.text_flag_source = None
        self.text_flag_check = None

        self.text_catalog_query = None
        self.text_id_mycatalog_query = None
        self.text_id_query = None
        self.text_Notes = None
        self.text_Notes_w = None

        self.show_error_bar = None
        self.aperture_setting = 1

        self.extra_flag_file = "extra_flag.flag"
        self.current_flux_dataframe = None

        # ----------------------------Source Table--------------------------------- #
        self.tb_source = 2
        self.tb_lightcurve = None
        self.tb_periodogram = None
        self.tb_tpf = None
        self.tpf_flux = None
        self.table_periodogram = None
        self.tb_periodogram_se_tb = None
        self.tb_nearby = None
        self.tb_catalog_main = None
        self.tb_catalog_all = None
        self.tb_isochrone = None
        self.tb_nearby_star = None
        self.tb_nearby_star_table = None

        self.fig_hr = None
        self.fig_position = None

        self.text_age = None
        self.text_metallicity = None
        self.text_extinction_av = None
        self.text_distance = None
        self.generate_isochrone_button = None
        self.delete_isochrone_button = None
        self.int_select_sector = None
        self.sector = 6

        # ----------------------------Figures ------------------------------------ #
        self.plot_width = 810
        self.plot_height = 300
        self.fiducial_frame = 0

        self.selection = {
            "selection_color": "black",
            "nonselection_fill_alpha": 0.7,
            "nonselection_fill_color": "grey",
            "selection_fill_alpha": 1,
        }

        self.selection_osc = {
            "line_color": "green",
            "nonselection_line_color": "#1F77B4",
            "nonselection_line_alpha": 1,
        }

        self.selection_l = {
            "line_color": "#1F77B4",
            "nonselection_line_color": "#1F77B4",
            "nonselection_line_alpha": 1,
        }

        self.selection_2 = {
            "selection_color": "red",
            "nonselection_fill_color": "blue",
            "nonselection_line_color": "blue",
            "nonselection_line_alpha": 0.7,
        }

        self.TOOLTIPS = [
            ("index", "$index"),
            ("(x,y)", "($x, $y)"),
        ]

        self.fig_lightcurve = None
        self.fig_tpf = None
        self.fig_stretch = None
        self.fig_periodogram = None

        # --------------------------- Button -------------------------------------#
        self.Generate_lc_button = None
        self.reset_axis_lc_button = None
        self.reset_dflt_lc_button = None

        self.next_button = None
        self.previous_button = None

        self.reset_axes_prd_button = None
        self.saveas_prd_tb_button = None
        self.save_prd_tb_button = None
        self.reset_prd_tb_button = None
        self.load_prd_tb_button = None
        self.save_userinput_button = None
        self.save_current_button = None
        self.aperture_selection_button = None
        self.update_catalog_button = None
        self.update_id_mycatalog_button = None
        self.update_id_button = None

        from bokeh.models import Div

        self.div_spinner = Div(text="", width=120, height=120)
        self.spinner_text = """
        <div class="loader">
        <style scoped>
        .loader {
            border: 16px solid #f3f3f3;
            border-top: 16px solid #3498db;
            border-radius: 50%;
            width: 120px;
            height: 120px;
            animation: spin 2s linear infinite;
        }

        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
        </style>
        </div>
        """

        self.show_spinner_button = None

        #  Analysis Window
        self.tb_lc_an1 = None
        self.tb_lc_an2 = None
        self.fig_lc_an1 = None
        self.fig_lc_an2 = None
        self.fig_pr_an1 = None
        self.fig_pr_an2 = None

        self.tb_pr_an1 = None
        self.tb_pr_an2 = None

        self.text_p1 = None
        self.text_p0 = None
        self.text_p2 = None
        self.text_pe = None

        self.period_recompute_button = None
        self.fold_button = None
        self.check_analysis = None

        # Control Panel
        self.Samp_status = 0
        self.Samp_selection = "Server"
        self.Control_function = "None"
        self.catalog_main = ""
        self.catalog_whole_gaia = None
        self.ds9_command_button = None
        self.ds9_catalog_status = None
        self.gaia_Gmag_start = None
        self.gaia_Gmag_end = None
        self.whole_gaia_filter = None

        self.gaia_Gmag_start_text = None
        self.gaia_Gmag_end_text = None
        self.gaia_update_button = None
        self.gaia_radius_text = None
        self.isochrone_data = None
        self.catalog_find_from_isocrhone = None

        self.text_banner_Gmag = None
        self.text_banner_bp_rp = None
        self.text_banner_dmin = None

        # interactive
        self.fig_tpfint = None
        self.stretch_sliderint = None
        self.dnu_slider = None
        self.r_button = None
        self.l_button = None
        self.rr_button = None
        self.ll_button = None

        self.minimum_frequency = None
        self.maximum_frequency = None
        self.maxdnu = None
        self.dnu_val = None

        self.minimum_frequency_text = None
        self.maximum_frequency_text = None
        self.maxdnu_text = None
        self.dnu_text = None

        self.update_int_button = None

        self.mesa_osc_data = None
        self.fig_mesa_int = None
        self.mesa_int_slider = None
        self.tb_mesa_osc_l0 = None
        self.tb_mesa_osc_l1 = None
        self.tb_mesa_osc_l2 = None

        self.int_select_mass = None
        self.int_select_y = None
        self.int_select_age = None
        self.int_select_z = None
        self.int_select_alpha = None

        self.comp_data = None

        self.interactive_file_control = -1

        self.source_int_slider = None
        self.frequency_minimum_text1 = None
        self.frequency_maximum_text1 = None
        self.frequency_maxdnu_text1 = None
        self.update_int_source_button = None

        self.set_value_dict = None

        self.tb_oscillation_modell0 = None
        self.tb_oscillation_modell1 = None
        self.tb_oscillation_modell2 = None
        self.plot_mesa_osc = None
        self.text_osc_query = None

        # for new model_selection
        self.tb_other_periodogram = None
        self.fig_other_periodogram = None
        self.tb_grid_source = None
        self.table_se_first = None
        self.table_se_second = None
        self.test_button = None
        self.freq_round = 10
        self.tb_echelle_diagram = None
        self.frequency_unit_string = "uHz"
        self.frequency_unit = u.Unit(self.frequency_unit_string)
        self.power_unit_string = "u.electron/u.s"
        self.power_unit = u.Unit(self.power_unit_string)

        #  Buttons
        self.clear_se_table1_button = None
        self.find_peaks_button = None
        self.echelle_noise_cuttoff_text = None
        self.clear_se_grid_prd_button = None
        self.select_mode_menu = None
        self.mode_apply_button = None
        self.move_se_1_2_button = None
        self.move_se_2_1_button = None
        self.delete_row_button = None
        self.tb_other_source = None
        self.save_table_2_button = None
        self.load_table_2_button = None
        self.select_grid_color = None
        self.select_grid_marker = None
        self.inverted_slider = None
        self.select_color_palette = None
        self.grid_circle_size = None
        self.clear_se_table2_button = None
        self.check_color_map_lock = None
        self.check_periodogram_axis_scale = None
        self.check_show_horizontal_lines = None
        self.check_make_grid = None
        self.check_show_echelle = None
        self.check_show_modes_grid = None
        self.check_reverse_color_palette = None
        self.inverted_slider_max_value_text = None
        self.default_color_palette = "Greys"
        self.color_palette_options = [
            "Spectral",
            "RdBu",
            "PiYG",
            "PRGn",
            "RdYlBu",
            "RdGy",
            "PuOr",
            "Greens",
            "Blues",
            "Purples",
            "Oranges",
            "Reds",
            "Greys",
            "YlOrBr",
            "YlGnBu",
            "YlGn",
            "Plasma",
            "Magma",
            "Inferno",
            "Viridis",
            "Cividis",
            "Greys256",
            "Inferno256",
            "Magma256",
            "Plasma256",
            "Viridis256",
            "Cividis256",
            "Turbo256",
        ]

        self.pkb_columns = [
            "n",
            "l",
            "nu",
            "max_nu18_nu84",
            "H",
            "max_H18_H84",
            "w",
            "max_w18_w84",
            "i",
            "max_i18_i84",
            "a1",
            "max_a1_18_a1_84",
            "alpha",
            "max_alpha_18_alpha_84",
            "Beta0",
            "max_Beta0_18_Beta0_84",
            "a3",
            "max_a3_18_a3_84",
        ]
        self.pkb_columns_extended = [
            "n",
            "l",
            "nu",
            "nu18",
            "nu84",
            "H",
            "H18",
            "H84",
            "w",
            "w18",
            "w84",
            "i",
            "i18",
            "i84",
            "a1",
            "a1_18",
            "a1_84",
            "alpha",
            "alpha18",
            "alpha84",
            "Beta0",
            "Beta0_18",
            "Beta0_84",
            "a3",
            "a3_18",
            "a3_84",
        ]

        # New buttons catalog
        self.button_type = "primary"
        self.selected_filename_text = None
        self.open_folder_button = None
        self.open_file_button = None
        self.open_catalog_button = None

        # Settings tab / data source configuration
        self.settings_layout = None
        self.settings_file_type_select = None
        self.settings_frequency_column_text = None
        self.settings_power_column_text = None
        self.settings_fits_hdu_text = None
        self.settings_text_delimiter_select = None
        self.settings_interface_mode_select = None
        self.settings_dialog_engine_select = None
        self.settings_manual_psd_path = None
        self.settings_manual_pkb_path = None
        self.settings_manual_bkg_path = None
        self.settings_select_psd_file_button = None
        self.settings_select_pkb_file_button = None
        self.settings_select_bkg_file_button = None
        self.settings_apply_psd_path_button = None
        self.settings_apply_pkb_path_button = None
        self.settings_apply_bkg_path_button = None
        self.settings_apply_source_config_button = None
        self.settings_source_info = None

        self.source_file_type = "fits-image"
        self.source_frequency_column = "0"
        self.source_power_column = "1"
        self.source_fits_hdu = "0"
        self.source_text_delimiter = "auto"
        self.source_input_frequency_unit = "Hz"
        self.source_target_frequency_unit = "uHz"
        self.interface_mode = "desktop"
        self.dialog_engine = "tkinter"

        self.save_as_table_2_button = None
        self.load_from_specific_file_table_2_button = None
        self.load_bkg_param_from_file_button = None

        self.inverted_line_initial_y_text = None
        self.inverted_line_length_text = None
        self.inverted_line_scale_text = None
        self.inverted_line_xvalue_text = None
        self.inverted_line_update_button = None
        self.Message = None
        self.message_banner = None
        self.selected_filename_background_text = None
        self.selected_filename_pkb_text = None
        self.selected_filename_fits_text = None
        self.calculate_synthetic_psd_button = None
        self.tb_plot = None
        self.table_plot = None
        self.select_grid_menu = None
        self.show_plot = None
        self.select_x_axis_type = None
        self.select_y_axis_type = None
        self.logo_div = None

        from bokeh.models import ColumnDataSource

        self.input_tb = ColumnDataSource(data=dict(frequency=[], power=[]))
        self.fig_home_psd = None
        self.get_data_button = None
