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 TBoxOptions {
  /// 矩形框左上角x坐标
  pub start_x: i32,
  /// 矩形框左上角y坐标
  pub start_y: i32,
  /// 矩形框右下角x坐标
  pub end_x: i32,
  /// 矩形框右下角y坐标
  pub end_y: i32,
  /// 矩形框线条宽度
  pub width: i32,
  /// 矩形框圆角半径
  pub radius: Option<i32>,
}

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

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

impl From<(i32, i32, i32, i32, i32, i32)> for TBox {
  fn from((start_x, start_y, end_x, end_y, width, radius): (i32, i32, i32, i32, i32, i32)) -> Self {
    Self::new(TBoxOptions {
      start_x,
      start_y,
      end_x,
      end_y,
      width,
      radius: Some(radius),
    })
  }
}

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

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

  fn clause(&self) -> PsdkFatherResult<CommandClause> {
    let mut ptc = PsdkTextCommand::with_header_format_tspl(self.header());
    ptc
      .append(self.options.start_x)
      .append(self.options.start_y)
      .append(self.options.end_x)
      .append(self.options.end_y)
      .append(self.options.width)
      .append(self.options.radius.unwrap_or(0));
    Ok(ptc.clause())
  }
}

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