use crate::mem_fs::FileSystem as MemFileSystem;
use crate::{
    FileDescriptor, FileOpener, FileSystem, FsError, Metadata, OpenOptions, OpenOptionsConfig,
    ReadDir, VirtualFile,
};
use anyhow::anyhow;
use std::convert::TryInto;
use std::io::{Error as IoError, ErrorKind as IoErrorKind, Read, Seek, SeekFrom, Write};
use std::ops::Deref;
use std::path::Path;
use std::path::PathBuf;
use std::sync::Arc;
use webc::{FsEntry, FsEntryType, OwnedFsEntryFile, WebC};

/// Custom file system wrapper to map requested file paths
#[derive(Debug)]
pub struct WebcFileSystem<T>
where
    T: std::fmt::Debug + Send + Sync + 'static,
{
    pub package: String,
    pub webc: Arc<T>,
    pub memory: Arc<MemFileSystem>,
}

impl<T> WebcFileSystem<T>
where
    T: std::fmt::Debug + Send + Sync + 'static,
    T: Deref<Target = WebC<'static>>,
{
    pub fn init(webc: Arc<T>, package: &str) -> Self {
        let fs = Self {
            package: package.to_string(),
            webc: webc.clone(),
            memory: Arc::new(MemFileSystem::default()),
        };
        for volume in webc.get_volumes_for_package(package) {
            for directory in webc.list_directories(&volume) {
                let _ = fs.create_dir(Path::new(&directory));
            }
        }
        fs
    }
}

/// Custom file opener, returns a WebCFile
#[derive(Debug)]
struct WebCFileOpener<T>
where
    T: std::fmt::Debug + Send + Sync + 'static,
{
    pub package: String,
    pub webc: Arc<T>,
    pub memory: Arc<MemFileSystem>,
}

impl<T> FileOpener for WebCFileOpener<T>
where
    T: std::fmt::Debug + Send + Sync + 'static,
    T: Deref<Target = WebC<'static>>,
{
    fn open(
        &mut self,
        path: &Path,
        _conf: &OpenOptionsConfig,
    ) -> Result<Box<dyn VirtualFile + Send + Sync>, FsError> {
        match get_volume_name_opt(path) {
            Some(volume) => {
                let file = (*self.webc)
                    .volumes
                    .get(&volume)
                    .ok_or(FsError::EntityNotFound)?
                    .get_file_entry(&format!("{}", path.display()))
                    .map_err(|_e| FsError::EntityNotFound)?;

                Ok(Box::new(WebCFile {
                    package: self.package.clone(),
                    volume,
                    webc: self.webc.clone(),
                    path: path.to_path_buf(),
                    entry: file,
                    cursor: 0,
                }))
            }
            None => {
                for volume in self.webc.get_volumes_for_package(&self.package) {
                    let v = match self.webc.volumes.get(&volume) {
                        Some(s) => s,
                        None => continue, // error
                    };

                    let entry = match v.get_file_entry(&format!("{}", path.display())) {
                        Ok(s) => s,
                        Err(_) => continue, // error
                    };

                    return Ok(Box::new(WebCFile {
                        package: self.package.clone(),
                        volume: volume.clone(),
                        webc: self.webc.clone(),
                        path: path.to_path_buf(),
                        entry,
                        cursor: 0,
                    }));
                }
                self.memory.new_open_options().open(path)
            }
        }
    }
}

#[derive(Debug)]
struct WebCFile<T>
where
    T: std::fmt::Debug + Send + Sync + 'static,
{
    pub webc: Arc<T>,
    #[allow(dead_code)]
    pub package: String,
    pub volume: String,
    #[allow(dead_code)]
    pub path: PathBuf,
    pub entry: OwnedFsEntryFile,
    pub cursor: u64,
}

