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

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

#[derive(Clone, Debug)]
pub struct TAutoDotOptions {
  /// 虚化 横向实线变成虚线
  pub enable: bool,
}

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

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

impl From<bool> for TAutoDot {
  fn from(enable: bool) -> Self {
    Self::new(TAutoDotOptions { enable })
  }
}

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

impl PsdkArgBase<String> for TAutoDot {
  fn header(&self) -> String {
    "SETC AUTODOTTED".to_string()
  }

  fn clause(&self) -> PsdkFatherResult<CommandClause> {
    let mut ptc = PsdkTextCommand::with_header_format_tspl(self.header());
    ptc.append(if self.options.enable { "ON" } else { "OFF" });
    Ok(ptc.clause())
  }
}

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