//! Auto-generated PID 31004 types.
//! Stornorechnung
//! 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 Pid31004Sg1 {
    pub dtm: Option<OwnedSegment>,
    pub rff: Option<OwnedSegment>,
}

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

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

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

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

/// SG3 — Steuernummer, Umsatzsteuernummer
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Pid31004Sg3 {
    pub rff: Option<OwnedSegment>,
}

/// SG50 — Geldbetrag, Qualifier
/// Qualifiers: 113
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Pid31004Sg50113 {
    pub moa: Option<OwnedSegment>,
}

/// SG50 — Geldbetrag, Qualifier
/// Qualifiers: 77
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Pid31004Sg5077 {
    pub moa: Option<OwnedSegment>,
}

/// SG50 — Geldbetrag, Qualifier
/// Qualifiers: 9
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Pid31004Sg509 {
    pub moa: Option<OwnedSegment>,
}

/// SG50 — Geldbetrag, Qualifier
/// Qualifiers: Z01
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Pid31004Sg50Z01 {
    pub moa: Option<OwnedSegment>,
}

/// SG52 — Zoll-/Steuer-/Gebührenfunktion, Qualifier
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Pid31004Sg52 {
    pub moa: Option<OwnedSegment>,
    pub tax: Option<OwnedSegment>,
}

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

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

/// SG8 — Zahlungsbedingung, Qualifier
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Pid31004Sg8 {
    pub dtm: Option<OwnedSegment>,
    pub pyt: Option<OwnedSegment>,
}

/// PID 31004: Stornorechnung
/// Kommunikation: ReErst an ReEmpf
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Pid31004 {
    pub unh: OwnedSegment,
    pub bgm: OwnedSegment,
    pub dtm: OwnedSegment,
    pub dtm_2: OwnedSegment,
    pub dtm_3: OwnedSegment,
    pub dtm_4: OwnedSegment,
    pub dtm_5: OwnedSegment,
    pub dtm_6: OwnedSegment,
    pub dtm_7: OwnedSegment,
    pub imd: OwnedSegment,
    pub ftx: OwnedSegment,
    pub uns: OwnedSegment,
    pub unt: OwnedSegment,
    pub sg1: Vec<Pid31004Sg1>,
    pub sg2_ms: Vec<Pid31004Sg2Ms>,
    pub sg2_mr: Vec<Pid31004Sg2Mr>,
    pub sg2_dp: Vec<Pid31004Sg2Dp>,
    pub sg2_zsh: Vec<Pid31004Sg2Zsh>,
    pub sg50_77: Vec<Pid31004Sg5077>,
    pub sg50_113: Vec<Pid31004Sg50113>,
    pub sg50_z01: Vec<Pid31004Sg50Z01>,
    pub sg50_9: Vec<Pid31004Sg509>,
    pub sg52: Vec<Pid31004Sg52>,
    pub sg7: Vec<Pid31004Sg7>,
    pub sg8: Vec<Pid31004Sg8>,
}

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

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

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

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

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

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

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

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

impl Pid31004Sg52 {
    /// 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 moa = if peek_is(segments, cursor, "MOA") {
            Some(consume(segments, cursor)?.clone())
        } else {
            None
        };
        let tax = if peek_is(segments, cursor, "TAX") {
            Some(consume(segments, cursor)?.clone())
        } else {
            None
        };
        if moa.is_none() && tax.is_none() {
            cursor.restore(saved);
            return None;
        }
        Some(Self {
            moa,
            tax,
        })
    }
}

impl Pid31004Sg5Ic {
    /// 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 Pid31004Sg7 {
    /// 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 Pid31004Sg8 {
    /// 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 pyt = if peek_is(segments, cursor, "PYT") {
            Some(consume(segments, cursor)?.clone())
        } else {
            None
        };
        if dtm.is_none() && pyt.is_none() {
            cursor.restore(saved);
            return None;
        }
        Some(Self {
            dtm,
            pyt,
        })
    }
}

impl Pid31004 {
    /// 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 dtm_4 = expect_segment(segments, &mut cursor, "DTM")?.clone();
        let dtm_5 = expect_segment(segments, &mut cursor, "DTM")?.clone();
        let dtm_6 = expect_segment(segments, &mut cursor, "DTM")?.clone();
        let dtm_7 = expect_segment(segments, &mut cursor, "DTM")?.clone();
        let imd = expect_segment(segments, &mut cursor, "IMD")?.clone();
        let ftx = expect_segment(segments, &mut cursor, "FTX")?.clone();
        let uns = expect_segment(segments, &mut cursor, "UNS")?.clone();
        let unt = expect_segment(segments, &mut cursor, "UNT")?.clone();
        let mut sg1 = Vec::new();
        while let Some(item) = Pid31004Sg1::from_segments(segments, &mut cursor) {
            sg1.push(item);
        }
        let mut sg2_ms = Vec::new();
        while let Some(item) = Pid31004Sg2Ms::from_segments(segments, &mut cursor) {
            sg2_ms.push(item);
        }
        let mut sg2_mr = Vec::new();
        while let Some(item) = Pid31004Sg2Mr::from_segments(segments, &mut cursor) {
            sg2_mr.push(item);
        }
        let mut sg2_dp = Vec::new();
        while let Some(item) = Pid31004Sg2Dp::from_segments(segments, &mut cursor) {
            sg2_dp.push(item);
        }
        let mut sg2_zsh = Vec::new();
        while let Some(item) = Pid31004Sg2Zsh::from_segments(segments, &mut cursor) {
            sg2_zsh.push(item);
        }
        let mut sg50_77 = Vec::new();
        while let Some(item) = Pid31004Sg5077::from_segments(segments, &mut cursor) {
            sg50_77.push(item);
        }
        let mut sg50_113 = Vec::new();
        while let Some(item) = Pid31004Sg50113::from_segments(segments, &mut cursor) {
            sg50_113.push(item);
        }
        let mut sg50_z01 = Vec::new();
        while let Some(item) = Pid31004Sg50Z01::from_segments(segments, &mut cursor) {
            sg50_z01.push(item);
        }
        let mut sg50_9 = Vec::new();
        while let Some(item) = Pid31004Sg509::from_segments(segments, &mut cursor) {
            sg50_9.push(item);
        }
        let mut sg52 = Vec::new();
        while let Some(item) = Pid31004Sg52::from_segments(segments, &mut cursor) {
            sg52.push(item);
        }
        let mut sg7 = Vec::new();
        while let Some(item) = Pid31004Sg7::from_segments(segments, &mut cursor) {
            sg7.push(item);
        }
        let mut sg8 = Vec::new();
        while let Some(item) = Pid31004Sg8::from_segments(segments, &mut cursor) {
            sg8.push(item);
        }

        Ok(Pid31004 {
            unh,
            bgm,
            dtm,
            dtm_2,
            dtm_3,
            dtm_4,
            dtm_5,
            dtm_6,
            dtm_7,
            imd,
            ftx,
            uns,
            unt,
            sg1,
            sg2_ms,
            sg2_mr,
            sg2_dp,
            sg2_zsh,
            sg50_77,
            sg50_113,
            sg50_z01,
            sg50_9,
            sg52,
            sg7,
            sg8,
        })
    }
}
