/*
 * CoolerControl - monitor and control your cooling and other devices
 * Copyright (c) 2021-2025  Guy Boldon, Eren Simsek and contributors
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

use std::collections::HashMap;

use crate::device::{
    ChannelExtensionNames, ChannelInfo, DeviceInfo, DriverInfo, DriverType, LightingMode,
    SpeedOptions,
};
use crate::repositories::liquidctl::base_driver::BaseDriver;
use crate::repositories::liquidctl::liqctld_client::DeviceResponse;
use crate::repositories::liquidctl::supported_devices::device_support::{ColorMode, DeviceSupport};

#[derive(Debug)]
pub struct HydroProSupport;
// asetek_pro.py

impl HydroProSupport {
    pub fn new() -> Self {
        Self {}
    }
}

impl DeviceSupport for HydroProSupport {
    fn supported_driver(&self) -> BaseDriver {
        BaseDriver::HydroPro
    }

    fn extract_info(&self, device_response: &DeviceResponse) -> DeviceInfo {
        let mut channels = HashMap::new();
        channels.insert(
            "pump".to_string(),
            ChannelInfo {
                speed_options: Some(SpeedOptions {
                    min_duty: 20,
                    max_duty: 100,
                    fixed_enabled: true,
                    extension: None,
                }),
                ..Default::default()
            },
        );
        for channel_name in &device_response.properties.speed_channels {
            channels.insert(
                channel_name.to_owned(),
                ChannelInfo {
                    speed_options: Some(SpeedOptions {
                        min_duty: 0,
                        max_duty: 100,
                        fixed_enabled: true,
                        extension: Some(ChannelExtensionNames::AutoHWCurve),
                    }),
                    ..Default::default()
                },
            );
        }
        let lighting_modes = self.get_color_channel_modes(None);
        channels.insert(
            "logo".to_string(),
            ChannelInfo {
                lighting_modes,
                ..Default::default()
            },
        );
        let lighting_speeds = vec![
            "slower".to_string(),
            "normal".to_string(),
            "faster".to_string(),
        ];
        DeviceInfo {
            channels,
            lighting_speeds,
            temp_min: 20,
            temp_max: 60,
            profile_max_length: 7,
            profile_min_length: 2,
            driver_info: DriverInfo {
                drv_type: DriverType::Liquidctl,
                name: Some(self.supported_driver().to_string()),
                version: device_response.liquidctl_version.clone(),
                locations: self.collect_driver_locations(device_response),
            },
            ..Default::default()
        }
    }

    fn get_color_channel_modes(&self, _channel_name: Option<&str>) -> Vec<LightingMode> {
        let color_modes: Vec<ColorMode> = vec![
            ColorMode::new("fixed", 1, 1, false, false),
            ColorMode::new("blinking", 1, 4, true, false),
            ColorMode::new("pulse", 1, 4, true, false),
            ColorMode::new("shift", 2, 4, true, false),
            ColorMode::new("alert", 3, 3, false, false),
        ];
        self.convert_to_channel_lighting_modes(color_modes)
    }
}
