use visa_rs::Instrument;
pub mod  p66319_d;

use crate::IInstr;
pub struct Power{
    ptype:Ptype,
    mhandle:Instrument,
    shandle:Option<Instrument>,
}
pub enum Ptype {
    Keithley,
    AgE36,
    Phh1506d,
    RigolDp800,
    Ag66,
}
pub enum Channel {
    One,
    Two,
}
impl Power {
    pub fn new(mut minstr:Instrument,sinstr:Option<Instrument>)->Result<Self,()>{

        let Some(dd) = minstr.m_get_describ() else {return Err(());};
        let mut div = false;
        if let Some(_) = sinstr{
            div = true;
        }
        if dd.contains("KEITHLEY"){
            return Ok(Self { ptype: Ptype::Keithley, mhandle: minstr ,shandle: sinstr });
        }else if dd.contains("E36"){
            return Ok(Self { ptype: Ptype::AgE36, mhandle: minstr ,shandle: sinstr });
        }
        else if dd.contains("PPH-1506D"){
            return Ok(Self { ptype: Ptype::Phh1506d, mhandle: minstr ,shandle: sinstr });
        }
        else if dd.contains("DP8"){
            return Ok(Self { ptype: Ptype::RigolDp800, mhandle: minstr ,shandle: sinstr });
        }
        else{
            return Ok(Self { ptype: Ptype::Ag66, mhandle: minstr ,shandle: sinstr  });
        }
    }
    pub fn setvolt(&mut self,channel:Channel,volt:f64){
        match channel {
            Channel::One => {
                let cmd = match self.ptype {
                    Ptype::Keithley => {format!("SOUR1:VOLT {}",volt)},
                    Ptype::Ag66 => {format!("SOUR:VOLT1 {}",volt)},
                    Ptype::Phh1506d|Ptype::RigolDp800 => {format!(":SOUR1:VOLT {}",volt)},
                    Ptype::AgE36 => {format!("VOLT {}",volt)},
                };
                self.mhandle.m_send(&cmd);
            },
            Channel::Two => {
                if let Some(sinstr) =&mut self.shandle{
                    let cmd = match self.ptype {
                        Ptype::Keithley => {format!("SOUR1:VOLT {}",volt)},
                        Ptype::Ag66 => {format!("SOUR:VOLT1 {}",volt)},
                        Ptype::Phh1506d|Ptype::RigolDp800 => {format!(":SOUR1:VOLT {}",volt)},
                        Ptype::AgE36 => {format!("VOLT {}",volt)},
                    };
                    sinstr.m_send(&cmd);
                }else{
                    let cmd = match self.ptype {
                        Ptype::Keithley => {format!("SOUR2:VOLT {}",volt)},
                        Ptype::Ag66 => {format!("SOUR:VOLT2 {}",volt)},
                        Ptype::Phh1506d|Ptype::RigolDp800 => {format!(":SOUR2:VOLT {}",volt)},
                        Ptype::AgE36 => {format!("VOLT {}",volt)},
                    };
                    self.mhandle.m_send(&cmd);
                }
                
            },
        }
    }
}