import { SystemSlider, SystemDropdown, SystemToggle } from "../widgets/common.slint";
import { Palette, HorizontalBox , VerticalBox, ScrollView, Slider, Button, Switch, ComboBox, GroupBox} from "std-widgets.slint";

export struct AvailableSystemProperties {
    charge_control_end_threshold: bool,
    panel_od: bool,
    boot_sound: bool,
    mini_led_mode: bool,
    disable_nvidia_powerd_on_battery: bool,
    ac_command: bool,
    bat_command: bool,
    throttle_thermal_policy: bool,
    ppt_pl1_spl: bool,
    ppt_pl2_sppt: bool,
    ppt_fppt: bool,
    ppt_apu_sppt: bool,
    ppt_platform_sppt: bool,
    nv_dynamic_boost: bool,
    nv_temp_target: bool,
}

export global SystemPageData {
    in-out property <float> charge_control_end_threshold: 30;
    callback set_charge_control_end_threshold(/* charge limit */ int);
    in-out property <int> throttle_thermal_policy: 0;
    in-out property <[string]> throttle_policy_choices: [@tr("Balanced"), @tr("Performance"), @tr("Quiet")];
    callback set_throttle_thermal_policy(int);
    in-out property <[string]> energy_performance_choices: [
        @tr("Default"),
        @tr("Performance"),
        @tr("BalancePerformance"),
        @tr("BalancePower"),
        @tr("Power")
    ];
    in-out property <int> throttle_balanced_epp: 0;
    callback set_throttle_balanced_epp(int);
    in-out property <int> throttle_performance_epp: 0;
    callback set_throttle_performance_epp(int);
    in-out property <int> throttle_quiet_epp: 0;
    callback set_throttle_quiet_epp(int);
    // if the EPP should change with throttle
    in-out property <bool> throttle_policy_linked_epp: true;
    callback set_throttle_policy_linked_epp(bool);
    in-out property <int> throttle_policy_on_ac: 0;
    callback set_throttle_policy_on_ac(int);
    in-out property <bool> change_throttle_policy_on_ac: true;
    callback set_change_throttle_policy_on_ac(bool);
    in-out property <int> throttle_policy_on_battery: 0;
    callback set_throttle_policy_on_battery(int);
    in-out property <bool> change_throttle_policy_on_battery: true;
    callback set_change_throttle_policy_on_battery(bool);
    in-out property <bool> panel_od;
    callback set_panel_od(bool);
    in-out property <bool> boot_sound;
    callback set_boot_sound(bool);
    in-out property <bool> mini_led_mode;
    callback set_mini_led_mode(bool);
    in-out property <float> ppt_pl1_spl: 5;
    callback set_ppt_pl1_spl(int);
    in-out property <float> ppt_pl2_sppt: 5;
    callback set_ppt_pl2_sppt(int);
    in-out property <float> ppt_fppt: 5;
    callback set_ppt_fppt(int);
    in-out property <float> ppt_apu_sppt: 5;
    callback set_ppt_apu_sppt(int);
    in-out property <float> ppt_platform_sppt: 5;
    callback set_ppt_platform_sppt(int);
    in-out property <float> nv_dynamic_boost: 5;
    callback set_nv_dynamic_boost(int);
    in-out property <float> nv_temp_target: 75;
    callback set_nv_temp_target(int);
    in-out property <AvailableSystemProperties> available: {
        charge_control_end_threshold: true,
        panel_od: true,
        boot_sound: true,
        mini_led_mode: true,
        disable_nvidia_powerd_on_battery: true,
        ac_command: true,
        bat_command: true,
        throttle_thermal_policy: true,
        ppt_pl1_spl: true,
        ppt_pl2_sppt: true,
        ppt_fppt: true,
        ppt_apu_sppt: true,
        ppt_platform_sppt: true,
        nv_dynamic_boost: true,
        nv_temp_target: true,
    };
}

