//! [`AudioRecorder`] — direct `AVAudioRecorder` capture.

#![allow(
    clippy::missing_errors_doc,
    clippy::must_use_candidate,
    clippy::module_name_repetitions
)]

use core::ffi::{c_char, c_void};
use core::ptr;
use std::convert::TryFrom;
use std::ffi::{CStr, CString};
use std::path::Path;

use crate::error::{from_swift, AVAudioError};
use crate::ffi;

fn channel_to_i32(channel: usize) -> Result<i32, AVAudioError> {
    i32::try_from(channel)
        .map_err(|_| AVAudioError::InvalidArgument("channel index exceeds Int32 range".into()))
}

/// Callback configuration bridging `AVAudioRecorderDelegate`.
#[derive(Default)]
pub struct AudioRecorderDelegate {
    did_finish_recording: Option<Box<dyn FnMut(bool) + Send + 'static>>,
    encode_error: Option<Box<dyn FnMut(Option<String>) + Send + 'static>>,
}

impl AudioRecorderDelegate {
    /// Creates an empty recorder-delegate configuration.
    pub fn new() -> Self {
        Self::default()
    }

    /// Registers a finish-recording callback.
    #[must_use]
    pub fn on_finish_recording<F>(mut self, callback: F) -> Self
    where
        F: FnMut(bool) + Send + 'static,
    {
        self.did_finish_recording = Some(Box::new(callback));
        self
    }

    /// Registers an encode-error callback.
    #[must_use]
    pub fn on_encode_error<F>(mut self, callback: F) -> Self
    where
        F: FnMut(Option<String>) + Send + 'static,
    {
        self.encode_error = Some(Box::new(callback));
        self
    }
}

struct AudioRecorderDelegateState {
    did_finish_recording: Option<Box<dyn FnMut(bool) + Send + 'static>>,
    encode_error: Option<Box<dyn FnMut(Option<String>) + Send + 'static>>,
}

/// Wraps an `AVAudioRecorder` instance.
pub struct AudioRecorder {
    ptr: *mut c_void,
}

unsafe impl Send for AudioRecorder {}

impl Drop for AudioRecorder {
    fn drop(&mut self) {
        if !self.ptr.is_null() {
            unsafe { ffi::av_audio_recorder_release(self.ptr) };
            self.ptr = ptr::null_mut();
        }
    }
}

impl AudioRecorder {
    /// Creates a recorder writing linear PCM data to the supplied path.
    pub fn create(
        path: impl AsRef<Path>,
        sample_rate: f64,
        channels: u32,
        bit_depth: u32,
    ) -> Result<Self, AVAudioError> {
        let path = path
            .as_ref()
            .to_str()
            .ok_or_else(|| AVAudioError::InvalidArgument("path is not valid UTF-8".into()))?;
        let path = CString::new(path).map_err(|error| {
            AVAudioError::InvalidArgument(format!("path contains NUL byte: {error}"))
        })?;
        let channels = i32::try_from(channels).map_err(|_| {
            AVAudioError::InvalidArgument("channel count exceeds Int32 range".into())
        })?;
        let bit_depth = i32::try_from(bit_depth)
            .map_err(|_| AVAudioError::InvalidArgument("bit depth exceeds Int32 range".into()))?;
        let mut err: *mut c_char = ptr::null_mut();
        let ptr = unsafe {
            ffi::av_audio_recorder_create(path.as_ptr(), sample_rate, channels, bit_depth, &mut err)
        };
        if ptr.is_null() {
            return Err(unsafe { from_swift(ffi::status::OPERATION_FAILED, err) });
        }
        Ok(Self { ptr })
    }

    #[cfg(feature = "async")]
    pub(crate) const fn ptr(&self) -> *mut c_void {
        self.ptr
    }

