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 TCircleOptions {
  ///  圆形框左上角x坐标
  pub x: i32,
  ///  圆形框左上角y坐标
  pub y: i32,
  /// 圆形框线宽
  pub width: i32,
  /// 圆形框半径
  pub radius: i32,
}

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

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

impl From<(i32, i32, i32, i32)> for TCircle {
  fn from((x, y, width, radius): (i32, i32, i32, i32)) -> Self {
    Self::new(TCircleOptions {
      x,
      y,
      width,
      radius,
    })
  }
}

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

impl PsdkArgBase<String> for TCircle {
  fn header(&self) -> String {
    "CIRCLE".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.radius)
      .append(self.options.width);
    Ok(ptc.clause())
  }
}

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