//! Auto-generated PID 17011 types.
//! Bestellung Angebot Änderung Technik
//! Do not edit manually.
#![allow(unused_imports, non_camel_case_types)]

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

/// SG1 — Referenz, Qualifier
/// Qualifiers: AAG
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Pid17011Sg1Aag {
    pub rff: Option<OwnedSegment>,
}

/// SG1 — Referenz, Qualifier
/// Qualifiers: ACW
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Pid17011Sg1Acw {
    pub rff: Option<OwnedSegment>,
}

/// SG1 — Referenz, Qualifier
/// Qualifiers: Z13
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Pid17011Sg1Z13 {
    pub rff: Option<OwnedSegment>,
}

/// SG29 — Positionsnummer
/// Qualifiers: Z19
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Pid17011Sg29Z19 {
    pub lin: Option<OwnedSegment>,
    pub pia: Option<OwnedSegment>,
}

/// SG29 — Positionsnummer
/// Qualifiers: Z55
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Pid17011Sg29Z55 {
    pub lin: Option<OwnedSegment>,
    pub pia: Option<OwnedSegment>,
}

/// SG29 — Positionsnummer
/// Qualifiers: Z56
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Pid17011Sg29Z56 {
    pub lin: Option<OwnedSegment>,
    pub pia: Option<OwnedSegment>,
}

/// SG2 — Beteiligter, Qualifier
/// Qualifiers: DEB
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Pid17011Sg2Deb {
    pub nad: Option<OwnedSegment>,
}

/// SG2 — Beteiligter, Qualifier
/// Qualifiers: DP
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Pid17011Sg2Dp {
    pub loc: Option<OwnedSegment>,
    pub nad: Option<OwnedSegment>,
    pub sg3_z18: Vec<Pid17011Sg3Z18>,
    pub sg3_z37: Vec<Pid17011Sg3Z37>,
}

/// SG2 — Beteiligter, Qualifier
/// Qualifiers: MR
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Pid17011Sg2Mr {
    pub nad: Option<OwnedSegment>,
}

/// SG2 — Beteiligter, Qualifier
/// Qualifiers: MS
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Pid17011Sg2Ms {
    pub nad: Option<OwnedSegment>,
    pub sg5_ic: Vec<Pid17011Sg5Ic>,
}

/// SG2 — Beteiligter, Qualifier
/// Qualifiers: Z09
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Pid17011Sg2Z09 {
    pub nad: Option<OwnedSegment>,
    pub sg5_ic: Vec<Pid17011Sg5Ic>,
}

/// SG3 — Referenz, Qualifier
/// Qualifiers: Z18
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Pid17011Sg3Z18 {
    pub rff: Option<OwnedSegment>,
}

/// SG3 — Referenz, Qualifier
/// Qualifiers: Z37
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Pid17011Sg3Z37 {
    pub rff: Option<OwnedSegment>,
}

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

/// PID 17011: Bestellung Angebot Änderung Technik
/// Kommunikation: NB, LF an MSB
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Pid17011 {
    pub unh: OwnedSegment,
    pub bgm: OwnedSegment,
    pub dtm: OwnedSegment,
    pub dtm_2: OwnedSegment,
    pub dtm_3: OwnedSegment,
    pub uns: OwnedSegment,
    pub unt: OwnedSegment,
    pub sg1_acw: Vec<Pid17011Sg1Acw>,
    pub sg1_aag: Vec<Pid17011Sg1Aag>,
    pub sg1_z13: Vec<Pid17011Sg1Z13>,
    pub sg2_ms: Vec<Pid17011Sg2Ms>,
    pub sg2_mr: Vec<Pid17011Sg2Mr>,
    pub sg2_dp: Vec<Pid17011Sg2Dp>,
    pub sg2_z09: Vec<Pid17011Sg2Z09>,
    pub sg2_deb: Vec<Pid17011Sg2Deb>,
    pub sg29_z19: Vec<Pid17011Sg29Z19>,
    pub sg29_z55: Vec<Pid17011Sg29Z55>,
    pub sg29_z56: Vec<Pid17011Sg29Z56>,
}

