//! Auto-generated PID 33001 types.
//! Bestätigung
//! Do not edit manually.

use serde::{Deserialize, Serialize};
use crate::segment::OwnedSegment;
use crate::cursor::{SegmentCursor, SegmentNotFound, peek_is, consume, expect_segment};

/// SG1 — Beteiligter, Qualifier
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Pid33001Sg1 {
    pub nad: Option<OwnedSegment>,
    pub sg3_ic: Vec<Pid33001Sg3Ic>,
}

/// SG3 — Funktion des Ansprechpartners, Code
/// Qualifiers: IC
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Pid33001Sg3Ic {
    pub com: Option<OwnedSegment>,
    pub cta: Option<OwnedSegment>,
}

/// SG4 — Währungsverwendung, Qualifier
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Pid33001Sg4 {
    pub cux: Option<OwnedSegment>,
}

/// SG5 — Dokumentenname, Code
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Pid33001Sg5 {
    pub doc: Option<OwnedSegment>,
    pub dtm: Option<OwnedSegment>,
    pub moa: Option<OwnedSegment>,
}

/// PID 33001: Bestätigung
/// Kommunikation: ReEmpf an ReErst
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Pid33001 {
    pub unh: OwnedSegment,
    pub bgm: OwnedSegment,
    pub dtm: OwnedSegment,
    pub rff: OwnedSegment,
    pub uns: OwnedSegment,
    pub moa: OwnedSegment,
    pub unt: OwnedSegment,
    pub sg1: Vec<Pid33001Sg1>,
    pub sg4: Vec<Pid33001Sg4>,
    pub sg5: Vec<Pid33001Sg5>,
}

impl Pid33001Sg1 {
    /// Try to assemble this group from segments at the cursor position.
    pub fn from_segments(
        segments: &[OwnedSegment],
        cursor: &mut SegmentCursor,
    ) -> Option<Self> {
        let saved = cursor.save();
        let nad = if peek_is(segments, cursor, "NAD") {
            Some(consume(segments, cursor)?.clone())
        } else {
            None
        };
        if nad.is_none() {
            cursor.restore(saved);
            return None;
        }
        let mut sg3_ic = Vec::new();
        while let Some(item) = Pid33001Sg3Ic::from_segments(segments, cursor) {
            sg3_ic.push(item);
        }
        Some(Self {
            nad,
            sg3_ic,
        })
    }
}

impl Pid33001Sg3Ic {
    /// Try to assemble this group from segments at the cursor position.
    pub fn from_segments(
        segments: &[OwnedSegment],
        cursor: &mut SegmentCursor,
    ) -> Option<Self> {
        let saved = cursor.save();
        let com = if peek_is(segments, cursor, "COM") {
            Some(consume(segments, cursor)?.clone())
        } else {
            None
        };
        let cta = if peek_is(segments, cursor, "CTA") {
            Some(consume(segments, cursor)?.clone())
        } else {
            None
        };
        if com.is_none() && cta.is_none() {
            cursor.restore(saved);
            return None;
        }
        Some(Self {
            com,
            cta,
        })
    }
}

impl Pid33001Sg4 {
    /// Try to assemble this group from segments at the cursor position.
    pub fn from_segments(
        segments: &[OwnedSegment],
        cursor: &mut SegmentCursor,
    ) -> Option<Self> {
        let saved = cursor.save();
        let cux = if peek_is(segments, cursor, "CUX") {
            Some(consume(segments, cursor)?.clone())
        } else {
            None
        };
        if cux.is_none() {
            cursor.restore(saved);
            return None;
        }
        Some(Self {
            cux,
        })
    }
}

impl Pid33001Sg5 {
    /// Try to assemble this group from segments at the cursor position.
    pub fn from_segments(
        segments: &[OwnedSegment],
        cursor: &mut SegmentCursor,
    ) -> Option<Self> {
        let saved = cursor.save();
        let doc = if peek_is(segments, cursor, "DOC") {
            Some(consume(segments, cursor)?.clone())
        } else {
            None
        };
        let dtm = if peek_is(segments, cursor, "DTM") {
            Some(consume(segments, cursor)?.clone())
        } else {
            None
        };
        let moa = if peek_is(segments, cursor, "MOA") {
            Some(consume(segments, cursor)?.clone())
        } else {
            None
        };
        if doc.is_none() && dtm.is_none() && moa.is_none() {
            cursor.restore(saved);
            return None;
        }
        Some(Self {
            doc,
            dtm,
            moa,
        })
    }
}

impl Pid33001 {
    /// Assemble this PID from a pre-tokenized segment list.
    pub fn from_segments(
        segments: &[OwnedSegment],
    ) -> Result<Self, SegmentNotFound> {
        let mut cursor = SegmentCursor::new(segments.len());

        let unh = expect_segment(segments, &mut cursor, "UNH")?.clone();
        let bgm = expect_segment(segments, &mut cursor, "BGM")?.clone();
        let dtm = expect_segment(segments, &mut cursor, "DTM")?.clone();
        let rff = expect_segment(segments, &mut cursor, "RFF")?.clone();
        let uns = expect_segment(segments, &mut cursor, "UNS")?.clone();
        let moa = expect_segment(segments, &mut cursor, "MOA")?.clone();
        let unt = expect_segment(segments, &mut cursor, "UNT")?.clone();
        let mut sg1 = Vec::new();
        while let Some(item) = Pid33001Sg1::from_segments(segments, &mut cursor) {
            sg1.push(item);
        }
        let mut sg4 = Vec::new();
        while let Some(item) = Pid33001Sg4::from_segments(segments, &mut cursor) {
            sg4.push(item);
        }
        let mut sg5 = Vec::new();
        while let Some(item) = Pid33001Sg5::from_segments(segments, &mut cursor) {
            sg5.push(item);
        }

        Ok(Pid33001 {
            unh,
            bgm,
            dtm,
            rff,
            uns,
            moa,
            unt,
            sg1,
            sg4,
            sg5,
        })
    }
}
