use crate::engine::PatternChange;
use crate::model::{FollowUp, LaunchQuantization, PatternSpeed, SyncMode};
use std::collections::{HashMap, HashSet};

#[derive(Clone)]
pub struct StagedChange {
    pub change: PatternChange,
    pub quantization: LaunchQuantization,
    pub sync_mode: SyncMode,
}

#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub enum StagedMuteChange {
    ToggleMute { bank: usize, pattern: usize },
    ToggleSolo { bank: usize, pattern: usize },
}

pub struct StagedPropChange {
    pub name: Option<String>,
    pub description: Option<String>,
    pub length: Option<usize>,
    pub speed: PatternSpeed,
    pub quantization: LaunchQuantization,
    pub sync_mode: SyncMode,
    pub follow_up: FollowUp,
}

pub struct PlaybackState {
    pub playing: bool,
    pub staged_changes: Vec<StagedChange>,
    pub queued_changes: Vec<StagedChange>,
    pub staged_mute_changes: HashSet<StagedMuteChange>,
    pub staged_prop_changes: HashMap<(usize, usize), StagedPropChange>,
}

impl Default for PlaybackState {
    fn default() -> Self {
        Self {
            playing: true,
            staged_changes: Vec::new(),
            queued_changes: Vec::new(),
            staged_mute_changes: HashSet::new(),
            staged_prop_changes: HashMap::new(),
        }
    }
}

impl PlaybackState {
    pub fn toggle(&mut self) {
        self.playing = !self.playing;
    }

    pub fn clear_queues(&mut self) {
        self.staged_changes.clear();
        self.queued_changes.clear();
        self.staged_prop_changes.clear();
    }

    pub fn has_staged_props(&self, bank: usize, pattern: usize) -> bool {
        self.staged_prop_changes.contains_key(&(bank, pattern))
    }

    pub fn stage_mute(&mut self, bank: usize, pattern: usize) {
        let change = StagedMuteChange::ToggleMute { bank, pattern };
        if self.staged_mute_changes.contains(&change) {
            self.staged_mute_changes.remove(&change);
        } else {
            self.staged_mute_changes.insert(change);
        }
    }

    pub fn stage_solo(&mut self, bank: usize, pattern: usize) {
        let change = StagedMuteChange::ToggleSolo { bank, pattern };
        if self.staged_mute_changes.contains(&change) {
            self.staged_mute_changes.remove(&change);
        } else {
            self.staged_mute_changes.insert(change);
        }
    }

    pub fn clear_staged_mutes(&mut self) {
        self.staged_mute_changes.retain(|c| !matches!(c, StagedMuteChange::ToggleMute { .. }));
    }

    pub fn clear_staged_solos(&mut self) {
        self.staged_mute_changes.retain(|c| !matches!(c, StagedMuteChange::ToggleSolo { .. }));
    }

    pub fn has_staged_mute(&self, bank: usize, pattern: usize) -> bool {
        self.staged_mute_changes.contains(&StagedMuteChange::ToggleMute { bank, pattern })
    }

    pub fn has_staged_solo(&self, bank: usize, pattern: usize) -> bool {
        self.staged_mute_changes.contains(&StagedMuteChange::ToggleSolo { bank, pattern })
    }
}
