use bq34z100::{Bq34Z100Error, Bq34z100g1, Bq34z100g1Driver};
//mod config;
use chrono_tz::Europe::Berlin;
use embedded_svc::wifi::{
    AccessPointConfiguration, AccessPointInfo, AuthMethod, ClientConfiguration, Configuration,
};
use esp_idf_hal::i2c::{I2cConfig, I2cDriver, I2cError};
use esp_idf_hal::units::FromValueType;
use esp_idf_svc::eventloop::EspSystemEventLoop;
use esp_idf_svc::ipv4::IpInfo;
use esp_idf_svc::mqtt::client::QoS::AtLeastOnce;
use esp_idf_svc::mqtt::client::QoS::ExactlyOnce;
use esp_idf_svc::mqtt::client::{EspMqttClient, LwtConfiguration, MqttClientConfiguration};
use esp_idf_svc::nvs::EspDefaultNvsPartition;
use esp_idf_svc::wifi::config::{ScanConfig, ScanType};
use esp_idf_svc::wifi::EspWifi;
use measurements::Temperature;
use plant_ctrl2::sipo::ShiftRegister40;

use anyhow::anyhow;
use anyhow::{bail, Ok, Result};
use std::ffi::CString;
use std::fs::File;
use std::path::Path;

use chrono::{DateTime, NaiveDateTime, Utc};
use ds18b20::Ds18b20;
use std::result::Result::Ok as OkStd;
use std::str::FromStr;
use std::sync::atomic::AtomicBool;
use std::sync::{Arc, Mutex};
use std::time::Duration;

use embedded_hal::digital::OutputPin;
use esp_idf_hal::adc::{attenuation, AdcChannelDriver, AdcDriver};
use esp_idf_hal::delay::Delay;
use esp_idf_hal::gpio::{AnyInputPin, Gpio39, Gpio4, InputOutput, Level, PinDriver, Pull};
use esp_idf_hal::pcnt::{
    PcntChannel, PcntChannelConfig, PcntControlMode, PcntCountMode, PcntDriver, PinIndex,
};
use esp_idf_hal::prelude::Peripherals;
use esp_idf_hal::reset::ResetReason;
use esp_idf_svc::sntp::{self, SyncStatus};
use esp_idf_svc::systime::EspSystemTime;
use esp_idf_sys::{esp, gpio_hold_dis, gpio_hold_en, vTaskDelay, EspError};
use one_wire_bus::OneWire;

use crate::config::{self, Config, WifiConfig};
use crate::STAY_ALIVE;

pub const PLANT_COUNT: usize = 8;
const PINS_PER_PLANT: usize = 5;
const PLANT_PUMP_OFFSET: usize = 0;
const PLANT_FAULT_OFFSET: usize = 1;
const PLANT_MOIST_PUMP_OFFSET: usize = 2;
const PLANT_MOIST_A_OFFSET: usize = 3;
const PLANT_MOIST_B_OFFSET: usize = 4;

const SPIFFS_PARTITION_NAME: &str = "storage";
const WIFI_CONFIG_FILE: &str = "/spiffs/wifi.cfg";
const CONFIG_FILE: &str = "/spiffs/config.cfg";

const TANK_MULTI_SAMPLE: usize = 11;

#[link_section = ".rtc.data"]
static mut LAST_WATERING_TIMESTAMP: [i64; PLANT_COUNT] = [0; PLANT_COUNT];
#[link_section = ".rtc.data"]
static mut CONSECUTIVE_WATERING_PLANT: [u32; PLANT_COUNT] = [0; PLANT_COUNT];
#[link_section = ".rtc.data"]
static mut LOW_VOLTAGE_DETECTED: bool = false;

pub struct FileSystemSizeInfo {
    pub total_size: usize,
    pub used_size: usize,
    pub free_size: usize,
}

#[derive(strum::Display)]
pub enum ClearConfigType {
    WifiConfig,
    Config,
    None,
}

#[derive(Debug)]
pub enum Sensor {
    A,
    B,
    PUMP,
}
pub trait PlantCtrlBoardInteraction {
    fn time(&mut self) -> Result<chrono::DateTime<Utc>>;
    fn wifi(
        &mut self,
        ssid: heapless::String<32>,
        password: Option<heapless::String<64>>,
        max_wait: u32,
    ) -> Result<IpInfo>;
    fn sntp(&mut self, max_wait: u32) -> Result<chrono::DateTime<Utc>>;
    fn mount_file_system(&mut self) -> Result<()>;
    fn file_system_size(&mut self) -> Result<FileSystemSizeInfo>;

    fn state_charge_percent(&mut self) -> Result<u8>;
    fn remaining_milli_ampere_hour(&mut self) -> Result<u16>;
    fn max_milli_ampere_hour(&mut self) -> Result<u16>;
    fn design_milli_ampere_hour(&mut self) -> Result<u16>;
    fn voltage_milli_volt(&mut self) -> Result<u16>;
    fn average_current_milli_ampere(&mut self) -> Result<i16>;
    fn cycle_count(&mut self) -> Result<u16>;
    fn state_health_percent(&mut self) -> Result<u8>;

