use crate::{
    types::pipewire::DeviceType,
    utils::{
        daemon::get_daemon_config,
        pipewire::{PwTerminator, create_link, get_device, link_player_to_virtual_mic},
    },
};
use anyhow::{Result, anyhow};
use rodio::{Decoder, DeviceSinkBuilder, MixerDeviceSink, Player, Source};
use serde::{Deserialize, Serialize};
use std::{
    collections::HashMap,
    error::Error,
    fs,
    path::{Path, PathBuf},
    time::Duration,
};

#[derive(Debug, Eq, PartialEq, Default, Clone, Serialize, Deserialize)]
pub enum PlayerState {
    #[default]
    Stopped,
    Paused,
    Playing,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TrackInfo {
    pub id: u32,
    pub path: PathBuf,
    pub duration: Option<f32>,
    pub position: f32,
    pub volume: f32,
    pub looped: bool,
    pub paused: bool,
}

#[derive(Default, Debug, Clone, Serialize, Deserialize)]
pub struct FullState {
    pub state: PlayerState,
    pub tracks: Vec<TrackInfo>,
    pub volume: f32,
    pub current_input: String,
    pub all_inputs: HashMap<String, String>,
}

pub struct PlayingSound {
    pub id: u32,
    pub sink: Player,
    pub path: PathBuf,
    pub duration: Option<f32>,
    pub looped: bool,
    pub volume: f32,
}

pub struct AudioPlayer {
    stream_handle: Option<MixerDeviceSink>,
    pub tracks: HashMap<u32, PlayingSound>,
    pub next_id: u32,

    input_link_sender: Option<PwTerminator>,
    player_link_sender: Option<PwTerminator>,
    pub input_device_name: Option<String>,

    pub volume: f32, // Master volume
}

impl AudioPlayer {
    pub async fn new() -> Result<Self> {
        let daemon_config = get_daemon_config();
        let default_volume = daemon_config.default_volume.unwrap_or(1.0);

        let mut audio_player = AudioPlayer {
            stream_handle: None,
            tracks: HashMap::new(),
            next_id: 1,

            input_link_sender: None,
            player_link_sender: None,
            input_device_name: daemon_config.default_input_name.clone(),

            volume: default_volume,
        };

        if audio_player.input_device_name.is_some() {
            audio_player.link_devices().await?;
        }

        Ok(audio_player)
    }

    fn ensure_stream(&mut self) -> Result<&MixerDeviceSink> {
        if self.stream_handle.is_none() {
            let mut sink = DeviceSinkBuilder::open_default_sink()?;
            sink.log_on_drop(false);
            self.stream_handle = Some(sink);
        }
        self.stream_handle
            .as_ref()
            .ok_or_else(|| anyhow!("Failed to initialize stream_handle"))
    }

    fn drop_stream(&mut self) {
        if self.stream_handle.is_some() {
            self.stream_handle = None;
            self.abort_player_link_thread();
        }
    }

    fn abort_link_thread(&mut self) {
        if self.input_link_sender.is_some() {
            println!("Sent terminate signal to input link thread");
            self.input_link_sender = None;
        }
    }

    fn abort_player_link_thread(&mut self) {
        if self.player_link_sender.is_some() {
            println!("Sent terminate signal to player link thread");
            self.player_link_sender = None;
        }
    }

    async fn link_player(&mut self) -> Result<()> {
        if self.player_link_sender.is_some() {
            return Ok(());
        }

        match link_player_to_virtual_mic().await {
            Ok(sender) => {
                self.player_link_sender = Some(sender);
                Ok(())
            }
            Err(_) => Ok(()),
        }
    }

    async fn link_devices(&mut self) -> Result<()> {
        self.abort_link_thread();

        let input_device;
        if let Some(input_device_name) = &self.input_device_name {
            if let Ok(device) = get_device(input_device_name).await {
                input_device = device;
            } else {
                eprintln!(
                    "Could not find selected input device {}, skipping device linking",
                    input_device_name
                );
                return Ok(());
            }
        } else {
            eprintln!("No input device selected, skipping device linking");
            return Ok(());
        }

        let daemon_input;
        if let Ok(device) = get_device("pwsp-virtual-mic").await {
            daemon_input = device;
        } else {
            eprintln!("Could not find pwsp-virtual-mic device, skipping device linking");
            return Ok(());
        }

        let Some(output_fl) = input_device.output_fl.clone() else {
            eprintln!("Failed to get pwsp-daemon output_fl");
            return Ok(());
        };
        let Some(output_fr) = input_device.output_fr.clone() else {
            eprintln!("Failed to get pwsp-daemon output_fr");
            return Ok(());
        };
        let Some(input_fl) = daemon_input.input_fl.clone() else {
            eprintln!("Failed to get pwsp-daemon input_fl");
            return Ok(());
        };
        let Some(input_fr) = daemon_input.input_fr.clone() else {
            eprintln!("Failed to get pwsp-daemon input_fr");
            return Ok(());
        };

        self.input_link_sender = Some(create_link(output_fl, output_fr, input_fl, input_fr).await?);

        Ok(())
    }

