// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright © 2024 Mike Robeson [dijksterhuis]

use std::path::Path;

use ot_tools_io::identifiers::ArrId;
use ot_tools_io::identifiers::SavedState;
use ot_tools_io::ArrangementFile;
use ot_tools_io::IsDefault;

use crate::projects::DataItem;
use crate::projects::R;

/// Alias of a DataFile container for the Bank type
pub type ArrDataFile = DataItem<ArrangementFile>;

impl ArrDataFile {
    pub fn create(dirpath: &Path, id: &ArrId) -> Self {
        let path = dirpath
            .join(id.file_stem(&SavedState::Working))
            .to_path_buf();
        let data = ArrangementFile::default();
        Self::new(path, data)
    }

    pub fn load(dirpath: &Path, id: &ArrId, state: &SavedState) -> R<Self> {
        let path = dirpath.join(id.file_stem(state)).to_path_buf();
        ArrDataFile::read(path)
    }

    pub fn to_new(&self, new_dirpath: &Path, id: &ArrId, state: &SavedState) -> R<Self> {
        let path = new_dirpath.join(id.file_stem(state)).to_path_buf();
        let mut new = self.clone();
        new.source = path;
        Ok(new)
    }

    pub fn is_default(&self) -> R<bool> {
        Ok(self.data.is_default())
    }
}