    /// Installs delegate callbacks bridging `AVAudioRecorderDelegate`.
    pub fn set_delegate(&self, delegate: AudioRecorderDelegate) -> Result<(), AVAudioError> {
        let state = Box::new(AudioRecorderDelegateState {
            did_finish_recording: delegate.did_finish_recording,
            encode_error: delegate.encode_error,
        });
        let userdata = Box::into_raw(state).cast::<c_void>();
        let mut err: *mut c_char = ptr::null_mut();
        let status = unsafe {
            ffi::av_audio_recorder_set_delegate(
                self.ptr,
                Some(recorder_finish_trampoline),
                Some(recorder_encode_error_trampoline),
                userdata,
                Some(recorder_delegate_drop),
                &mut err,
            )
        };
        if status != ffi::status::OK {
            unsafe { recorder_delegate_drop(userdata) };
            return Err(unsafe { from_swift(status, err) });
        }
        Ok(())
    }

    /// Clears any installed recorder delegate callbacks.
    pub fn clear_delegate(&self) {
        unsafe { ffi::av_audio_recorder_clear_delegate(self.ptr) };
    }

    /// Starts recording.
    pub fn record(&self) -> bool {
        unsafe { ffi::av_audio_recorder_record(self.ptr) }
    }

    /// Stops recording.
    pub fn stop(&self) {
        unsafe { ffi::av_audio_recorder_stop(self.ptr) };
    }

    /// Pauses recording.
    pub fn pause(&self) {
        unsafe { ffi::av_audio_recorder_pause(self.ptr) };
    }

    /// Returns whether recording is active.
    pub fn is_recording(&self) -> bool {
        unsafe { ffi::av_audio_recorder_is_recording(self.ptr) }
    }

    /// Returns the current recorded duration in seconds.
    pub fn current_time(&self) -> f64 {
        unsafe { ffi::av_audio_recorder_current_time(self.ptr) }
    }

    /// Enables or disables metering.
    pub fn set_metering_enabled(&self, enabled: bool) {
        unsafe { ffi::av_audio_recorder_set_metering_enabled(self.ptr, enabled) };
    }

    /// Refreshes meter values.
    pub fn update_meters(&self) {
        unsafe { ffi::av_audio_recorder_update_meters(self.ptr) };
    }

    /// Returns the average power for a channel.
    pub fn average_power(&self, channel: usize) -> Result<f32, AVAudioError> {
        let channel = channel_to_i32(channel)?;
        Ok(unsafe { ffi::av_audio_recorder_average_power(self.ptr, channel) })
    }

    /// Returns the peak power for a channel.
    pub fn peak_power(&self, channel: usize) -> Result<f32, AVAudioError> {
        let channel = channel_to_i32(channel)?;
        Ok(unsafe { ffi::av_audio_recorder_peak_power(self.ptr, channel) })
    }

    /// Deletes the underlying recording file.
    pub fn delete_recording(&self) -> bool {
        unsafe { ffi::av_audio_recorder_delete_recording(self.ptr) }
    }
}

unsafe extern "C" fn recorder_finish_trampoline(userdata: *mut c_void, success: bool) {
    let Some(state) = userdata.cast::<AudioRecorderDelegateState>().as_mut() else {
        return;
    };
    if let Some(callback) = state.did_finish_recording.as_mut() {
        callback(success);
    }
}

unsafe extern "C" fn recorder_encode_error_trampoline(userdata: *mut c_void, message: *mut c_char) {
    let Some(state) = userdata.cast::<AudioRecorderDelegateState>().as_mut() else {
        return;
    };
    if let Some(callback) = state.encode_error.as_mut() {
        let value = if message.is_null() {
            None
        } else {
            let decoded = CStr::from_ptr(message).to_string_lossy().into_owned();
            unsafe { ffi::ava_string_free(message) };
            Some(decoded)
        };
        callback(value);
    } else if !message.is_null() {
        unsafe { ffi::ava_string_free(message) };
    }
}

unsafe extern "C" fn recorder_delegate_drop(userdata: *mut c_void) {
    if userdata.is_null() {
        return;
    }
    drop(Box::from_raw(userdata.cast::<AudioRecorderDelegateState>()));
}