    pub fn pause(&mut self, id: Option<u32>) {
        if let Some(id) = id {
            if let Some(sound) = self.tracks.get_mut(&id) {
                sound.sink.pause();
            }
        } else {
            for sound in self.tracks.values_mut() {
                sound.sink.pause();
            }
        }
    }

    pub fn resume(&mut self, id: Option<u32>) {
        if let Some(id) = id {
            if let Some(sound) = self.tracks.get_mut(&id) {
                sound.sink.play();
            }
        } else {
            for sound in self.tracks.values_mut() {
                sound.sink.play();
            }
        }
    }

    pub fn stop(&mut self, id: Option<u32>) {
        if let Some(id) = id {
            self.tracks.remove(&id);
        } else {
            self.tracks.clear();
        }
        if self.tracks.is_empty() {
            self.drop_stream();
        }
    }

    pub fn is_paused(&self) -> bool {
        if self.tracks.is_empty() {
            return false;
        }
        self.tracks.values().all(|s| s.sink.is_paused())
    }

    pub fn get_state(&self) -> PlayerState {
        if self.tracks.is_empty() {
            return PlayerState::Stopped;
        }

        if self
            .tracks
            .values()
            .any(|s| !s.sink.is_paused() && !s.sink.empty())
        {
            return PlayerState::Playing;
        }

        if self.is_paused() {
            return PlayerState::Paused;
        }

        PlayerState::Stopped
    }

    pub fn get_volume(&mut self, id: Option<u32>) -> Option<f32> {
        if let Some(id) = id {
            if let Some(sound) = self.tracks.get_mut(&id) {
                Some(sound.sink.volume())
            } else {
                None
            }
        } else {
            Some(self.volume)
        }
    }

    pub fn set_volume(&mut self, volume: f32, id: Option<u32>) {
        if let Some(id) = id {
            if let Some(sound) = self.tracks.get_mut(&id) {
                sound.volume = volume;
                sound.sink.set_volume(self.volume * volume);
            }
        } else {
            self.volume = volume;
            for sound in self.tracks.values_mut() {
                sound.sink.set_volume(self.volume * sound.volume);
            }
        }
    }

    pub fn get_position(&self, id: Option<u32>) -> f32 {
        if let Some(id) = id {
            if let Some(sound) = self.tracks.get(&id) {
                return sound.sink.get_pos().as_secs_f32();
            }
        } else if let Some(sound) = self.tracks.values().last() {
            // Fallback to last added track if no ID
            return sound.sink.get_pos().as_secs_f32();
        }
        0.0
    }

    pub fn seek(&mut self, position: f32, id: Option<u32>) -> Result<()> {
        let position = if position < 0.0 { 0.0 } else { position };

        if let Some(id) = id {
            if let Some(sound) = self.tracks.get_mut(&id) {
                sound.sink.try_seek(Duration::from_secs_f32(position))?;
            }
        } else {
            // Seek all? Or last? Let's seek all for now if no ID provided
            for sound in self.tracks.values_mut() {
                sound.sink.try_seek(Duration::from_secs_f32(position)).ok();
            }
        }
        Ok(())
    }

    pub fn get_duration(&mut self, id: Option<u32>) -> Result<f32> {
        if let Some(id) = id {
            if let Some(sound) = self.tracks.get(&id) {
                return sound.duration.ok_or(anyhow!("Unknown duration"));
            }
        } else if let Some(sound) = self.tracks.values().last() {
            return sound.duration.ok_or(anyhow!("Unknown duration"));
        }
        Err(anyhow!("No track playing"))
    }

