use crate::{
    element::BPMNElement,
    elements::{
        intermediate_catch_event::BPMNIntermediateCatchEvent,
        message_intermediate_catch_event::BPMNMessageIntermediateCatchEvent,
        timer_intermediate_catch_event::BPMNTimerIntermediateCatchEvent,
    },
    parser::{
        parser::NameSpace,
        parser_state::ParserState,
        parser_traits::{Closeable, Openable, Recognisable},
        tags::{OpenedTag, Tag},
    },
};
use anyhow::Result;
use quick_xml::events::{BytesEnd, BytesStart};

pub(crate) struct TagIntermediateCatchEvent {}

impl Recognisable for TagIntermediateCatchEvent {
    fn recognise_tag(e: &BytesStart, state: &ParserState, n: NameSpace) -> Option<Tag>
    where
        Self: Sized,
    {
        if n.is_bpmn() {
            match state.open_tags.iter().last() {
                Some(OpenedTag::Process { .. }) | Some(OpenedTag::SubProcess { .. }) => {
                    if e.local_name().as_ref() == b"intermediateCatchEvent" {
                        return Some(Tag::IntermediateCatchEvent);
                    }
                }
                _ => {}
            }
            None
        } else {
            None
        }
    }
}

impl Openable for TagIntermediateCatchEvent {
    fn open_tag(_tag: Tag, e: &BytesStart, state: &mut ParserState) -> Result<OpenedTag>
    where
        Self: Sized,
    {
        let (index, id) = state.read_and_add_id(e)?;

        Ok(OpenedTag::IntermediateCatchEvent {
            global_index: index,
            id,
            message_marker_id: None,
            timer_marker_id: None,
        })
    }
}

impl Closeable for TagIntermediateCatchEvent {
    fn close_tag(opened_tag: OpenedTag, _e: &BytesEnd, state: &mut ParserState) -> Result<()> {
        match state.open_tags.iter_mut().last() {
            Some(OpenedTag::Process { elements, .. })
            | Some(OpenedTag::SubProcess { elements, .. }) => {
                if let OpenedTag::IntermediateCatchEvent {
                    global_index,
                    id,
                    message_marker_id,
                    timer_marker_id,
                } = opened_tag
                {
                    let local_index = elements.len();
                    match (message_marker_id, timer_marker_id) {
                        (None, None) => {
                            //no marker
                            elements.push(BPMNElement::IntermediateCatchEvent(
                                BPMNIntermediateCatchEvent {
                                    global_index,
                                    id,
                                    local_index,
                                    incoming_sequence_flows: vec![],
                                    outgoing_sequence_flows: vec![],
                                },
                            ));
                        }
                        (None, Some(timer_marker_id)) => {
                            //timer marker
                            elements.push(BPMNElement::TimerIntermediateCatchEvent(
                                BPMNTimerIntermediateCatchEvent {
                                    global_index,
                                    id,
                                    local_index,
                                    timer_marker_id,
                                    incoming_sequence_flows: vec![],
                                    outgoing_sequence_flows: vec![],
                                },
                            ));
                        }
                        (Some(message_marker_id), None) => {
                            //message marker
                            elements.push(BPMNElement::MessageIntermediateCatchEvent(
                                BPMNMessageIntermediateCatchEvent {
                                    global_index,
                                    id,
                                    local_index,
                                    message_marker_id,
                                    incoming_sequence_flows: vec![],
                                    outgoing_sequence_flows: vec![],
                                    incoming_message_flow: None,
                                },
                            ));
                        }
                        (Some(_), Some(_)) => todo!(),
                    }
                    Ok(())
                } else {
                    unreachable!()
                }
            }
            _ => unreachable!(),
        }
    }
}
