//! Yaneurao Itufuru Archive File (.scd)
use crate::ext::io::*;
use crate::scripts::base::*;
use crate::types::*;
use crate::utils::encoding::encode_string;
use crate::utils::struct_pack::*;
use crate::utils::xored_stream::XoredStream as Crypto;
use anyhow::Result;
use msg_tool_macro::*;
use std::collections::HashMap;
use std::io::{Read, Seek, SeekFrom, Write};
use std::sync::{Arc, Mutex};

#[derive(Debug)]
/// Yaneurao Itufuru Archive Builder
pub struct ItufuruArchiveBuilder {}

impl ItufuruArchiveBuilder {
    /// Creates a new instance of `ItufuruArchiveBuilder`
    pub const fn new() -> Self {
        ItufuruArchiveBuilder {}
    }
}

impl ScriptBuilder for ItufuruArchiveBuilder {
    fn default_encoding(&self) -> Encoding {
        Encoding::Cp932
    }

    fn default_archive_encoding(&self) -> Option<Encoding> {
        Some(Encoding::Cp932)
    }

    fn build_script(
        &self,
        data: Vec<u8>,
        _filename: &str,
        _encoding: Encoding,
        archive_encoding: Encoding,
        config: &ExtraConfig,
        _archive: Option<&Box<dyn Script>>,
    ) -> Result<Box<dyn Script + Send + Sync>> {
        Ok(Box::new(ItufuruArchive::new(
            MemReader::new(data),
            archive_encoding,
            config,
        )?))
    }

    fn build_script_from_file(
        &self,
        filename: &str,
        _encoding: Encoding,
        archive_encoding: Encoding,
        config: &ExtraConfig,
        _archive: Option<&Box<dyn Script>>,
    ) -> Result<Box<dyn Script + Send + Sync>> {
        if filename == "-" {
            let data = crate::utils::files::read_file(filename)?;
            Ok(Box::new(ItufuruArchive::new(
                MemReader::new(data),
                archive_encoding,
                config,
            )?))
        } else {
            let f = std::fs::File::open(filename)?;
            let reader = std::io::BufReader::new(f);
            Ok(Box::new(ItufuruArchive::new(
                reader,
                archive_encoding,
                config,
            )?))
        }
    }

    fn build_script_from_reader<'a>(
        &self,
        reader: Box<dyn ReadSeek + Send + Sync + 'a>,
        _filename: &str,
        _encoding: Encoding,
        archive_encoding: Encoding,
        config: &ExtraConfig,
        _archive: Option<&Box<dyn Script>>,
    ) -> Result<Box<dyn Script + Send + Sync + 'a>> {
        Ok(Box::new(ItufuruArchive::new(
            reader,
            archive_encoding,
            config,
        )?))
    }

    fn extensions(&self) -> &'static [&'static str] {
        &["scd"]
    }

    fn is_this_format(&self, _filename: &str, buf: &[u8], buf_len: usize) -> Option<u8> {
        if buf_len >= 4 && buf.starts_with(b"SCR\0") {
            Some(1)
        } else {
            None
        }
    }

    fn script_type(&self) -> &'static ScriptType {
        &ScriptType::YaneuraoItufuruArc
    }

    fn is_archive(&self) -> bool {
        true
    }

    fn create_archive(
        &self,
        filename: &str,
        files: &[&str],
        encoding: Encoding,
        config: &ExtraConfig,
    ) -> Result<Box<dyn Archive>> {
        let f = std::fs::File::create(filename)?;
        let writer = std::io::BufWriter::new(f);
        let archive = ItufuruArchiveWriter::new(writer, files, encoding, config)?;
        Ok(Box::new(archive))
    }
}

#[derive(Debug, StructPack, StructUnpack)]
struct ItufuruFileHeader {
    #[fstring = 12]
    file_name: String,
    offset: u32,
}

#[derive(Debug, StructPack)]
struct CustomHeader {
    #[fstring = 12]
    file_name: String,
    offset: u32,
    #[skip_pack]
    size: u32,
}

struct Entry {
    name: String,
    data: MemReader,
}

impl Read for Entry {
    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
        self.data.read(buf)
    }
}

impl ArchiveContent for Entry {
    fn name(&self) -> &str {
        &self.name
    }

    fn script_type(&self) -> Option<&ScriptType> {
        Some(&ScriptType::YaneuraoItufuru)
    }

    fn data(&mut self) -> Result<Vec<u8>> {
        Ok(self.data.data.clone())
    }

    fn to_data<'a>(&'a mut self) -> Result<Box<dyn ReadSeek + Send + Sync + 'a>> {
        Ok(Box::new(&mut self.data))
    }
}

