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

#[derive(Clone, Debug)]
pub struct TDmatrixOptions {
  /// 条码起始x坐标
  pub x: i32,
  /// 条码起始y坐标
  pub y: i32,
  /// 条码宽度
  pub width: i32,
  /// 条码高度
  pub height: i32,
  /// 条码内容
  pub content: String,
}

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

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

impl<C: Into<String>> From<(i32, i32, i32, i32, C)> for TDmatrix {
  fn from((x, y, width, height, content): (i32, i32, i32, i32, C)) -> Self {
    Self::new(TDmatrixOptions {
      x,
      y,
      width,
      height,
      content: content.into(),
    })
  }
}

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

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

  fn clause(&self) -> PsdkFatherResult<CommandClause> {
    let mut ptc = PsdkTextCommand::with_header_format_tspl(self.header());
    ptc
      .append(self.options.x)
      .append(self.options.y)
      .append(self.options.width)
      .append(self.options.height)
      .append_with_quote(&self.options.content);

    Ok(ptc.clause())
  }
}

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