    fn general_fault(&mut self, enable: bool);

    fn is_day(&self) -> bool;
    fn water_temperature_c(&mut self) -> Result<f32>;
    fn tank_sensor_percent(&mut self) -> Result<u16>;

    fn set_low_voltage_in_cycle(&mut self);
    fn clear_low_voltage_in_cycle(&mut self);
    fn low_voltage_in_cycle(&mut self) -> bool;
    fn any_pump(&mut self, enabled: bool) -> Result<()>;

    //keep state during deepsleep
    fn light(&mut self, enable: bool) -> Result<()>;

    fn measure_moisture_hz(&self, plant: usize, sensor: Sensor) -> Result<i32>;
    fn pump(&self, plant: usize, enable: bool) -> Result<()>;
    fn last_pump_time(&self, plant: usize) -> chrono::DateTime<Utc>;
    fn store_last_pump_time(&mut self, plant: usize, time: chrono::DateTime<Utc>);
    fn store_consecutive_pump_count(&mut self, plant: usize, count: u32);
    fn consecutive_pump_count(&mut self, plant: usize) -> u32;

    //keep state during deepsleep
    fn fault(&self, plant: usize, enable: bool);

    //config
    fn is_config_reset(&mut self) -> bool;
    fn remove_configs(&mut self) -> Result<ClearConfigType>;
    fn get_config(&mut self) -> Result<config::Config>;
    fn set_config(&mut self, wifi: &Config) -> Result<()>;
    fn get_wifi(&mut self) -> Result<config::WifiConfig>;
    fn set_wifi(&mut self, wifi: &WifiConfig) -> Result<()>;
    fn wifi_ap(&mut self) -> Result<()>;
    fn wifi_scan(&mut self) -> Result<Vec<AccessPointInfo>>;
    fn test(&mut self) -> Result<()>;
    fn is_wifi_config_file_existant(&mut self) -> bool;
    fn mqtt(&mut self, config: &Config) -> Result<()>;
    fn mqtt_publish(&mut self, config: &Config, subtopic: &str, message: &[u8]) -> Result<()>;
}

pub trait CreatePlantHal<'a> {
    fn create() -> Result<Mutex<PlantCtrlBoard<'static>>>;
}

pub struct PlantHal {}

pub struct PlantCtrlBoard<'a> {
    shift_register: ShiftRegister40<
        PinDriver<'a, esp_idf_hal::gpio::Gpio21, InputOutput>,
        PinDriver<'a, esp_idf_hal::gpio::Gpio22, InputOutput>,
        PinDriver<'a, esp_idf_hal::gpio::Gpio19, InputOutput>,
    >,
    tank_driver: AdcDriver<'a, esp_idf_hal::adc::ADC1>,
    tank_channel: esp_idf_hal::adc::AdcChannelDriver<'a, { attenuation::DB_11 }, Gpio39>,
    solar_is_day: PinDriver<'a, esp_idf_hal::gpio::Gpio25, esp_idf_hal::gpio::Input>,
    boot_button: PinDriver<'a, esp_idf_hal::gpio::Gpio0, esp_idf_hal::gpio::Input>,
    signal_counter: PcntDriver<'a>,
    light: PinDriver<'a, esp_idf_hal::gpio::Gpio26, InputOutput>,
    main_pump: PinDriver<'a, esp_idf_hal::gpio::Gpio23, InputOutput>,
    tank_power: PinDriver<'a, esp_idf_hal::gpio::Gpio27, InputOutput>,
    general_fault: PinDriver<'a, esp_idf_hal::gpio::Gpio13, InputOutput>,
    pub wifi_driver: EspWifi<'a>,
    one_wire_bus: OneWire<PinDriver<'a, Gpio4, esp_idf_hal::gpio::InputOutput>>,
    mqtt_client: Option<EspMqttClient<'a>>,
    battery_driver: Bq34z100g1Driver<I2cDriver<'a>, Delay>,
}

