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

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

#[derive(Clone, Debug)]
pub struct TPageOptions {
  pub width: i32,
  pub height: i32,
}

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

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

impl From<(i32, i32)> for TPage {
  fn from((width, height): (i32, i32)) -> Self {
    Self::new(TPageOptions { width, height })
  }
}

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

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

  fn clause(&self) -> PsdkFatherResult<CommandClause> {
    let mut ptc = PsdkTextCommand::with_header_format_tspl(self.header());
    ptc
      .append(self.options.width.to_string() + " mm")
      .append(self.options.height.to_string() + " mm");
    Ok(ptc.clause())
  }
}

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