impl<T> VirtualFile for WebCFile<T>
where
    T: std::fmt::Debug + Send + Sync + 'static,
    T: Deref<Target = WebC<'static>>,
{
    fn last_accessed(&self) -> u64 {
        0
    }
    fn last_modified(&self) -> u64 {
        0
    }
    fn created_time(&self) -> u64 {
        0
    }
    fn size(&self) -> u64 {
        self.entry.get_len()
    }
    fn set_len(&mut self, _new_size: u64) -> Result<(), FsError> {
        Ok(())
    }
    fn unlink(&mut self) -> Result<(), FsError> {
        Ok(())
    }
    fn bytes_available(&self) -> Result<usize, FsError> {
        Ok(self.size().try_into().unwrap_or(u32::MAX as usize))
    }
    fn sync_to_disk(&self) -> Result<(), FsError> {
        Ok(())
    }
    fn get_fd(&self) -> Option<FileDescriptor> {
        None
    }
}

impl<T> Read for WebCFile<T>
where
    T: std::fmt::Debug + Send + Sync + 'static,
    T: Deref<Target = WebC<'static>>,
{
    fn read(&mut self, buf: &mut [u8]) -> Result<usize, IoError> {
        let bytes = self
            .webc
            .volumes
            .get(&self.volume)
            .ok_or_else(|| {
                IoError::new(
                    IoErrorKind::NotFound,
                    anyhow!("Unknown volume {:?}", self.volume),
                )
            })?
            .get_file_bytes(&self.entry)
            .map_err(|e| IoError::new(IoErrorKind::NotFound, e))?;

        let cursor: usize = self.cursor.try_into().unwrap_or(u32::MAX as usize);
        let _start = cursor.min(bytes.len());
        let bytes = &bytes[cursor..];

        let mut len = 0;
        for (source, target) in bytes.iter().zip(buf.iter_mut()) {
            *target = *source;
            len += 1;
        }

        Ok(len)
    }
}

// WebC file is not writable, the FileOpener will return a MemoryFile for writing instead
// This code should never be executed (since writes are redirected to memory instead).
impl<T> Write for WebCFile<T>
where
    T: std::fmt::Debug + Send + Sync + 'static,
{
    fn write(&mut self, buf: &[u8]) -> Result<usize, IoError> {
        Ok(buf.len())
    }
    fn flush(&mut self) -> Result<(), IoError> {
        Ok(())
    }
}

impl<T> Seek for WebCFile<T>
where
    T: std::fmt::Debug + Send + Sync + 'static,
    T: Deref<Target = WebC<'static>>,
{
    fn seek(&mut self, pos: SeekFrom) -> Result<u64, IoError> {
        let self_size = self.size();
        match pos {
            SeekFrom::Start(s) => {
                self.cursor = s.min(self_size);
            }
            SeekFrom::End(e) => {
                let self_size_i64 = self_size.try_into().unwrap_or(i64::MAX);
                self.cursor = ((self_size_i64).saturating_add(e))
                    .min(self_size_i64)
                    .try_into()
                    .unwrap_or(i64::MAX as u64);
            }
            SeekFrom::Current(c) => {
                self.cursor = (self
                    .cursor
                    .saturating_add(c.try_into().unwrap_or(i64::MAX as u64)))
                .min(self_size);
            }
        }
        Ok(self.cursor)
    }
}

fn get_volume_name_opt<P: AsRef<Path>>(path: P) -> Option<String> {
    use std::path::Component::Normal;
    if let Some(Normal(n)) = path.as_ref().components().next() {
        if let Some(s) = n.to_str() {
            if s.ends_with(':') {
                return Some(s.replace(':', ""));
            }
        }
    }
    None
}

#[allow(dead_code)]
fn get_volume_name<P: AsRef<Path>>(path: P) -> String {
    get_volume_name_opt(path).unwrap_or_else(|| "atom".to_string())
}

fn transform_into_read_dir<'a>(path: &Path, fs_entries: &[FsEntry<'a>]) -> crate::ReadDir {
    let entries = fs_entries
        .iter()
        .map(|e| crate::DirEntry {
            path: path.join(&*e.text),
            metadata: Ok(crate::Metadata {
                ft: translate_file_type(e.fs_type),
                accessed: 0,
                created: 0,
                modified: 0,
                len: e.get_len(),
            }),
        })
        .collect();

    crate::ReadDir::new(entries)
}

