#[derive(Clone, Debug)]
/// All kind of errors AllMyToes may produce.
pub enum ToeErrorType {
    FileDoesNotExist,
    FileNotReadable,
    NoCacheIdentifiable,
    FormatCannotBeHandled,
    ErrorOnProbingMimeType,
    ProbingMimeTypeHadNoResult,
    SourceFileCantBeOpened,
    SourceFileCantBeRead,
    TempCantBeCreated(String),
    TempDataCantBeWritten(String),
    TempHeaderCantBeWritten(String),
    UnsupportedImageFormat,
    CacheDoesNotExist(String),
    FileAllocationError(String),
    TempThumbCantBeRenamed(String, String, String),
    ThumbDoesNotExistsAfterCreation,
    ThumbDirCantBeCreated(String),
    CanNotSetPermission(String, String),
}

impl ToeErrorType {
    /// Returns an human readable error message for the error.
    pub fn msg(&self) -> String {
        match self {
            ToeErrorType::FileDoesNotExist => String::from("File does not exist"),
            ToeErrorType::FileNotReadable => String::from("File is not readable"),
            ToeErrorType::NoCacheIdentifiable => String::from("Could not identify a cache dir. At least one of the env vars HOME and XDG_CACHE_HOME must be defined."),
            ToeErrorType::FormatCannotBeHandled => String::from("File format cannot be processed."),
            ToeErrorType::ErrorOnProbingMimeType => String::from("Error on probing MIME type."),
            ToeErrorType::ProbingMimeTypeHadNoResult => String::from("Could not identify MIME type of input file."),
            ToeErrorType::SourceFileCantBeOpened => String::from("Cannot open source file for reading."),
            ToeErrorType::SourceFileCantBeRead => String::from("Error when reading source file."),
            ToeErrorType::TempCantBeCreated(v) => format!("Cannot create temporary thumbnail file {v}"),
            ToeErrorType::TempDataCantBeWritten(v) => format!("Cannot write data chunk to temporary thumbnail file {v}"),
            ToeErrorType::TempHeaderCantBeWritten(v) => format!("Cannot write header to temporary thumbnail file {v}"),
            ToeErrorType::UnsupportedImageFormat => String::from("Unsupported image format"),
            ToeErrorType::CacheDoesNotExist(v) => format!("Cache dir {v} cannot be found on file system."),
            ToeErrorType::FileAllocationError(v) => format!("File allocation error: {v}"),
            ToeErrorType::TempThumbCantBeRenamed(f, t, e) => format!("Temporary thumb file cannot be renamed from {f} to {t}. ({e})"),
            ToeErrorType::ThumbDoesNotExistsAfterCreation => String::from("Thumb has been created but the file does not exist."),
            ToeErrorType::ThumbDirCantBeCreated(v) => format!("Thumbnail directory {v} can't be created."),
            ToeErrorType::CanNotSetPermission(f, m) => format!("Cannot set the permissions for file or directory '{f}'. Error: {m}"),
        }
    }

    /// Return/exit code for the application per error case
    pub fn error_code(&self) -> i32 {
        match self {
            ToeErrorType::FileDoesNotExist => 1,
            ToeErrorType::FileNotReadable => 2,

            ToeErrorType::CacheDoesNotExist(_) => 10,

            ToeErrorType::SourceFileCantBeOpened => 103,
            ToeErrorType::SourceFileCantBeRead => 104,
            ToeErrorType::FormatCannotBeHandled => 103,
            ToeErrorType::UnsupportedImageFormat => 104,
            ToeErrorType::ErrorOnProbingMimeType => 105,
            ToeErrorType::ProbingMimeTypeHadNoResult => 106,
            ToeErrorType::NoCacheIdentifiable => 110,
            ToeErrorType::ThumbDirCantBeCreated(_) => 112,
            ToeErrorType::TempCantBeCreated(_) => 113,
            ToeErrorType::TempDataCantBeWritten(_) => 114,
            ToeErrorType::TempHeaderCantBeWritten(_) => 115,
            ToeErrorType::FileAllocationError(_) => 116,
            ToeErrorType::TempThumbCantBeRenamed(_, _, _) => 117,
            ToeErrorType::ThumbDoesNotExistsAfterCreation => 120,
            ToeErrorType::CanNotSetPermission(_, _) => 121,
        }
    }
}
