use std::fmt;

use crate::{filters::FilterError, smooth::SmoothError, spectrum::SpectrumError};

#[derive(Debug)]
pub enum ScientificError {
    Io(std::io::Error),
    Spectrum(SpectrumError),
    Filter(FilterError),
    Math(String),
    Smooth(SmoothError),
    InvalidArgument(String),
    // 可拓展更多
}

impl std::error::Error for ScientificError {}

impl fmt::Display for ScientificError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ScientificError::Io(e) => write!(f, "IO Error: {}", e),
            ScientificError::Spectrum(e) => write!(f, "Spectrum Error: {}", e),
            ScientificError::Filter(e) => write!(f, "Filter Error: {}", e),
            ScientificError::Math(msg) => write!(f, "Math Error: {}", msg),
            ScientificError::Smooth(e) => write!(f, "Filter Error: {}", e),
            ScientificError::InvalidArgument(msg) => write!(f, "Invalid Argument: {}", msg),
        }
    }
}

impl From<std::io::Error> for ScientificError {
    fn from(err: std::io::Error) -> ScientificError {
        ScientificError::Io(err)
    }
}

impl From<SpectrumError> for ScientificError {
    fn from(err: SpectrumError) -> ScientificError {
        ScientificError::Spectrum(err)
    }
}

impl From<FilterError> for ScientificError {
    fn from(err: FilterError) -> ScientificError {
        ScientificError::Filter(err)
    }
}

impl From<SmoothError> for ScientificError {
    fn from(err: SmoothError) -> ScientificError {
        ScientificError::Smooth(err)
    }
}