impl<T> FileSystem for WebcFileSystem<T>
where
    T: std::fmt::Debug + Send + Sync + 'static,
    T: Deref<Target = WebC<'static>>,
{
    fn read_dir(&self, path: &Path) -> Result<ReadDir, FsError> {
        let path = normalizes_path(path);
        let read_dir_result = self
            .webc
            .read_dir(&self.package, &path)
            .map(|o| transform_into_read_dir(Path::new(&path), o.as_ref()))
            .map_err(|_| FsError::EntityNotFound);

        match read_dir_result {
            Ok(o) => Ok(o),
            Err(_) => self.memory.read_dir(Path::new(&path)),
        }
    }
    fn create_dir(&self, path: &Path) -> Result<(), FsError> {
        let path = normalizes_path(path);
        let result = self.memory.create_dir(Path::new(&path));
        result
    }
    fn remove_dir(&self, path: &Path) -> Result<(), FsError> {
        let path = normalizes_path(path);
        let result = self.memory.remove_dir(Path::new(&path));
        if self.webc.get_file_entry(&self.package, &path).is_some() {
            Ok(())
        } else {
            result
        }
    }
    fn rename(&self, from: &Path, to: &Path) -> Result<(), FsError> {
        let from = normalizes_path(from);
        let to = normalizes_path(to);
        let result = self.memory.rename(Path::new(&from), Path::new(&to));
        if self.webc.get_file_entry(&self.package, &from).is_some() {
            Ok(())
        } else {
            result
        }
    }
    fn metadata(&self, path: &Path) -> Result<Metadata, FsError> {
        let path = normalizes_path(path);
        if let Some(fs_entry) = self.webc.get_file_entry(&self.package, &path) {
            Ok(Metadata {
                ft: translate_file_type(FsEntryType::File),
                accessed: 0,
                created: 0,
                modified: 0,
                len: fs_entry.1.get_len(),
            })
        } else if self.webc.read_dir(&self.package, &path).is_ok() {
            Ok(Metadata {
                ft: translate_file_type(FsEntryType::Dir),
                accessed: 0,
                created: 0,
                modified: 0,
                len: 0,
            })
        } else {
            self.memory.metadata(Path::new(&path))
        }
    }
    fn remove_file(&self, path: &Path) -> Result<(), FsError> {
        let path = normalizes_path(path);
        let result = self.memory.remove_file(Path::new(&path));
        if self.webc.get_file_entry(&self.package, &path).is_some() {
            Ok(())
        } else {
            result
        }
    }
    fn new_open_options(&self) -> OpenOptions {
        OpenOptions::new(Box::new(WebCFileOpener {
            package: self.package.clone(),
            webc: self.webc.clone(),
            memory: self.memory.clone(),
        }))
    }
    fn symlink_metadata(&self, path: &Path) -> Result<Metadata, FsError> {
        let path = normalizes_path(path);
        if let Some(fs_entry) = self.webc.get_file_entry(&self.package, &path) {
            Ok(Metadata {
                ft: translate_file_type(FsEntryType::File),
                accessed: 0,
                created: 0,
                modified: 0,
                len: fs_entry.1.get_len(),
            })
        } else if self.webc.read_dir(&self.package, &path).is_ok() {
            Ok(Metadata {
                ft: translate_file_type(FsEntryType::Dir),
                accessed: 0,
                created: 0,
                modified: 0,
                len: 0,
            })
        } else {
            self.memory.symlink_metadata(Path::new(&path))
        }
    }
}

fn normalizes_path(path: &Path) -> String {
    let path = format!("{}", path.display());
    if !path.starts_with('/') {
        format!("/{path}")
    } else {
        path
    }
}

fn translate_file_type(f: FsEntryType) -> crate::FileType {
    crate::FileType {
        dir: f == FsEntryType::Dir,
        file: f == FsEntryType::File,
        symlink: false,
        char_device: false,
        block_device: false,
        socket: false,
        fifo: false,
    }
}