impl PlantCtrlBoardInteraction for PlantCtrlBoard<'_> {
    fn is_day(&self) -> bool {
        self.solar_is_day.get_level().into()
    }

    fn water_temperature_c(&mut self) -> Result<f32> {
        let mut delay = Delay::new_default();

        self.one_wire_bus
            .reset(&mut delay)
            .map_err(|err| -> anyhow::Error { anyhow!("Missing attribute: {:?}", err) })?;
        let first = self.one_wire_bus.devices(false, &mut delay).next();
        if first.is_none() {
            bail!("Not found any one wire  Ds18b20");
        }
        let device_address = first
            .unwrap()
            .map_err(|err| -> anyhow::Error { anyhow!("Missing attribute: {:?}", err) })?;

        let water_temp_sensor = Ds18b20::new::<EspError>(device_address)
            .map_err(|err| -> anyhow::Error { anyhow!("Missing attribute: {:?}", err) })?;

        water_temp_sensor
            .start_temp_measurement(&mut self.one_wire_bus, &mut delay)
            .map_err(|err| -> anyhow::Error { anyhow!("Missing attribute: {:?}", err) })?;
        ds18b20::Resolution::Bits12.delay_for_measurement_time(&mut delay);
        let sensor_data = water_temp_sensor
            .read_data(&mut self.one_wire_bus, &mut delay)
            .map_err(|err| -> anyhow::Error { anyhow!("Missing attribute: {:?}", err) })?;
        if sensor_data.temperature == 85_f32 {
            bail!("Ds18b20 dummy temperature returned");
        }
        Ok(sensor_data.temperature / 10_f32)
    }

    fn tank_sensor_percent(&mut self) -> Result<u16> {
        let delay = Delay::new_default();
        self.tank_power.set_high()?;
        //let stabilize
        delay.delay_ms(100);
        unsafe {
            vTaskDelay(100);
        }

        let mut store = [0_u16; TANK_MULTI_SAMPLE];
        for multisample in 0..TANK_MULTI_SAMPLE {
            let value = self.tank_driver.read(&mut self.tank_channel)?;
            store[multisample] = value;
        }
        store.sort();
        let median = store[6] as f32 / 1000_f32;
        let config_open_voltage_mv = 3.0;
        if config_open_voltage_mv < median {
            self.tank_power.set_low()?;
            bail!(
                "Tank sensor missing, open loop voltage {} on tank sensor input {}",
                config_open_voltage_mv,
                median
            );
        }

        let r2 = median * 50.0 / (3.3 - median);
        let mut percent = r2 / 190_f32 * 100_f32;
        percent = percent.clamp(0.0, 100.0);

        let quantizised = quantize_to_next_5_percent(percent as f64) as u16;
        println!(
            "Tank sensor  raw {} percent {} quantized {}",
            median, percent, quantizised
        );
        return Ok(quantizised);
    }

    fn set_low_voltage_in_cycle(&mut self) {
        unsafe {
            LOW_VOLTAGE_DETECTED = true;
        }
    }

    fn clear_low_voltage_in_cycle(&mut self) {
        unsafe {
            LOW_VOLTAGE_DETECTED = false;
        }
    }

    fn light(&mut self, enable: bool) -> Result<()> {
        unsafe { gpio_hold_dis(self.light.pin()) };
        self.light.set_state(enable.into())?;
        unsafe { gpio_hold_en(self.light.pin()) };
        Ok(())
    }

    fn pump(&self, plant: usize, enable: bool) -> Result<()> {
        let index = plant * PINS_PER_PLANT + PLANT_PUMP_OFFSET;
        //currently infailable error, keep for future as result anyway
        self.shift_register.decompose()[index]
            .set_state(enable.into())
            .unwrap();
        Ok(())
    }

    fn last_pump_time(&self, plant: usize) -> chrono::DateTime<Utc> {
        let ts = unsafe { LAST_WATERING_TIMESTAMP }[plant];
        let timestamp = NaiveDateTime::from_timestamp_millis(ts).unwrap();
        DateTime::<Utc>::from_naive_utc_and_offset(timestamp, Utc)
    }

    fn store_last_pump_time(&mut self, plant: usize, time: chrono::DateTime<Utc>) {
        unsafe {
            LAST_WATERING_TIMESTAMP[plant] = time.timestamp_millis();
        }
    }

    fn store_consecutive_pump_count(&mut self, plant: usize, count: u32) {
        unsafe {
            CONSECUTIVE_WATERING_PLANT[plant] = count;
        }
    }

    fn consecutive_pump_count(&mut self, plant: usize) -> u32 {
        unsafe {
            return CONSECUTIVE_WATERING_PLANT[plant];
        }
    }

    fn fault(&self, plant: usize, enable: bool) {
        let index = plant * PINS_PER_PLANT + PLANT_FAULT_OFFSET;
        self.shift_register.decompose()[index]
            .set_state(enable.into())
            .unwrap()
    }

    fn low_voltage_in_cycle(&mut self) -> bool {
        unsafe {
            return LOW_VOLTAGE_DETECTED;
        }
    }

    fn any_pump(&mut self, enable: bool) -> Result<()> {
        {
            self.main_pump.set_state(enable.into()).unwrap();
            Ok(())
        }
    }

    fn time(&mut self) -> Result<chrono::DateTime<Utc>> {
        let time = EspSystemTime {}.now().as_millis();
        let smaller_time = time as i64;
        let local_time = NaiveDateTime::from_timestamp_millis(smaller_time)
            .ok_or(anyhow!("could not convert timestamp"))?;
        Ok(local_time.and_utc())
    }

    fn sntp(&mut self, max_wait_ms: u32) -> Result<chrono::DateTime<Utc>> {
        let sntp = sntp::EspSntp::new_default()?;
        let mut counter = 0;
        while sntp.get_sync_status() != SyncStatus::Completed {
            let delay = Delay::new_default();
            delay.delay_ms(100);
            counter += 100;
            if counter > max_wait_ms {
                bail!("Reached sntp timeout, aborting")
            }
        }

        self.time()
    }

    fn measure_moisture_hz(&self, plant: usize, sensor: Sensor) -> Result<i32> {
        self.signal_counter.counter_pause()?;
        self.signal_counter.counter_clear()?;
        //
        let offset = match sensor {
            Sensor::A => PLANT_MOIST_A_OFFSET,
            Sensor::B => PLANT_MOIST_B_OFFSET,
            Sensor::PUMP => PLANT_MOIST_PUMP_OFFSET,
        };
        let index = plant * PINS_PER_PLANT + offset;

        let delay = Delay::new_default();
        let measurement = 100;
        let factor = 1000 as f32 / measurement as f32;

        self.shift_register.decompose()[index].set_high().unwrap();
        //give some time to stabilize
        delay.delay_ms(10);
        self.signal_counter.counter_resume()?;
        delay.delay_ms(measurement);
        self.signal_counter.counter_pause()?;
        self.shift_register.decompose()[index].set_low().unwrap();
        let unscaled = self.signal_counter.get_counter_value()? as i32;
        let hz = (unscaled as f32 * factor) as i32;
        println!("Measuring {:?} @ {} with {}", sensor, plant, hz);
        Ok(hz)
    }

    fn general_fault(&mut self, enable: bool) {
        self.general_fault.set_state(enable.into()).unwrap();
    }

    fn wifi_ap(&mut self) -> Result<()> {
        let apconfig = AccessPointConfiguration {
            ssid: heapless::String::from_str("PlantCtrl").unwrap(),
            auth_method: AuthMethod::None,
            ssid_hidden: false,
            ..Default::default()
        };
        let clientconfig = ClientConfiguration::default();
        self.wifi_driver
            .set_configuration(&Configuration::Mixed(clientconfig, apconfig))?;
        self.wifi_driver.start()?;
        Ok(())
    }

    fn wifi(
        &mut self,
        ssid: heapless::String<32>,
        password: Option<heapless::String<64>>,
        max_wait: u32,
    ) -> Result<IpInfo> {
        match password {
            Some(pw) => {
                //TODO expect error due to invalid pw or similar! //call this during configuration and check if works, revert to config mode if not
                self.wifi_driver.set_configuration(&Configuration::Client(
                    ClientConfiguration {
                        ssid: ssid,
                        password: pw,
                        ..Default::default()
                    },
                ))?;
            }
            None => {
                self.wifi_driver
                    .set_configuration(&Configuration::Client(ClientConfiguration {
                        ssid: ssid,
                        auth_method: AuthMethod::None,
                        ..Default::default()
                    }))
                    .unwrap();
            }
        }

        self.wifi_driver.start()?;
        self.wifi_driver.connect()?;

        let delay = Delay::new_default();
        let mut counter = 0_u32;
        while !self.wifi_driver.is_connected()? {
            println!("Waiting for station connection");
            delay.delay_ms(250);
            counter += 250;
            if counter > max_wait {
                //ignore these errors, wifi will not be used this
                self.wifi_driver.disconnect().unwrap_or(());
                self.wifi_driver.stop().unwrap_or(());
                bail!("Did not manage wifi connection within timeout");
            }
        }
        println!("Should be connected now");

        while !self.wifi_driver.is_up().unwrap() {
            println!("Waiting for network being up");
            delay.delay_ms(250);
            counter += 250;
            if counter > max_wait {
                //ignore these errors, wifi will not be used this
                self.wifi_driver.disconnect().unwrap_or(());
                self.wifi_driver.stop().unwrap_or(());
                bail!("Did not manage wifi connection within timeout");
            }
        }
        //update freertos registers ;)
        let address = self.wifi_driver.sta_netif().get_ip_info().unwrap();
        println!("IP info: {:?}", address);
        Ok(address)
    }

    fn mount_file_system(&mut self) -> Result<()> {
        let base_path = CString::new("/spiffs")?;
        let storage = CString::new(SPIFFS_PARTITION_NAME)?;
        let conf = esp_idf_sys::esp_vfs_spiffs_conf_t {
            base_path: base_path.as_ptr(),
            partition_label: storage.as_ptr(),
            max_files: 2,
            format_if_mount_failed: true,
        };

        unsafe {
            esp_idf_sys::esp!(esp_idf_sys::esp_vfs_spiffs_register(&conf))?;
            Ok(())
        }
    }

    fn file_system_size(&mut self) -> Result<FileSystemSizeInfo> {
        let storage = CString::new(SPIFFS_PARTITION_NAME)?;
        let mut total_size = 0;
        let mut used_size = 0;
        unsafe {
            esp_idf_sys::esp!(esp_idf_sys::esp_spiffs_info(
                storage.as_ptr(),
                &mut total_size,
                &mut used_size
            ))?;
        }
        Ok(FileSystemSizeInfo {
            total_size,
            used_size,
            free_size: total_size - used_size,
        })
    }

    fn is_config_reset(&mut self) -> bool {
        self.boot_button.get_level() == Level::Low
    }

    fn remove_configs(&mut self) -> Result<ClearConfigType> {
        let config = Path::new(CONFIG_FILE);
        if config.exists() {
            println!("Removing config");
            std::fs::remove_file(config)?;
            return Ok(ClearConfigType::Config);
        }

        let wifi_config = Path::new(WIFI_CONFIG_FILE);
        if wifi_config.exists() {
            println!("Removing wifi config");
            std::fs::remove_file(wifi_config)?;
            return Ok(ClearConfigType::WifiConfig);
        }

        Ok(ClearConfigType::None)
    }

    fn get_wifi(&mut self) -> Result<config::WifiConfig> {
        let cfg = File::open(WIFI_CONFIG_FILE)?;
        let config: WifiConfig = serde_json::from_reader(cfg)?;
        Ok(config)
    }

    fn set_wifi(&mut self, wifi: &WifiConfig) -> Result<()> {
        let mut cfg = File::create(WIFI_CONFIG_FILE)?;
        serde_json::to_writer(&mut cfg, &wifi)?;
        println!("Wrote wifi config {}", wifi);
        Ok(())
    }

    fn get_config(&mut self) -> Result<config::Config> {
        let cfg = File::open(CONFIG_FILE)?;
        let mut config: Config = serde_json::from_reader(cfg)?;
        //remove duplicate end of topic
        if config.base_topic.ends_with("/") {
            config.base_topic.pop();
        }
        Ok(config)
    }

    fn set_config(&mut self, config: &Config) -> Result<()> {
        let mut cfg = File::create(CONFIG_FILE)?;
        serde_json::to_writer(&mut cfg, &config)?;
        println!("Wrote config config {:?}", config);
        Ok(())
    }

    fn wifi_scan(&mut self) -> Result<Vec<AccessPointInfo>> {
        //remove this parts
        for i in 1..11 {
            println!("Scanning channel {}", i);
            self.wifi_driver.start_scan(
                &ScanConfig {
                    scan_type: ScanType::Passive(Duration::from_secs(1)),
                    show_hidden: false,
                    channel: Some(i),
                    ..Default::default()
                },
                true,
            )?;
            let sr = self.wifi_driver.get_scan_result()?;
            for r in sr.iter() {
                println!("Found wifi {}", r.ssid);
            }
        }

        self.wifi_driver.start_scan(
            &ScanConfig {
                scan_type: ScanType::Passive(Duration::from_secs(1)),
                show_hidden: false,
                ..Default::default()
            },
            true,
        )?;
        Ok(self.wifi_driver.get_scan_result()?)
    }

    fn test(&mut self) -> Result<()> {
        self.general_fault(true);
        unsafe { vTaskDelay(100) };
        self.general_fault(false);
        unsafe { vTaskDelay(100) };
        self.any_pump(true)?;
        unsafe { vTaskDelay(500) };
        self.any_pump(false)?;
        unsafe { vTaskDelay(500) };
        self.light(true)?;
        unsafe { vTaskDelay(500) };
        self.light(false)?;
        unsafe { vTaskDelay(500) };
        for i in 0..8 {
            self.fault(i, true);
            unsafe { vTaskDelay(500) };
            self.fault(i, false);
            unsafe { vTaskDelay(500) };
        }
        for i in 0..8 {
            self.pump(i, true)?;
            unsafe { vTaskDelay(100) };
            self.pump(i, false)?;
            unsafe { vTaskDelay(100) };
        }
        for i in 0..8 {
            self.measure_moisture_hz(i, Sensor::A)?;
        }
        for i in 0..8 {
            self.measure_moisture_hz(i, Sensor::B)?;
        }
        for i in 0..8 {
            self.measure_moisture_hz(i, Sensor::PUMP)?;
        }

        Ok(())
    }

    fn is_wifi_config_file_existant(&mut self) -> bool {
        let config = Path::new(CONFIG_FILE);
        config.exists()
    }

    fn mqtt(&mut self, config: &Config) -> Result<()> {
        let last_will_topic = format!("{}/state", config.base_topic);

        let mqtt_client_config = MqttClientConfiguration {
            lwt: Some(LwtConfiguration {
                topic: &last_will_topic,
                payload: "lost".as_bytes(),
                qos: AtLeastOnce,
                retain: true,
            }),
            keep_alive_interval : Some(Duration::from_secs(60*60*2)),
            //room for improvement
            ..Default::default()
        };

        let round_trip_ok = Arc::new(AtomicBool::new(false));
        let round_trip_topic = format!("{}/internal/roundtrip", config.base_topic);
        let stay_alive_topic = format!("{}/stay_alive", config.base_topic);
        println!("Round trip topic is {}", round_trip_topic);
        println!("Stay alive topic is {}", stay_alive_topic);

        let stay_alive_topic_copy = stay_alive_topic.clone();
        let round_trip_topic_copy = round_trip_topic.clone();
        let round_trip_ok_copy = round_trip_ok.clone();
        let mut client =
            EspMqttClient::new_cb(&config.mqtt_url, &mqtt_client_config, move |event| {
                let payload = event.payload();
                match payload {
                    embedded_svc::mqtt::client::EventPayload::Received {
                        id: _,
                        topic,
                        data,
                        details: _,
                    } => {
                        let data = String::from_utf8_lossy(data);
                        if let Some(topic) = topic {
                            //todo use enums
                            if topic.eq(round_trip_topic_copy.as_str()) {
                                round_trip_ok_copy
                                    .store(true, std::sync::atomic::Ordering::Relaxed);
                            } else if topic.eq(stay_alive_topic_copy.as_str()) {
                                let value = data.eq_ignore_ascii_case("true")
                                    || data.eq_ignore_ascii_case("1");
                                println!("Received stay alive with value {}", value);
                                STAY_ALIVE.store(value, std::sync::atomic::Ordering::Relaxed);
                            } else {
                                println!("Unknown topic recieved {}", topic);
                            }
                        }
                    }
                    _ => {}
                }
            })?;
        //subscribe to roundtrip

        client.subscribe(round_trip_topic.as_str(), ExactlyOnce)?;
        client.subscribe(stay_alive_topic.as_str(), ExactlyOnce)?;
        //publish to roundtrip
        client.publish(
            round_trip_topic.as_str(),
            ExactlyOnce,
            false,
            "online_test".as_bytes(),
        )?;

        let wait_for_roundtrip = 0;
        while wait_for_roundtrip < 100 {
            match round_trip_ok.load(std::sync::atomic::Ordering::Relaxed) {
                true => {
                    println!("Round trip registered, proceeding");
                    self.mqtt_client = Some(client);
                    return Ok(());
                }
                false => {
                    unsafe { vTaskDelay(10) };
                }
            }
        }
        bail!("Mqtt did not complete roundtrip in time");
    }

    fn mqtt_publish(&mut self, config: &Config, subtopic: &str, message: &[u8]) -> Result<()> {
        if !subtopic.starts_with("/") {
            println!("Subtopic without / at start {}", subtopic);
            bail!("Subtopic without / at start {}", subtopic);
        }
        if subtopic.len() > 192 {
            println!("Subtopic exceeds 192 chars {}", subtopic);
            bail!("Subtopic exceeds 192 chars {}", subtopic);
        }
        if self.mqtt_client.is_none() {
            println!("Not connected to mqtt");
            bail!("Not connected to mqtt");
        }
        let client = self.mqtt_client.as_mut().unwrap();

        let mut full_topic: heapless::String<256> = heapless::String::new();
        if full_topic.push_str(&config.base_topic).is_err() {
            println!("Some error assembling full_topic 1");
            bail!("Some error assembling full_topic 1")
        };
        if full_topic.push_str(subtopic).is_err() {
            println!("Some error assembling full_topic 2");
            bail!("Some error assembling full_topic 2")
        };
        client.publish(
            &full_topic,
            embedded_svc::mqtt::client::QoS::ExactlyOnce,
            true,
            message,
        )?;

        return Ok(());
    }

    fn state_charge_percent(&mut self) -> Result<u8> {
        match self.battery_driver.state_of_charge() {
            OkStd(r) => Ok(r),
            Err(err) => bail!("Error reading SoC {:?}", err),
        }
    }

    fn remaining_milli_ampere_hour(&mut self) -> Result<u16> {
        match self.battery_driver.remaining_capacity() {
            OkStd(r) => Ok(r),
            Err(err) => bail!("Error reading Remaining Capacity {:?}", err),
        }
    }

    fn max_milli_ampere_hour(&mut self) -> Result<u16> {
        match self.battery_driver.full_charge_capacity() {
            OkStd(r) => Ok(r),
            Err(err) => bail!("Error reading Full Charge Capacity {:?}", err),
        }
    }

    fn design_milli_ampere_hour(&mut self) -> Result<u16> {
        match self.battery_driver.design_capacity() {
            OkStd(r) => Ok(r),
            Err(err) => bail!("Error reading Design Capacity {:?}", err),
        }
    }

    fn voltage_milli_volt(&mut self) -> Result<u16> {
        return match self.battery_driver.voltage() {
            OkStd(r) => Ok(r),
            Err(err) => bail!("Error reading voltage {:?}", err),
        };
    }

    fn average_current_milli_ampere(&mut self) -> Result<i16> {
        match self.battery_driver.average_current() {
            OkStd(r) => Ok(r),
            Err(err) => bail!("Error reading Average Current {:?}", err),
        }
    }

    fn cycle_count(&mut self) -> Result<u16> {
        match self.battery_driver.cycle_count() {
            OkStd(r) => Ok(r),
            Err(err) => bail!("Error reading Cycle Count {:?}", err),
        }
    }

    fn state_health_percent(&mut self) -> Result<u8> {
        match self.battery_driver.state_of_health() {
            OkStd(r) => Ok(r as u8),
            Err(err) => bail!("Error reading State of Health {:?}", err),
        }
    }
}

