//! Auto-generated PID 27001 types.
//! Übermittlung der Ausgleichs-energiepreise
//! 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 Pid27001Sg1 {
    pub rff: Option<OwnedSegment>,
}

/// SG17 — Produktgruppen-Art, Code
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Pid27001Sg17 {
    pub pgi: Option<OwnedSegment>,
    pub sg36: Vec<Pid27001Sg36>,
}

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

/// SG36 — Positionsnummer
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Pid27001Sg36 {
    pub lin: Option<OwnedSegment>,
    pub sg40: Vec<Pid27001Sg40>,
}

/// SG40 — Preis, Qualifier
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Pid27001Sg40 {
    pub dtm: Option<OwnedSegment>,
    pub pri: Option<OwnedSegment>,
}

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

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

/// PID 27001: Übermittlung der Ausgleichs-energiepreise
/// Kommunikation: BIKO an BKV
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Pid27001 {
    pub unh: OwnedSegment,
    pub bgm: OwnedSegment,
    pub dtm: OwnedSegment,
    pub unt: OwnedSegment,
    pub sg1: Vec<Pid27001Sg1>,
    pub sg17: Vec<Pid27001Sg17>,
    pub sg2: Vec<Pid27001Sg2>,
    pub sg6: Vec<Pid27001Sg6>,
}

impl Pid27001Sg1 {
    /// 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 Pid27001Sg17 {
    /// 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 pgi = if peek_is(segments, cursor, "PGI") {
            Some(consume(segments, cursor)?.clone())
        } else {
            None
        };
        if pgi.is_none() {
            cursor.restore(saved);
            return None;
        }
        let mut sg36 = Vec::new();
        while let Some(item) = Pid27001Sg36::from_segments(segments, cursor) {
            sg36.push(item);
        }
        Some(Self {
            pgi,
            sg36,
        })
    }
}

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

impl Pid27001Sg36 {
    /// 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
        };
        if lin.is_none() {
            cursor.restore(saved);
            return None;
        }
        let mut sg40 = Vec::new();
        while let Some(item) = Pid27001Sg40::from_segments(segments, cursor) {
            sg40.push(item);
        }
        Some(Self {
            lin,
            sg40,
        })
    }
}

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

impl Pid27001Sg4Ic {
    /// 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 Pid27001Sg6 {
    /// 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 Pid27001 {
    /// 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 unt = expect_segment(segments, &mut cursor, "UNT")?.clone();
        let mut sg1 = Vec::new();
        while let Some(item) = Pid27001Sg1::from_segments(segments, &mut cursor) {
            sg1.push(item);
        }
        let mut sg17 = Vec::new();
        while let Some(item) = Pid27001Sg17::from_segments(segments, &mut cursor) {
            sg17.push(item);
        }
        let mut sg2 = Vec::new();
        while let Some(item) = Pid27001Sg2::from_segments(segments, &mut cursor) {
            sg2.push(item);
        }
        let mut sg6 = Vec::new();
        while let Some(item) = Pid27001Sg6::from_segments(segments, &mut cursor) {
            sg6.push(item);
        }

        Ok(Pid27001 {
            unh,
            bgm,
            dtm,
            unt,
            sg1,
            sg17,
            sg2,
            sg6,
        })
    }
}