#[derive(Debug)]
/// Yaneurao Itufuru Archive Script
pub struct ItufuruArchive<'b, T: Read + Seek + std::fmt::Debug + 'b> {
    reader: Arc<Mutex<Crypto<T>>>,
    first_file_offset: u32,
    files: Vec<CustomHeader>,
    _mark: std::marker::PhantomData<&'b ()>,
}

impl<'b, T: Read + Seek + std::fmt::Debug + 'b> ItufuruArchive<'b, T> {
    /// Creates a new `ItufuruArchive`
    ///
    /// * `reader` - The reader to read the archive data from
    /// * `archive_encoding` - The encoding used for the archive
    /// * `config` - Extra configuration options
    pub fn new(mut reader: T, archive_encoding: Encoding, _config: &ExtraConfig) -> Result<Self> {
        let mut header = [0u8; 4];
        reader.read_exact(&mut header)?;
        if &header != b"SCR\0" {
            return Err(anyhow::anyhow!("Invalid Itufuru archive header"));
        }
        let file_count = reader.read_u32()?;
        let first_file_offset = reader.read_u32()?;
        reader.read_u32()?; // Skip unused field
        let mut reader = Crypto::new(reader, 0xA5);
        let mut tfiles = Vec::with_capacity(file_count as usize);
        for _ in 0..file_count {
            let file = ItufuruFileHeader::unpack(&mut reader, false, archive_encoding, &None)?;
            tfiles.push(file);
        }
        let mut files = Vec::with_capacity(tfiles.len());
        if !tfiles.is_empty() {
            for i in 0..tfiles.len() - 1 {
                let file = CustomHeader {
                    file_name: tfiles[i].file_name.clone(),
                    offset: tfiles[i].offset,
                    size: tfiles[i + 1].offset - tfiles[i].offset,
                };
                files.push(file);
            }
            let last_file = &tfiles[tfiles.len() - 1];
            let file = CustomHeader {
                file_name: last_file.file_name.clone(),
                offset: last_file.offset,
                size: reader.seek(SeekFrom::End(0))? as u32 - last_file.offset - first_file_offset,
            };
            files.push(file);
        }
        Ok(ItufuruArchive {
            reader: Arc::new(Mutex::new(reader)),
            first_file_offset,
            files,
            _mark: std::marker::PhantomData,
        })
    }
}

impl<'b, T: Read + Seek + std::fmt::Debug + 'b> Script for ItufuruArchive<'b, T> {
    fn default_output_script_type(&self) -> OutputScriptType {
        OutputScriptType::Json
    }

    fn default_format_type(&self) -> FormatOptions {
        FormatOptions::None
    }

    fn is_archive(&self) -> bool {
        true
    }

    fn iter_archive_filename<'a>(
        &'a self,
    ) -> Result<Box<dyn Iterator<Item = Result<String>> + 'a>> {
        Ok(Box::new(
            self.files.iter().map(|s| Ok(s.file_name.to_owned())),
        ))
    }

    fn iter_archive_offset<'a>(&'a self) -> Result<Box<dyn Iterator<Item = Result<u64>> + 'a>> {
        Ok(Box::new(self.files.iter().map(|s| Ok(s.offset as u64))))
    }

    fn open_file<'a>(&'a self, index: usize) -> Result<Box<dyn ArchiveContent + Send + Sync + 'a>> {
        if index >= self.files.len() {
            return Err(anyhow::anyhow!(
                "Index out of bounds: {} (max: {})",
                index,
                self.files.len()
            ));
        }
        let entry = &self.files[index];
        let file_offset = entry.offset as u64;
        match self.reader.cpeek_exact_at_vec(
            file_offset + self.first_file_offset as u64,
            entry.size as usize,
        ) {
            Ok(data) => {
                let name = entry.file_name.clone();
                Ok(Box::new(Entry {
                    name,
                    data: MemReader::new(data),
                }))
            }
            Err(e) => Err(anyhow::anyhow!(
                "Failed to read file {}: {}",
                entry.file_name,
                e
            )),
        }
    }
}

/// Archive Writer for Itufuru Archive
pub struct ItufuruArchiveWriter<T: Write + Seek> {
    writer: T,
    headers: HashMap<String, CustomHeader>,
    first_file_offset: u32,
    encoding: Encoding,
}

