//! Auto-generated PID 13010 types.
//! normiertes Profil
//! 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
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Pid13010Sg1 {
    pub rff: Option<OwnedSegment>,
}

/// SG10 — Menge, Qualifier
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Pid13010Sg10 {
    pub dtm: Option<OwnedSegment>,
    pub qty: Option<OwnedSegment>,
}

/// SG2 — Beteiligter, Qualifier
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Pid13010Sg2 {
    pub nad: Option<OwnedSegment>,
    pub sg4_ic: Vec<Pid13010Sg4Ic>,
}

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

/// SG5 — Beteiligter, Qualifier
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Pid13010Sg5 {
    pub nad: Option<OwnedSegment>,
    pub sg6_z04: Vec<Pid13010Sg6Z04>,
}

/// SG6 — Ortsangabe, Qualifier
/// Qualifiers: Z04
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Pid13010Sg6Z04 {
    pub dtm: Option<OwnedSegment>,
    pub loc: Option<OwnedSegment>,
    pub sg9: Vec<Pid13010Sg9>,
}

/// SG9
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Pid13010Sg9 {
    pub lin: Option<OwnedSegment>,
    pub pia: Option<OwnedSegment>,
    pub sg10: Vec<Pid13010Sg10>,
}

/// PID 13010: normiertes Profil
/// Kommunikation: 
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Pid13010 {
    pub unb: OwnedSegment,
    pub unh: OwnedSegment,
    pub bgm: OwnedSegment,
    pub dtm: OwnedSegment,
    pub uns: OwnedSegment,
    pub unt: OwnedSegment,
    pub unz: OwnedSegment,
    pub sg1: Vec<Pid13010Sg1>,
    pub sg2: Vec<Pid13010Sg2>,
    pub sg5: Vec<Pid13010Sg5>,
}

impl Pid13010Sg1 {
    /// 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 Pid13010Sg10 {
    /// 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 dtm = if peek_is(segments, cursor, "DTM") {
            Some(consume(segments, cursor)?.clone())
        } else {
            None
        };
        let qty = if peek_is(segments, cursor, "QTY") {
            Some(consume(segments, cursor)?.clone())
        } else {
            None
        };
        if dtm.is_none() && qty.is_none() {
            cursor.restore(saved);
            return None;
        }
        Some(Self {
            dtm,
            qty,
        })
    }
}

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

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

impl Pid13010Sg6Z04 {
    /// 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 dtm = if peek_is(segments, cursor, "DTM") {
            Some(consume(segments, cursor)?.clone())
        } else {
            None
        };
        let loc = if peek_is(segments, cursor, "LOC") {
            Some(consume(segments, cursor)?.clone())
        } else {
            None
        };
        if dtm.is_none() && loc.is_none() {
            cursor.restore(saved);
            return None;
        }
        let mut sg9 = Vec::new();
        while let Some(item) = Pid13010Sg9::from_segments(segments, cursor) {
            sg9.push(item);
        }
        Some(Self {
            dtm,
            loc,
            sg9,
        })
    }
}

impl Pid13010Sg9 {
    /// 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;
        }
        let mut sg10 = Vec::new();
        while let Some(item) = Pid13010Sg10::from_segments(segments, cursor) {
            sg10.push(item);
        }
        Some(Self {
            lin,
            pia,
            sg10,
        })
    }
}

impl Pid13010 {
    /// 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 unb = expect_segment(segments, &mut cursor, "UNB")?.clone();
        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 uns = expect_segment(segments, &mut cursor, "UNS")?.clone();
        let unt = expect_segment(segments, &mut cursor, "UNT")?.clone();
        let unz = expect_segment(segments, &mut cursor, "UNZ")?.clone();
        let mut sg1 = Vec::new();
        while let Some(item) = Pid13010Sg1::from_segments(segments, &mut cursor) {
            sg1.push(item);
        }
        let mut sg2 = Vec::new();
        while let Some(item) = Pid13010Sg2::from_segments(segments, &mut cursor) {
            sg2.push(item);
        }
        let mut sg5 = Vec::new();
        while let Some(item) = Pid13010Sg5::from_segments(segments, &mut cursor) {
            sg5.push(item);
        }

        Ok(Pid13010 {
            unb,
            unh,
            bgm,
            dtm,
            uns,
            unt,
            unz,
            sg1,
            sg2,
            sg5,
        })
    }
}