impl Pid17011Sg1Aag {
    /// 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 rff = if peek_is(segments, cursor, "RFF") {
            Some(consume(segments, cursor)?.clone())
        } else {
            None
        };
        if rff.is_none() {
            cursor.restore(saved);
            return None;
        }
        Some(Self {
            rff,
        })
    }
}

impl Pid17011Sg1Acw {
    /// 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 rff = if peek_is(segments, cursor, "RFF") {
            Some(consume(segments, cursor)?.clone())
        } else {
            None
        };
        if rff.is_none() {
            cursor.restore(saved);
            return None;
        }
        Some(Self {
            rff,
        })
    }
}

impl Pid17011Sg1Z13 {
    /// 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 rff = if peek_is(segments, cursor, "RFF") {
            Some(consume(segments, cursor)?.clone())
        } else {
            None
        };
        if rff.is_none() {
            cursor.restore(saved);
            return None;
        }
        Some(Self {
            rff,
        })
    }
}

impl Pid17011Sg29Z19 {
    /// 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 lin = if peek_is(segments, cursor, "LIN") {
            Some(consume(segments, cursor)?.clone())
        } else {
            None
        };
        let pia = if peek_is(segments, cursor, "PIA") {
            Some(consume(segments, cursor)?.clone())
        } else {
            None
        };
        if lin.is_none() && pia.is_none() {
            cursor.restore(saved);
            return None;
        }
        Some(Self {
            lin,
            pia,
        })
    }
}

impl Pid17011Sg29Z55 {
    /// 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 lin = if peek_is(segments, cursor, "LIN") {
            Some(consume(segments, cursor)?.clone())
        } else {
            None
        };
        let pia = if peek_is(segments, cursor, "PIA") {
            Some(consume(segments, cursor)?.clone())
        } else {
            None
        };
        if lin.is_none() && pia.is_none() {
            cursor.restore(saved);
            return None;
        }
        Some(Self {
            lin,
            pia,
        })
    }
}

impl Pid17011Sg29Z56 {
    /// 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 lin = if peek_is(segments, cursor, "LIN") {
            Some(consume(segments, cursor)?.clone())
        } else {
            None
        };
        let pia = if peek_is(segments, cursor, "PIA") {
            Some(consume(segments, cursor)?.clone())
        } else {
            None
        };
        if lin.is_none() && pia.is_none() {
            cursor.restore(saved);
            return None;
        }
        Some(Self {
            lin,
            pia,
        })
    }
}

impl Pid17011Sg2Deb {
    /// 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;
        }
        Some(Self {
            nad,
        })
    }
}

impl Pid17011Sg2Dp {
    /// 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 loc = if peek_is(segments, cursor, "LOC") {
            Some(consume(segments, cursor)?.clone())
        } else {
            None
        };
        let nad = if peek_is(segments, cursor, "NAD") {
            Some(consume(segments, cursor)?.clone())
        } else {
            None
        };
        if loc.is_none() && nad.is_none() {
            cursor.restore(saved);
            return None;
        }
        let mut sg3_z18 = Vec::new();
        while let Some(item) = Pid17011Sg3Z18::from_segments(segments, cursor) {
            sg3_z18.push(item);
        }
        let mut sg3_z37 = Vec::new();
        while let Some(item) = Pid17011Sg3Z37::from_segments(segments, cursor) {
            sg3_z37.push(item);
        }
        Some(Self {
            loc,
            nad,
            sg3_z18,
            sg3_z37,
        })
    }
}

impl Pid17011Sg2Mr {
    /// 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;
        }
        Some(Self {
            nad,
        })
    }
}

impl Pid17011Sg2Ms {
    /// 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 sg5_ic = Vec::new();
        while let Some(item) = Pid17011Sg5Ic::from_segments(segments, cursor) {
            sg5_ic.push(item);
        }
        Some(Self {
            nad,
            sg5_ic,
        })
    }
}