export component PageSystem inherits Rectangle {
    property <bool> show_fade_cover: false;
    property <bool> show_throttle_advanced: false;
    clip: true;
    padding: 8px;
    ScrollView {
        VerticalLayout {
            padding: 10px;
            spacing: 10px;
            Rectangle {
                background: Palette.alternate-background;
                border-color: Palette.accent-background;
                border-width: 3px;
                border-radius: 10px;
                height: 40px;
                Text {
                    font-size: 18px;
                    color: Palette.control-foreground;
                    horizontal-alignment: TextHorizontalAlignment.center;
                    text: @tr("Base system settings");
                }
            }

            if SystemPageData.available.charge-control-end-threshold: SystemSlider {
                text: @tr("Charge limit");
                minimum: 20;
                maximum: 100;
                value <=> SystemPageData.charge_control_end_threshold;
                released => {
                    SystemPageData.set_charge_control_end_threshold(Math.round(SystemPageData.charge_control_end_threshold))
                }
            }

            if SystemPageData.available.throttle-thermal-policy: HorizontalLayout {
                spacing: 10px;
                SystemDropdown {
                    text: @tr("Throttle Policy");
                    current_index <=> SystemPageData.throttle_thermal_policy;
                    current_value: SystemPageData.throttle_policy_choices[SystemPageData.throttle_thermal_policy];
                    model <=> SystemPageData.throttle_policy_choices;
                    selected => {
                        SystemPageData.set_throttle_thermal_policy(SystemPageData.throttle_thermal_policy)
                    }
                }

                Button {
                    text: @tr("Advanced");
                    clicked => {
                        root.show_fade_cover = true;
                        root.show_throttle_advanced = true;
                    }
                }
            }

            HorizontalBox {
                padding: 0px;
                spacing: 10px;
                if SystemPageData.available.panel-od: SystemToggle {
                    text: @tr("Panel Overdrive");
                    checked <=> SystemPageData.panel_od;
                    toggled => {
                        SystemPageData.set_panel_od(SystemPageData.panel_od)
                    }
                }

                if SystemPageData.available.mini-led-mode: SystemToggle {
                    text: @tr("MiniLED Mode");
                    checked <=> SystemPageData.mini_led_mode;
                    toggled => {
                        SystemPageData.set_mini_led_mode(SystemPageData.mini_led_mode)
                    }
                }

                if SystemPageData.available.boot-sound: SystemToggle {
                    text: @tr("POST boot sound");
                    checked <=> SystemPageData.boot_sound;
                    toggled => {
                        SystemPageData.set_boot_sound(SystemPageData.boot_sound)
                    }
                }
            }

            Rectangle {
                background: Palette.alternate-background;
                border-color: Palette.accent-background;
                border-width: 3px;
                border-radius: 10px;
                height: 40px;
                Text {
                    font-size: 18px;
                    color: Palette.control-foreground;
                    horizontal-alignment: TextHorizontalAlignment.center;
                    text: @tr("System performance settings");
                }
            }

            if SystemPageData.available.ppt-pl1-spl: SystemSlider {
                text: @tr("ppt_pl1_spl" => "ppt_pl1_spl");
                minimum: 5;
                maximum: 250;
                value <=> SystemPageData.ppt_pl1_spl;
                released => {
                    SystemPageData.set_ppt_pl1_spl(Math.round(SystemPageData.ppt_pl1_spl))
                }
            }

            if SystemPageData.available.ppt-pl2-sppt: SystemSlider {
                text: @tr("ppt_pl2_sppt" => "ppt_pl2_sppt");
                minimum: 5;
                maximum: 250;
                value <=> SystemPageData.ppt_pl2_sppt;
                released => {
                    SystemPageData.set_ppt_pl2_sppt(Math.round(SystemPageData.ppt_pl2_sppt))
                }
            }

            if SystemPageData.available.ppt-fppt: SystemSlider {
                text: @tr("ppt_fppt" => "ppt_fppt");
                minimum: 5;
                maximum: 250;
                value <=> SystemPageData.ppt_fppt;
                released => {
                    SystemPageData.set_ppt_fppt(Math.round(SystemPageData.ppt_fppt))
                }
            }

            if SystemPageData.available.ppt-apu-sppt: SystemSlider {
                text: @tr("ppt_apu_sppt" => "ppt_apu_sppt");
                minimum: 5;
                maximum: 130;
                value <=> SystemPageData.ppt_apu_sppt;
                released => {
                    SystemPageData.set_ppt_apu_sppt(Math.round(SystemPageData.ppt_apu_sppt))
                }
            }

            if SystemPageData.available.ppt-platform-sppt: SystemSlider {
                text: @tr("ppt_platform_sppt" => "ppt_platform_sppt");
                maximum: 130;
                minimum: 5;
                value <=> SystemPageData.ppt_platform_sppt;
                released => {
                    SystemPageData.set_ppt_platform_sppt(Math.round(SystemPageData.ppt_platform_sppt))
                }
            }

            if SystemPageData.available.nv-dynamic-boost: SystemSlider {
                text: @tr("nv_dynamic_boost" => "nv_dynamic_boost");
                minimum: 5;
                maximum: 25;
                value <=> SystemPageData.nv_dynamic_boost;
                released => {
                    SystemPageData.set_nv_dynamic_boost(Math.round(SystemPageData.nv_dynamic_boost))
                }
            }

            if SystemPageData.available.nv-temp-target: SystemSlider {
                text: @tr("nv_temp_target" => "nv_temp_target");
                minimum: 75;
                maximum: 87;
                value <=> SystemPageData.nv_temp_target;
                released => {
                    SystemPageData.set_nv_temp_target(Math.round(SystemPageData.nv_temp_target))
                }
            }
        }
    }

    if root.show_fade_cover: Rectangle {
        width: 100%;
        height: 100%;
        background: Palette.background;
        opacity: 0.9;
        TouchArea {
            height: 100%;
            width: 100%;
            clicked => {
                // toolbar-dropdown.close();
                if (root.show_throttle_advanced) {
                    root.show_throttle_advanced = false;
                }
                root.show_fade_cover = false;
            }
        }
    }

    if root.show_throttle_advanced: Rectangle {
        width: 100%;
        height: 100%;
        opacity: 1;
        ScrollView {
            VerticalLayout {
                padding: 50px;
                padding-top: 5px;
                padding-bottom: 100px;
                spacing: 10px;
                GroupBox {
                    VerticalBox {
                        spacing: 10px;
                        Text {
                            font-size: 18px;
                            horizontal-alignment: TextHorizontalAlignment.center;
                            vertical-alignment: TextVerticalAlignment.center;
                            text: @tr("Energy Performance Preference linked to Throttle Policy");
                        }

                        SystemToggle {
                            text: @tr("Change EPP based on Throttle Policy");
                            checked <=> SystemPageData.throttle_policy_linked_epp;
                            toggled => {
                                SystemPageData.set_throttle_policy_linked_epp(SystemPageData.throttle_policy_linked_epp)
                            }
                        }

                        SystemDropdown {
                            text: @tr("EPP for Balanced Policy");
                            current_index <=> SystemPageData.throttle_balanced_epp;
                            current_value: SystemPageData.energy_performance_choices[SystemPageData.throttle_balanced_epp];
                            model <=> SystemPageData.energy_performance_choices;
                            selected => {
                                SystemPageData.set_throttle_balanced_epp(SystemPageData.throttle_balanced_epp)
                            }
                        }

                        SystemDropdown {
                            text: @tr("EPP for Performance Policy");
                            current_index <=> SystemPageData.throttle_performance_epp;
                            current_value: SystemPageData.energy_performance_choices[SystemPageData.throttle_performance_epp];
                            model <=> SystemPageData.energy_performance_choices;
                            selected => {
                                SystemPageData.set_throttle_performance_epp(SystemPageData.throttle_performance_epp)
                            }
                        }

                        SystemDropdown {
                            text: @tr("EPP for Quiet Policy");
                            current_index <=> SystemPageData.throttle_quiet_epp;
                            current_value: SystemPageData.energy_performance_choices[SystemPageData.throttle_quiet_epp];
                            model <=> SystemPageData.energy_performance_choices;
                            selected => {
                                SystemPageData.set_throttle_quiet_epp(SystemPageData.throttle_quiet_epp)
                            }
                        }
                    }
                }

                GroupBox {
                    VerticalBox {
                        spacing: 10px;
                        Text {
                            font-size: 18px;
                            horizontal-alignment: TextHorizontalAlignment.center;
                            vertical-alignment: TextVerticalAlignment.center;
                            text: @tr("Throttle Policy for power state");
                        }

                        HorizontalLayout {
                            spacing: 10px;
                            SystemDropdown {
                                text: @tr("Throttle Policy on Battery");
                                current_index <=> SystemPageData.throttle_policy_on_battery;
                                current_value: SystemPageData.throttle_policy_choices[SystemPageData.throttle_policy_on_battery];
                                model <=> SystemPageData.throttle_policy_choices;
                                selected => {
                                    SystemPageData.set_throttle_policy_on_battery(SystemPageData.throttle_policy_on_battery)
                                }
                            }

                            SystemToggle {
                                text: @tr("Enabled");
                                checked <=> SystemPageData.change_throttle_policy_on_battery;
                                toggled => {
                                    SystemPageData.set_change_throttle_policy_on_battery(SystemPageData.change_throttle_policy_on_battery);
                                }
                            }
                        }

                        HorizontalLayout {
                            spacing: 10px;
                            SystemDropdown {
                                text: @tr("Throttle Policy on AC");
                                current_index <=> SystemPageData.throttle_policy_on_ac;
                                current_value: SystemPageData.throttle_policy_choices[SystemPageData.throttle_policy_on_ac];
                                model <=> SystemPageData.throttle_policy_choices;
                                selected => {
                                    SystemPageData.set_throttle_policy_on_ac(SystemPageData.throttle_policy_on_ac)
                                }
                            }

                            SystemToggle {
                                text: @tr("Enabled");
                                checked <=> SystemPageData.change_throttle_policy_on_ac;
                                toggled => {
                                    SystemPageData.set_change_throttle_policy_on_ac(SystemPageData.change_throttle_policy_on_ac);
                                }
                            }
                        }
                    }
                }
            }
        }

        Button {
            x: root.width - self.width - 6px;
            y: 6px;
            text: "X";
            height: 40px;
            clicked => {
                root.show_throttle_advanced = false;
                root.show_fade_cover = false;
            }
        }
    }
}
