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

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

#[derive(Clone, Debug)]
pub struct CLineOptions {
  /// 线条宽度
  pub line_width: i32,
  /// 线条左上角x坐标
  pub start_x: i32,
  /// 线条左上角y坐标
  pub start_y: i32,
  /// 线条右下角x坐标
  pub end_x: i32,
  /// 线条右下角y坐标
  pub end_y: i32,
  /// 实线:true  虚线:false(默认实线)
  pub is_solid_line: bool,
}

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

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

impl From<(i32, i32, i32, i32, i32)> for CLine {
  fn from((line_width, start_x, start_y, end_x, end_y): (i32, i32, i32, i32, i32)) -> Self {
    Self::new(CLineOptions {
      line_width,
      start_x,
      start_y,
      end_x,
      end_y,
      is_solid_line: true,
    })
  }
}

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

impl PsdkArgBase<String> for CLine {
  fn header(&self) -> String {
    if self.options.is_solid_line {
      "L".to_string()
    } else {
      "LP".to_string()
    }
  }

  fn clause(&self) -> PsdkFatherResult<CommandClause> {
    let mut ptc = PsdkTextCommand::with_header_format_cpcl(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.line_width + 1);
    Ok(ptc.clause())
  }
}

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