# Copyright 2015 Jake Ross
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ===============================================================================

# ============= enthought library imports =======================
from traitsui.api import UItem, Item, HGroup, VGroup, Group, EnumEditor, spring, View

from pychron.core.pychron_traits import BorderVGroup, BorderHGroup
from pychron.envisage.icon_button_editor import icon_button_editor
from pychron.options.options import (
    SubOptions,
    AppearanceSubOptions,
    GroupSubOptions,
    MainOptions,
    TitleSubOptions,
)
from pychron.processing.j_error_mixin import J_ERROR_GROUP
from pychron.pychron_constants import (
    MAIN,
    APPEARANCE,
    SCHAEN2020_3,
    SCHAEN2020_3youngest,
)


class DisplaySubOptions(TitleSubOptions):
    def traits_view(self):
        errbar_grp = VGroup(
            HGroup(
                Item("x_end_caps", label="X End Caps"),
                Item("y_end_caps", label="Y End Caps"),
            ),
            HGroup(
                Item("error_bar_line_width", label="Line Width"),
                Item("error_bar_nsigma", label="NSigma"),
            ),
            show_border=True,
            label="Error Bars",
        )

        an_grp = VGroup(
            Item("analysis_number_sorting", label="Analysis# Order"),
            Item(
                "global_analysis_number_sorting",
                label="Global Sort",
                tooltip="Applicable only when " "using Aux Grouping",
            ),
            HGroup(
                Item("include_group_legend", label="Include Group Legend"),
                UItem(
                    "group_legend_label_attribute", enabled_when="include_group_legend"
                ),
            ),
            HGroup(
                Item("use_cmap_analysis_number", label="Use Color Mapping"),
                UItem("cmap_analysis_number", enabled_when="use_cmap_analysis_number"),
            ),
            Item("use_latest_overlay"),
            show_border=True,
            label="Analysis #",
        )

        label_grp = VGroup(
            HGroup(
                Item("label_box"),
                Item(
                    "analysis_label_display",
                    label="Label Format",
                    width=100,
                    style="readonly",
                ),
                spring,
                icon_button_editor(
                    "edit_label_format_button", "cog", tooltip="Open Label maker"
                ),
            ),
            VGroup(
                Item(
                    "label_all_peaks",
                    label="Label Peaks",
                    tooltip="Label each peak with its calculated age",
                ),
                HGroup(
                    Item("peak_label_bgcolor_enabled", label="Background"),
                    UItem(
                        "peak_label_bgcolor", enabled_when="peak_label_bgcolor_enabled"
                    ),
                ),
                HGroup(
                    Item(
                        "peak_label_border",
                        label="Border Width",
                        tooltip="Border width in pixels, user 0 to disable",
                    ),
                    Item("peak_label_border_color", label="Border"),
                    enabled_when="peak_label_border",
                ),
                Item("peak_label_sigfigs", label="SigFigs"),
                show_border=True,
                label="Peaks",
            ),
            show_border=True,
            label="Label",
        )

        inset_grp = VGroup(
            HGroup(
                Item("display_inset", label="Use"),
                Item("inset_location", label="Location"),
                Item("inset_width", label="Width"),
                Item("inset_height", label="Height"),
            ),
            show_border=True,
            label="Inset",
        )

        mean_label = HGroup(
            Item(
                "mean_label_display",
                label="Mean Label Format",
                width=100,
                style="readonly",
            ),
            spring,
            icon_button_editor(
                "edit_mean_format_button", "cog", tooltip="Open Mean Label maker"
            ),
        )

        submean = HGroup(
            VGroup(Item("display_group_marker", label="Group Marker")),
            VGroup(
                Item(
                    "display_mean",
                    label="Value",
                ),
                Item(
                    "display_percent_error",
                    label="%Error",
                ),
            ),
            VGroup(
                Item(
                    "display_mean_mswd",
                    label="MSWD",
                ),
                Item("display_mean_n", label="N"),
                Item("display_mswd_pvalue", label="P-Value"),
            ),
            VGroup(
                Item("mean_sig_figs", label="Mean SigFigs"),
                Item("mswd_sig_figs", label="MSWD SigFigs"),
            ),
            enabled_when="display_mean_indicator",
        )

        mean_grp = VGroup(
            Item("display_mean_indicator", label="Indicator"),
            submean,
            mean_label,
            show_border=True,
            label="Mean",
        )

        info_grp = HGroup(
            Item("show_info", label="Show"),
            Item("show_mean_info", label="Mean", enabled_when="show_info"),
            Item("show_error_type_info", label="Error Type", enabled_when="show_info"),
            show_border=True,
            label="Info",
        )

        display_grp = VGroup(
            mean_grp,
            an_grp,
            inset_grp,
            self._get_title_group(),
            label_grp,
            info_grp,
            errbar_grp,
            scrollable=True,
            show_border=True,
            label="Display",
        )

        return self._make_view(display_grp)


class CalculationSubOptions(SubOptions):
    def traits_view(self):
        calcgrp = BorderVGroup(
            Item(
                "probability_curve_kind", width=-150, label="Probability Curve Method"
            ),
            Item("mean_calculation_kind", width=-150, label="Mean Calculation Method"),
            BorderVGroup(
                Item("shapiro_wilk_alpha", label="Shapiro-Wilk alpha"),
                HGroup(
                    Item("skew_min", label="Skew Min."),
                    Item("skew_max", label="Skew Max"),
                ),
                visible_when='mean_calculation_kind =="{}" '
                'or mean_calculation_kind=="{}"'.format(
                    SCHAEN2020_3, SCHAEN2020_3youngest
                ),
                label="Normality Options",
            ),
            Item("error_calc_method", width=-150, label="Error Calculation Method"),
            Item("nsigma", label="Age Error NSigma"),
            BorderVGroup(