fn print_battery(
    battery_driver: &mut Bq34z100g1Driver<I2cDriver, Delay>,
) -> Result<(), Bq34Z100Error<I2cError>> {
    let fwversion = battery_driver.fw_version().unwrap_or_else(|e| {
        println!("Firmeware {:?}", e);
        0
    });
    println!("fw version is {}", fwversion);

    let design_capacity = battery_driver.design_capacity().unwrap_or_else(|e| {
        println!("Design capacity {:?}", e);
        0
    });
    println!("Design Capacity {}", design_capacity);
    if design_capacity == 1000 {
        println!("Still stock configuring battery, readouts are likely to be wrong!");
    }

    let flags = battery_driver.get_flags_decoded()?;
    println!("Flags {:?}", flags);

    let chem_id = battery_driver.chem_id().unwrap_or_else(|e| {
        println!("Chemid {:?}", e);
        0
    });

    let bat_temp = battery_driver.internal_temperature().unwrap_or_else(|e| {
        println!("Bat Temp {:?}", e);
        0
    });
    let temp_c = Temperature::from_kelvin(bat_temp as f64 / 10_f64).as_celsius();
    let voltage = battery_driver.voltage().unwrap_or_else(|e| {
        println!("Bat volt {:?}", e);
        0
    });
    let current = battery_driver.current().unwrap_or_else(|e| {
        println!("Bat current {:?}", e);
        0
    });
    let state = battery_driver.state_of_charge().unwrap_or_else(|e| {
        println!("Bat Soc {:?}", e);
        0
    });
    let charge_voltage = battery_driver.charge_voltage().unwrap_or_else(|e| {
        println!("Bat Charge Volt {:?}", e);
        0
    });
    let charge_current = battery_driver.charge_current().unwrap_or_else(|e| {
        println!("Bat Charge Current {:?}", e);
        0
    });
    println!("ChemId: {} Current voltage {} and current {} with charge {}% and temp {} CVolt: {} CCur {}", chem_id, voltage, current, state, temp_c, charge_voltage, charge_current);
    let _ = battery_driver.unsealed();
    let _ = battery_driver.it_enable();
    return Result::Ok(());
}

