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

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

#[derive(Clone, Debug)]
pub struct TGapOptions {
  /// 是否定位到缝隙
  pub enable: bool,
}

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

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

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

impl PsdkArgBase<String> for TGap {
  fn header(&self) -> String {
    "SET GAP".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, TGap, true);