    pub async fn play(&mut self, file_path: &Path, concurrent: bool) -> Result<u32> {
        let path_buf = file_path.to_path_buf();

        let decoder_result =
            tokio::task::spawn_blocking(move || -> Result<_, Box<dyn Error + Send + Sync>> {
                if !path_buf.exists() {
                    return Err(format!("File does not exist: {}", path_buf.display()).into());
                }

                let file = fs::File::open(&path_buf)?;
                let decoder = Decoder::try_from(file)
                    .map_err(|e| Box::new(e) as Box<dyn Error + Send + Sync>)?;
                Ok(decoder)
            })
            .await?;

        match decoder_result {
            Ok(source) => {
                if !concurrent {
                    self.tracks.clear();
                }

                self.ensure_stream()?;
                self.link_player().await.ok();

                let id = self.next_id;
                self.next_id += 1;

                let duration = source.total_duration().map(|d| d.as_secs_f32());

                let mixer = self
                    .stream_handle
                    .as_ref()
                    .ok_or_else(|| anyhow::anyhow!("stream_handle is unexpectedly missing"))?
                    .mixer();
                let sink = Player::connect_new(mixer);
                sink.set_volume(self.volume); // Default volume is 1.0 * master
                sink.append(source);
                sink.play();

                let sound = PlayingSound {
                    id,
                    sink,
                    path: file_path.to_path_buf(),
                    duration,
                    looped: false,
                    volume: 1.0,
                };

                self.tracks.insert(id, sound);

                Ok(id)
            }
            Err(err) => Err(anyhow!(err)),
        }
    }

    pub fn set_loop(&mut self, enabled: bool, id: Option<u32>) {
        if let Some(id) = id {
            if let Some(sound) = self.tracks.get_mut(&id) {
                sound.looped = enabled;
            }
        } else {
            // Set loop for all? Or just last?
            // Let's set for all.
            for sound in self.tracks.values_mut() {
                sound.looped = enabled;
            }
        }
    }

    pub fn get_tracks(&self) -> Vec<TrackInfo> {
        let mut tracks: Vec<_> = self
            .tracks
            .values()
            .map(|sound| TrackInfo {
                id: sound.id,
                path: sound.path.clone(),
                duration: sound.duration,
                position: sound.sink.get_pos().as_secs_f32(),
                volume: sound.volume,
                looped: sound.looped,
                paused: sound.sink.is_paused(),
            })
            .collect();
        tracks.sort_by_key(|t| t.id);
        tracks
    }

    pub async fn update(&mut self, check_devices: bool) {
        if check_devices {
            if let Some(input_device_name) = &self.input_device_name {
                // Unlink devices if selected input device was removed
                if self.input_link_sender.is_some() && get_device(input_device_name).await.is_err()
                {
                    eprintln!(
                        "Selected input device {} was removed, unlinking devices",
                        input_device_name
                    );
                    self.abort_link_thread();
                }
                // Link devices if not linked
                else if self.input_link_sender.is_none() {
                    self.link_devices().await.ok();
                }
            }

            if self.stream_handle.is_some() && self.player_link_sender.is_none() {
                self.link_player().await.ok();
            }
        }

        // Handle looped sounds
        let mut restarts = vec![];

        for (id, sound) in &self.tracks {
            if sound.sink.empty() && sound.looped {
                restarts.push(*id);
            }
        }

        let mut restart_futures = vec![];

        for id in restarts {
            if let Some(sound) = self.tracks.get(&id) {
                let path = sound.path.clone();
                let handle = tokio::task::spawn_blocking(move || {
                    if let Ok(file) = fs::File::open(&path)
                        && let Ok(source) = Decoder::try_from(file)
                    {
                        return Some((id, source));
                    }
                    None
                });
                restart_futures.push(handle);
            }
        }

        for handle in restart_futures {
            if let Ok(res) = handle.await
                && let Some((id, source)) = res
                && let Some(sound) = self.tracks.get_mut(&id)
            {
                sound.sink.append(source);
                sound.sink.play();
            }
        }

        self.tracks
            .retain(|_, sound| !sound.sink.empty() || sound.looped);

        if self.tracks.is_empty() {
            self.drop_stream();
        }
    }

    pub async fn set_current_input_device(&mut self, name: &str) -> Result<()> {
        let input_device = get_device(name).await?;

        if input_device.device_type != DeviceType::Input {
            return Err(anyhow!("Selected device is not an input device"));
        }

        self.input_device_name = Some(name.to_string());

        self.link_devices().await?;

        Ok(())
    }
}
