// Code generated by fixer-gen. DO NOT EDIT.
#![allow(clippy::new_without_default)]
#![allow(clippy::needless_pass_by_value)]
#![allow(clippy::too_many_arguments)]
#![allow(unused_imports)]

use fixer::message::Message;
use fixer::fix_string::FIXString;
use fixer::errors::MessageRejectErrorEnum;
use fixer::session::session_id::SessionID;
{% if needs_decimal %}
use rust_decimal::Decimal;
{% endif %}
{% if needs_timestamp %}
use jiff::Timestamp;
{% endif %}
use crate::field;
use crate::tag;

/// `{{ name }}` is the `{{ fix_package }}` `{{ name }}` type, `MsgType` = {{ msg_type }}.
pub struct {{ name }} {
    pub message: Message,
}

impl {{ name }} {
    /// Creates a new `{{ name }}` with required fields.
    pub fn new({% for f in required_fields %}{{ f.snake_name }}: field::{{ f.name }}Field{% if not loop.last %}, {% endif %}{% endfor %}) -> Self {
        let mut msg = Message::new();
        msg.header.set_field(tag::MSG_TYPE, FIXString::from("{{ msg_type }}".to_string()));
{% for f in required_fields %}
        msg.body.set_field(tag::{{ f.const_name }}, {{ f.snake_name }}.0);
{% endfor %}
        Self { message: msg }
    }

    /// Creates a `{{ name }}` from an existing `Message`.
    pub fn from_message(msg: Message) -> Self {
        Self { message: msg }
    }

    /// Returns the underlying `Message`.
    pub fn to_message(self) -> Message {
        self.message
    }

{% for f in fields %}
{% if f.is_group %}
    // TODO: group setter/getter for {{ f.name }}
{% else %}
{% if f.fixer_type == "FIXString" %}
    /// Sets `{{ f.name }}`, Tag {{ f.tag }}.
    pub fn set_{{ f.snake_name }}(&mut self, v: String) {
        self.message.body.set_field(tag::{{ f.const_name }}, FIXString::from(v));
    }

    /// Gets `{{ f.name }}`, Tag {{ f.tag }}.
    pub fn get_{{ f.snake_name }}(&self) -> Result<String, MessageRejectErrorEnum> {
        let mut fld = field::{{ f.name }}Field::new(String::new());
        self.message.body.get_field(tag::{{ f.const_name }}, &mut fld.0)?;
        Ok(fld.value().to_string())
    }
{% elif f.fixer_type == "FIXBoolean" %}
    /// Sets `{{ f.name }}`, Tag {{ f.tag }}.
    pub fn set_{{ f.snake_name }}(&mut self, v: bool) {
        self.message.body.set_field(tag::{{ f.const_name }}, fixer::fix_boolean::FIXBoolean::from(v));
    }

    /// Gets `{{ f.name }}`, Tag {{ f.tag }}.
    pub fn get_{{ f.snake_name }}(&self) -> Result<bool, MessageRejectErrorEnum> {
        let mut fld = field::{{ f.name }}Field::new(false);
        self.message.body.get_field(tag::{{ f.const_name }}, &mut fld.0)?;
        Ok(fld.value())
    }
{% elif f.fixer_type == "FIXInt" %}
    /// Sets `{{ f.name }}`, Tag {{ f.tag }}.
    pub fn set_{{ f.snake_name }}(&mut self, v: isize) {
        self.message.body.set_field(tag::{{ f.const_name }}, fixer::fix_int::FIXInt::from(v));
    }

    /// Gets `{{ f.name }}`, Tag {{ f.tag }}.
    pub fn get_{{ f.snake_name }}(&self) -> Result<isize, MessageRejectErrorEnum> {
        let mut fld = field::{{ f.name }}Field::new(0);
        self.message.body.get_field(tag::{{ f.const_name }}, &mut fld.0)?;
        Ok(fld.value())
    }
{% elif f.fixer_type == "FIXFloat" %}
    /// Sets `{{ f.name }}`, Tag {{ f.tag }}.
    pub fn set_{{ f.snake_name }}(&mut self, v: f64) {
        self.message.body.set_field(tag::{{ f.const_name }}, fixer::fix_float::FIXFloat::from(v));
    }

    /// Gets `{{ f.name }}`, Tag {{ f.tag }}.
    pub fn get_{{ f.snake_name }}(&self) -> Result<f64, MessageRejectErrorEnum> {
        let mut fld = field::{{ f.name }}Field::new(0.0);
        self.message.body.get_field(tag::{{ f.const_name }}, &mut fld.0)?;
        Ok(fld.value())
    }
{% elif f.fixer_type == "FIXDecimal" %}
    /// Sets `{{ f.name }}`, Tag {{ f.tag }}.
    pub fn set_{{ f.snake_name }}(&mut self, val: Decimal, scale: i32) {
        self.message.body.set_field(tag::{{ f.const_name }}, fixer::fix_decimal::FIXDecimal { decimal: val, scale });
    }

    /// Gets `{{ f.name }}`, Tag {{ f.tag }}.
    pub fn get_{{ f.snake_name }}(&self) -> Result<Decimal, MessageRejectErrorEnum> {
        let mut fld = field::{{ f.name }}Field::new(Decimal::ZERO, 0);
        self.message.body.get_field(tag::{{ f.const_name }}, &mut fld.0)?;
        Ok(fld.value())
    }
{% elif f.fixer_type == "FIXUTCTimestamp" %}
    /// Sets `{{ f.name }}`, Tag {{ f.tag }}.
    pub fn set_{{ f.snake_name }}(&mut self, v: Timestamp) {
        self.message.body.set_field(tag::{{ f.const_name }}, fixer::fix_utc_timestamp::FIXUTCTimestamp {
            time: v,
            precision: fixer::fix_utc_timestamp::TimestampPrecision::Millis,
        });
    }

    /// Gets `{{ f.name }}`, Tag {{ f.tag }}.
    pub fn get_{{ f.snake_name }}(&self) -> Result<Timestamp, MessageRejectErrorEnum> {
        let mut fld = field::{{ f.name }}Field::new(Timestamp::UNIX_EPOCH);
        self.message.body.get_field(tag::{{ f.const_name }}, &mut fld.0)?;
        Ok(fld.value())
    }
{% endif %}

    /// Returns true if `{{ f.name }}` is present, Tag {{ f.tag }}.
    pub fn has_{{ f.snake_name }}(&self) -> bool {
        self.message.body.has(tag::{{ f.const_name }})
    }
{% endif %}
{% endfor %}
}

/// `RouteOut` is the callback type for routing `{{ name }}` messages.
pub type RouteOut = fn(msg: {{ name }}, session_id: SessionID) -> Result<(), MessageRejectErrorEnum>;

/// Route type returned by the `route` function.
pub type Route = (&'static str, &'static str, Box<dyn Fn(&Message, SessionID) -> Result<(), MessageRejectErrorEnum> + Send>);

/// Returns the begin string, message type, and route function for `{{ name }}`.
pub fn route(router: RouteOut) -> Route {
    let r = move |msg: &Message, session_id: SessionID| -> Result<(), MessageRejectErrorEnum> {
        router({{ name }}::from_message(msg.clone()), session_id)
    };
    ("{{ router_begin_string }}", "{{ msg_type }}", Box::new(r))
}
