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

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

#[derive(Clone, Debug)]
pub struct CPageWidthOptions {
  /// 打印区域宽度(单位dot)
  pub width: i32,
}

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

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

impl From<i32> for CPageWidth {
  fn from(width: i32) -> Self {
    Self::new(CPageWidthOptions { width })
  }
}

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

impl PsdkArgBase<String> for CPageWidth {
  fn header(&self) -> String {
    "PAGE-WIDTH".to_string()
  }

  fn clause(&self) -> PsdkFatherResult<CommandClause> {
    let mut ptc = PsdkTextCommand::with_header_format_cpcl(self.header());
    ptc.append(self.options.width);
    Ok(ptc.clause())
  }
}

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