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

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

use crate::marks::Line;

#[derive(Clone, Debug)]
pub struct TBarOptions {
  /// 起始x坐标
  pub x: i32,
  /// 起始y坐标
  pub y: i32,
  /// 线宽度
  pub width: i32,
  /// 高度
  pub height: i32,
  /// 实线 虚线:(默认实线)
  pub line: Line,
}

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

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

impl From<(i32, i32, i32, i32)> for TBar {
  fn from((x, y, width, height): (i32, i32, i32, i32)) -> Self {
    Self::new(TBarOptions {
      x,
      y,
      width,
      height,
      line: Line::SolidLine,
    })
  }
}

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

impl PsdkArgBase<String> for TBar {
  fn header(&self) -> String {
    "BAR".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.width)
      .append(self.options.height)
      .append(self.options.line.command_value());
    Ok(ptc.clause())
  }
}

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