objets_metier_rs 1.0.2

Bibliothèque Rust moderne et sûre pour l'API COM Objets Métier Sage 100c - Production Ready
use crate::com::{SafeDispatch, SafeVariant};
use crate::errors::SageResult;
use windows::Win32::System::Com::IDispatch;

/// ✅ IMPLÉMENTATION ENRICHIE - Version Phase 6
///
/// Wrapper pour gammes de produits (IBSCIALGamme3)
///
/// Les gammes permettent de décliner un article selon différentes caractéristiques
/// (couleurs, tailles, finitions, etc.). Chaque gamme contient des énumérés qui
/// représentent les valeurs possibles.
///
/// # Exemple
/// ```no_run
/// use objets_metier_rs::wrappers::cial::CialApplication;
///
/// let cial_app = CialApplication::new("Objets100c.CIAL")?;
/// let factory = cial_app.factory_gamme()?;
///
/// let gamme = factory.create()?;
/// gamme.set_intitule("Couleurs")?;
/// gamme.set_type_gamme(1)?;  // Gamme enumérée
/// gamme.set_longueur_enumere(20)?;
/// gamme.set_n_numero(1)?;
/// gamme.write()?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub struct Gamme {
    pub(crate) dispatch: IDispatch,
}

impl Gamme {
    fn dispatch(&self) -> SafeDispatch<'_> {
        SafeDispatch::new(&self.dispatch)
    }

    // ═══════════════════════════════════════════════════════════════════════
    // PROPRIÉTÉS DE BASE (3 propriétés)
    // ═══════════════════════════════════════════════════════════════════════

    /// Identifiant unique (cbMarq)
    pub fn cbmarq(&self) -> SageResult<i32> {
        self.dispatch().call_method_by_name("cbMarq", &[])?.to_i32()
    }

    /// Intitulé de la gamme
    pub fn intitule(&self) -> SageResult<String> {
        self.dispatch()
            .call_method_by_name("Intitule", &[])?
            .to_string()
    }

    /// Numéro de la gamme (1 ou 2)
    pub fn n_numero(&self) -> SageResult<i32> {
        self.dispatch()
            .call_method_by_name("nNumero", &[])?
            .to_i32()
    }

    // ═══════════════════════════════════════════════════════════════════════
    // PROPRIÉTÉS DE CONFIGURATION (5 propriétés)
    // ═══════════════════════════════════════════════════════════════════════

    /// Type de gamme (0=Champ libre, 1=Enuméré)
    pub fn type_gamme(&self) -> SageResult<i16> {
        self.dispatch()
            .call_method_by_name("TypeGamme", &[])?
            .to_short()
    }

    /// Longueur de l'énuméré (si type enuméré)
    pub fn longueur_enumere(&self) -> SageResult<i32> {
        self.dispatch()
            .call_method_by_name("LongueurEnumere", &[])?
            .to_i32()
    }

    /// Nombre d'énumérés dans la gamme
    pub fn nb_enumeres(&self) -> SageResult<i32> {
        let enums = self.gamme_enumeres()?;
        let dispatch = enums.to_dispatch()?;
        let safe_dispatch = SafeDispatch::new(&dispatch);
        safe_dispatch.call_method_by_name("Count", &[])?.to_i32()
    }

    /// Actif (gamme utilisable)
    pub fn actif(&self) -> SageResult<bool> {
        self.dispatch().call_method_by_name("Actif", &[])?.to_bool()
    }

    /// Obligatoire (gamme requise pour l'article)
    pub fn obligatoire(&self) -> SageResult<bool> {
        self.dispatch()
            .call_method_by_name("Obligatoire", &[])?
            .to_bool()
    }

    // ═══════════════════════════════════════════════════════════════════════
    // COLLECTIONS (1 collection)
    // ═══════════════════════════════════════════════════════════════════════

    /// Collection des énumérés de la gamme
    pub fn gamme_enumeres(&self) -> SageResult<SafeVariant> {
        self.dispatch().call_method_by_name("GammeEnumeres", &[])
    }

    // ═══════════════════════════════════════════════════════════════════════
    // SETTERS (7 setters)
    // ═══════════════════════════════════════════════════════════════════════

    /// Modifie l'intitulé de la gamme
    pub fn set_intitule(&self, value: &str) -> SageResult<()> {
        let param = SafeVariant::from_string(value);
        self.dispatch().call_property_put("Intitule", &[param])?;
        Ok(())
    }

    /// Modifie le numéro de la gamme (1 ou 2)
    pub fn set_n_numero(&self, value: i32) -> SageResult<()> {
        let param = SafeVariant::I4(value);
        self.dispatch().call_property_put("nNumero", &[param])?;
        Ok(())
    }

    /// Modifie le type de gamme (0=Champ libre, 1=Enuméré)
    pub fn set_type_gamme(&self, type_gamme: i16) -> SageResult<()> {
        let param = SafeVariant::from_i16(type_gamme);
        self.dispatch().call_property_put("TypeGamme", &[param])?;
        Ok(())
    }

    /// Modifie la longueur de l'énuméré
    pub fn set_longueur_enumere(&self, longueur: i32) -> SageResult<()> {
        let param = SafeVariant::I4(longueur);
        self.dispatch()
            .call_property_put("LongueurEnumere", &[param])?;
        Ok(())
    }

    /// Active/désactive la gamme
    pub fn set_actif(&self, actif: bool) -> SageResult<()> {
        let param = SafeVariant::from_bool(actif);
        self.dispatch().call_property_put("Actif", &[param])?;
        Ok(())
    }

    /// Rend la gamme obligatoire ou optionnelle
    pub fn set_obligatoire(&self, obligatoire: bool) -> SageResult<()> {
        let param = SafeVariant::from_bool(obligatoire);
        self.dispatch().call_property_put("Obligatoire", &[param])?;
        Ok(())
    }

    // ═══════════════════════════════════════════════════════════════════════
    // MÉTHODES STANDARD (3 méthodes)
    // ═══════════════════════════════════════════════════════════════════════

    /// Enregistre les modifications
    pub fn write(&self) -> SageResult<()> {
        self.dispatch().call_method_by_name("Write", &[])?;
        Ok(())
    }

    /// Enregistre avec valeurs par défaut
    pub fn write_default(&self) -> SageResult<()> {
        self.dispatch().call_method_by_name("WriteDefault", &[])?;
        Ok(())
    }

    /// Supprime l'objet
    pub fn remove(&self) -> SageResult<()> {
        self.dispatch().call_method_by_name("Remove", &[])?;
        Ok(())
    }
}