impl Pid17011Sg2Z09 {
    /// 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 sg5_ic = Vec::new();
        while let Some(item) = Pid17011Sg5Ic::from_segments(segments, cursor) {
            sg5_ic.push(item);
        }
        Some(Self {
            nad,
            sg5_ic,
        })
    }
}

impl Pid17011Sg3Z18 {
    /// 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 rff = if peek_is(segments, cursor, "RFF") {
            Some(consume(segments, cursor)?.clone())
        } else {
            None
        };
        if rff.is_none() {
            cursor.restore(saved);
            return None;
        }
        Some(Self {
            rff,
        })
    }
}

impl Pid17011Sg3Z37 {
    /// 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 rff = if peek_is(segments, cursor, "RFF") {
            Some(consume(segments, cursor)?.clone())
        } else {
            None
        };
        if rff.is_none() {
            cursor.restore(saved);
            return None;
        }
        Some(Self {
            rff,
        })
    }
}

impl Pid17011Sg5Ic {
    /// 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 Pid17011 {
    /// 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 dtm_2 = expect_segment(segments, &mut cursor, "DTM")?.clone();
        let dtm_3 = expect_segment(segments, &mut cursor, "DTM")?.clone();
        let uns = expect_segment(segments, &mut cursor, "UNS")?.clone();
        let unt = expect_segment(segments, &mut cursor, "UNT")?.clone();
        let mut sg1_acw = Vec::new();
        while let Some(item) = Pid17011Sg1Acw::from_segments(segments, &mut cursor) {
            sg1_acw.push(item);
        }
        let mut sg1_aag = Vec::new();
        while let Some(item) = Pid17011Sg1Aag::from_segments(segments, &mut cursor) {
            sg1_aag.push(item);
        }
        let mut sg1_z13 = Vec::new();
        while let Some(item) = Pid17011Sg1Z13::from_segments(segments, &mut cursor) {
            sg1_z13.push(item);
        }
        let mut sg2_ms = Vec::new();
        while let Some(item) = Pid17011Sg2Ms::from_segments(segments, &mut cursor) {
            sg2_ms.push(item);
        }
        let mut sg2_mr = Vec::new();
        while let Some(item) = Pid17011Sg2Mr::from_segments(segments, &mut cursor) {
            sg2_mr.push(item);
        }
        let mut sg2_dp = Vec::new();
        while let Some(item) = Pid17011Sg2Dp::from_segments(segments, &mut cursor) {
            sg2_dp.push(item);
        }
        let mut sg2_z09 = Vec::new();
        while let Some(item) = Pid17011Sg2Z09::from_segments(segments, &mut cursor) {
            sg2_z09.push(item);
        }
        let mut sg2_deb = Vec::new();
        while let Some(item) = Pid17011Sg2Deb::from_segments(segments, &mut cursor) {
            sg2_deb.push(item);
        }
        let mut sg29_z19 = Vec::new();
        while let Some(item) = Pid17011Sg29Z19::from_segments(segments, &mut cursor) {
            sg29_z19.push(item);
        }
        let mut sg29_z55 = Vec::new();
        while let Some(item) = Pid17011Sg29Z55::from_segments(segments, &mut cursor) {
            sg29_z55.push(item);
        }
        let mut sg29_z56 = Vec::new();
        while let Some(item) = Pid17011Sg29Z56::from_segments(segments, &mut cursor) {
            sg29_z56.push(item);
        }

        Ok(Pid17011 {
            unh,
            bgm,
            dtm,
            dtm_2,
            dtm_3,
            uns,
            unt,
            sg1_acw,
            sg1_aag,
            sg1_z13,
            sg2_ms,
            sg2_mr,
            sg2_dp,
            sg2_z09,
            sg2_deb,
            sg29_z19,
            sg29_z55,
            sg29_z56,
        })
    }
}