impl<T: Write + Seek> ItufuruArchiveWriter<T> {
    /// Creates a new `ItufuruArchiveWriter`
    ///
    /// * `writer` - The writer to write the archive data to
    /// * `files` - List of file names to include in the archive
    /// * `encoding` - The encoding used for the archive
    /// * `config` - Extra configuration options
    pub fn new(
        mut writer: T,
        files: &[&str],
        encoding: Encoding,
        _config: &ExtraConfig,
    ) -> Result<Self> {
        writer.write_all(b"SCR\0")?;
        let file_count = files.len() as u32;
        writer.write_u32(file_count)?;
        let first_file_offset = 0x10 + file_count * 16; // 16 bytes per file header
        writer.write_u32(first_file_offset)?;
        writer.write_u32(0)?; // Unused field
        let mut headers = HashMap::new();
        for file in files {
            headers.insert(
                file.to_string(),
                CustomHeader {
                    file_name: file.to_string(),
                    offset: 0,
                    size: 0,
                },
            );
        }
        let mut crypto = Crypto::new(&mut writer, 0xA5);
        for (_, header) in headers.iter() {
            header.pack(&mut crypto, false, encoding, &None)?;
        }
        Ok(ItufuruArchiveWriter {
            writer,
            headers,
            first_file_offset,
            encoding,
        })
    }
}

impl<T: Write + Seek> Archive for ItufuruArchiveWriter<T> {
    fn new_file<'a>(
        &'a mut self,
        name: &str,
        _size: Option<u64>,
    ) -> Result<Box<dyn WriteSeek + 'a>> {
        let entry = self
            .headers
            .get_mut(name)
            .ok_or_else(|| anyhow::anyhow!("File '{}' not found in archive", name))?;
        if entry.size != 0 {
            return Err(anyhow::anyhow!("File '{}' already exists in archive", name));
        }
        entry.offset = self.writer.stream_position()? as u32 - self.first_file_offset;
        Ok(Box::new(ItufuruArchiveWriterEntry::new(
            &mut self.writer,
            entry,
            self.first_file_offset,
        )))
    }
    fn write_header(&mut self) -> Result<()> {
        let mut crypto = Crypto::new(&mut self.writer, 0xA5);
        let mut entries = self.headers.values().collect::<Vec<_>>();
        entries.sort_by_key(|h| h.offset);
        crypto.seek(SeekFrom::Start(16))?;
        for entry in entries.iter() {
            entry.pack(&mut crypto, false, self.encoding, &None)?;
        }
        Ok(())
    }
}

/// File Writer for Itufuru Archive
pub struct ItufuruArchiveWriterEntry<'a, T: Write + Seek> {
    writer: Crypto<&'a mut T>,
    header: &'a mut CustomHeader,
    first_file_offset: u32,
    pos: usize,
}

impl<'a, T: Write + Seek> ItufuruArchiveWriterEntry<'a, T> {
    fn new(writer: &'a mut T, header: &'a mut CustomHeader, first_file_offset: u32) -> Self {
        let writer = Crypto::new(writer, 0xA5);
        ItufuruArchiveWriterEntry {
            writer,
            header,
            first_file_offset,
            pos: 0,
        }
    }
}

impl<'a, T: Write + Seek> Write for ItufuruArchiveWriterEntry<'a, T> {
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        self.writer.seek(SeekFrom::Start(
            self.header.offset as u64 + self.first_file_offset as u64 + self.pos as u64,
        ))?;
        let written = self.writer.write(buf)?;
        self.pos += written;
        self.header.size = self.header.size.max(self.pos as u32);
        Ok(written)
    }

    fn flush(&mut self) -> std::io::Result<()> {
        self.writer.flush()
    }
}

impl<'a, T: Write + Seek> Seek for ItufuruArchiveWriterEntry<'a, T> {
    fn seek(&mut self, pos: SeekFrom) -> std::io::Result<u64> {
        let new_pos = match pos {
            SeekFrom::Start(offset) => offset as usize,
            SeekFrom::End(offset) => {
                if offset < 0 {
                    if (-offset) as usize > self.header.size as usize {
                        return Err(std::io::Error::new(
                            std::io::ErrorKind::InvalidInput,
                            "Seek from end exceeds file length",
                        ));
                    }
                    self.header.size as usize - (-offset) as usize
                } else {
                    self.header.size as usize + offset as usize
                }
            }
            SeekFrom::Current(offset) => {
                if offset < 0 {
                    if (-offset) as usize > self.pos {
                        return Err(std::io::Error::new(
                            std::io::ErrorKind::InvalidInput,
                            "Seek from current exceeds current position",
                        ));
                    }
                    self.pos.saturating_sub((-offset) as usize)
                } else {
                    self.pos + offset as usize
                }
            }
        };
        self.pos = new_pos;
        Ok(self.pos as u64)
    }

    fn stream_position(&mut self) -> std::io::Result<u64> {
        Ok(self.pos as u64)
    }
}