impl CreatePlantHal<'_> for PlantHal {
    fn create() -> Result<Mutex<PlantCtrlBoard<'static>>> {
        let peripherals = Peripherals::take()?;

        let i2c = peripherals.i2c1;
        let config = I2cConfig::new()
            .scl_enable_pullup(false)
            .sda_enable_pullup(false)
            .baudrate(10_u32.kHz().into());
        let scl = peripherals.pins.gpio16;
        let sda = peripherals.pins.gpio17;

        let driver = I2cDriver::new(i2c, sda, scl, &config).unwrap();

        let i2c_port = driver.port();
        esp!(unsafe { esp_idf_sys::i2c_set_timeout(i2c_port, 1048000) }).unwrap();

        let mut battery_driver: Bq34z100g1Driver<I2cDriver, Delay> = Bq34z100g1Driver {
            i2c: driver,
            delay: Delay::new_default(),
            flash_block_data: [0; 32],
        };

        let mut clock = PinDriver::input_output(peripherals.pins.gpio21)?;
        clock.set_pull(Pull::Floating).unwrap();
        let mut latch = PinDriver::input_output(peripherals.pins.gpio22)?;
        latch.set_pull(Pull::Floating).unwrap();
        let mut data = PinDriver::input_output(peripherals.pins.gpio19)?;
        data.set_pull(Pull::Floating).unwrap();
        let shift_register = ShiftRegister40::new(clock.into(), latch.into(), data.into());
        for mut pin in shift_register.decompose() {
            pin.set_low().unwrap();
        }

        let mut one_wire_pin = PinDriver::input_output_od(peripherals.pins.gpio4)?;
        one_wire_pin.set_pull(Pull::Floating).unwrap();
        //TODO make to none if not possible to init

        //init,reset rtc memory depending on cause
        let reasons = ResetReason::get();
        let reset_store = match reasons {
            ResetReason::Software => false,
            ResetReason::ExternalPin => false,
            ResetReason::Watchdog => true,
            ResetReason::Sdio => true,
            ResetReason::Panic => true,
            ResetReason::InterruptWatchdog => true,
            ResetReason::PowerOn => true,
            ResetReason::Unknown => true,
            ResetReason::Brownout => true,
            ResetReason::TaskWatchdog => true,
            ResetReason::DeepSleep => false,
        };
        if reset_store {
            println!("Clear and reinit RTC store");
            unsafe {
                LAST_WATERING_TIMESTAMP = [0; PLANT_COUNT];
                CONSECUTIVE_WATERING_PLANT = [0; PLANT_COUNT];
                LOW_VOLTAGE_DETECTED = false;
            };
        } else {
            println!("Keeping RTC store");
            unsafe {
                println!(
                    "Current low voltage detection is {:?}",
                    LOW_VOLTAGE_DETECTED
                );
                for i in 0..PLANT_COUNT {
                    let smaller_time = LAST_WATERING_TIMESTAMP[i];
                    let local_time = NaiveDateTime::from_timestamp_millis(smaller_time)
                        .ok_or(anyhow!("could not convert timestamp"))?;
                    let utc_time = local_time.and_utc();
                    let europe_time = utc_time.with_timezone(&Berlin);

                    println!(
                        "LAST_WATERING_TIMESTAMP[{}] = {} as europe {}",
                        i, LAST_WATERING_TIMESTAMP[i], europe_time
                    );
                }
                for i in 0..PLANT_COUNT {
                    println!(
                        "CONSECUTIVE_WATERING_PLANT[{}] = {}",
                        i, CONSECUTIVE_WATERING_PLANT[i]
                    );
                }
            }
        }

        let mut counter_unit1 = PcntDriver::new(
            peripherals.pcnt0,
            Some(peripherals.pins.gpio18),
            Option::<AnyInputPin>::None,
            Option::<AnyInputPin>::None,
            Option::<AnyInputPin>::None,
        )?;

        println!("Channel config start");

        counter_unit1.channel_config(
            PcntChannel::Channel0,
            PinIndex::Pin0,
            PinIndex::Pin1,
            &PcntChannelConfig {
                lctrl_mode: PcntControlMode::Keep,
                hctrl_mode: PcntControlMode::Keep,
                pos_mode: PcntCountMode::Increment,
                neg_mode: PcntCountMode::Hold,
                counter_h_lim: i16::MAX,
                counter_l_lim: 0,
            },
        )?;

        println!("Setup filter");

        //TODO validate filter value! currently max allowed value
        counter_unit1.set_filter_value(1023)?;
        counter_unit1.filter_enable()?;

        println!("Wifi start");

        let sys_loop = EspSystemEventLoop::take()?;
        let nvs = EspDefaultNvsPartition::take()?;
        let wifi_driver = EspWifi::new(peripherals.modem, sys_loop, Some(nvs))?;

        let adc_config = esp_idf_hal::adc::config::Config {
            resolution: esp_idf_hal::adc::config::Resolution::Resolution12Bit,
            calibration: true,
        };
        let tank_driver = AdcDriver::new(peripherals.adc1, &adc_config)?;
        let tank_channel: AdcChannelDriver<'_, { attenuation::DB_11 }, Gpio39> =
            AdcChannelDriver::new(peripherals.pins.gpio39)?;

        let mut solar_is_day = PinDriver::input(peripherals.pins.gpio25)?;
        solar_is_day.set_pull(Pull::Floating)?;

        let mut boot_button = PinDriver::input(peripherals.pins.gpio0)?;
        boot_button.set_pull(Pull::Floating)?;

        let mut light = PinDriver::input_output(peripherals.pins.gpio26)?;
        light.set_pull(Pull::Floating).unwrap();

        let mut main_pump = PinDriver::input_output(peripherals.pins.gpio23)?;
        main_pump.set_pull(Pull::Floating)?;
        main_pump.set_low()?;
        let mut tank_power = PinDriver::input_output(peripherals.pins.gpio27)?;
        tank_power.set_pull(Pull::Floating)?;
        let mut general_fault = PinDriver::input_output(peripherals.pins.gpio13)?;
        general_fault.set_pull(Pull::Floating)?;
        general_fault.set_low()?;
        let one_wire_bus = OneWire::new(one_wire_pin)
            .map_err(|err| -> anyhow::Error { anyhow!("Missing attribute: {:?}", err) })?;

        println!("After stuff");

        let status = print_battery(&mut battery_driver);
        if status.is_err() {
            println!("Error communicating with battery!! {:?}", status.err());
        }
        let rv = Mutex::new(PlantCtrlBoard {
            shift_register,
            tank_driver,
            tank_channel,
            solar_is_day,
            boot_button,
            light,
            main_pump,
            tank_power,
            general_fault,
            one_wire_bus,
            signal_counter: counter_unit1,
            wifi_driver,
            mqtt_client: None,
            battery_driver,
        });
        Ok(rv)
    }
}

fn quantize_to_next_5_percent(value: f64) -> i32 {
    // Multiply by 100 to work with integer values
    let multiplied_value = (value * 100.0).round() as i32;

    // Calculate the remainder when divided by 5
    let remainder = multiplied_value % 5;

    // If the remainder is greater than or equal to half of 5, round up to the next 5%
    let rounded_value = if remainder >= 2 {
        multiplied_value + (5 - remainder)
    } else {
        multiplied_value - remainder
    };

    // Divide by 100 to get back to a float
    rounded_value / 100
}
