Documentation
use std::sync::{Arc, Mutex};

use psdk_father::{
  args::{ArgContext, PsdkArgBase, PsdkEasyArg},
  command::{CommandClause, PsdkTextCommand},
  errors::PsdkFatherResult,
};

use crate::marks::Font;

#[derive(Clone, Debug)]
pub struct CMagOptions {
  /// 字体(默认TSS16)
  pub font: Option<Font>,
}

#[derive(Clone, Debug)]
pub struct CMag {
  options: CMagOptions,
  ctx: Arc<Mutex<ArgContext>>,
}

impl CMag {
  pub fn new(options: CMagOptions) -> Self {
    Self {
      options,
      ctx: Default::default(),
    }
  }
}

impl Default for CMag {
  fn default() -> Self {
    Self::new(CMagOptions { font: None })
  }
}

impl From<Font> for CMag {
  fn from(font: Font) -> Self {
    Self::new(CMagOptions { font: Some(font) })
  }
}

impl PsdkEasyArg<String> for CMag {
  fn context(&self) -> Arc<Mutex<ArgContext>> {
    self.ctx.clone()
  }
}

impl PsdkArgBase<String> for CMag {
  fn header(&self) -> String {
    "SETMAG".to_string()
  }

  fn clause(&self) -> PsdkFatherResult<CommandClause> {
    let mut ptc = PsdkTextCommand::with_header_format_cpcl(self.header());
    let font = self.options.font.clone().unwrap_or(Font::Tss16);
    ptc.append(font.get_ex()).append(font.get_ey());
    Ok(ptc.clause())
  }
}

psdk_father::impl_psdk_arg_to_easy_arg!(String, CMag, true);