
use super::{Instrument,IInstr};
pub struct CMW500{
    pub instr:Instrument,
    tdd:Vec<usize>,
}

impl CMW500 {
    pub fn new(instr:Instrument)->Self{
        Self { instr,tdd: vec![33,34,35,36,37,38,39,40,41,42,43,44,45,48,250] }
    }
    pub fn init_instr(&mut self)->std::io::Result<()> {
        self.instr.m_send("*RST; *OPC?")?;
        Ok(())
    }
    pub fn set_sched(&mut self,sched:((&str,&str,&str),(&str,&str,&str)))->std::io::Result<()> {
        self.instr.m_send("CONFigure:LTE:SIGN:CONNection:STYPe UDCH; OPC?")?;
        std::thread::sleep(std::time::Duration::from_millis(200));
        self.instr.m_send(&format!("CONFigure:LTE:SIGN:CONNection:UDCHannels:DL {},KEEP,{},{}; OPC?",sched.0.0,sched.0.1,sched.0.2))?;
        self.instr.m_send(&format!("CONFigure:LTE:SIGN:CONNection:UDCHannels:UL {},KEEP,{},{}; OPC?",sched.1.0,sched.1.1,sched.1.2))?;
        Ok(())
    }

    pub fn set_band(&mut self,band:usize)->std::io::Result<()>{
        self.instr.m_send(&format!("CONFigure:LTE:SIGN:DMODe {}; OPC?",self.lte_mode(&band)))?;
        self.instr.m_send(&format!("CONFigure:LTE:SIGN:BAND OB{}; OPC?",band))?;
        Ok(())
    }
    pub fn lte_mode(&self,band:&usize)->&'static str{
        if self.tdd.contains(&band){
            "TDD"
        }else{
            "FDD"
        }
    }

    pub fn set_width(&mut self,width:usize)->std::io::Result<()>{
        self.instr.m_send(&format!("CONF:LTE:SIGN:CELL:BAND:PCC:DL {}; OPC?",width))?;
        Ok(())
    }

    pub fn set_loss(&mut self,tx_loss:f64,rx_loss:f64)->std::io::Result<()>{
        self.instr.m_send(&format!("CONFigure:LTE:SIGN:RFSettings:EATTenuation:INPut {:.2}; OPC?",tx_loss))?;
        self.instr.m_send(&format!("CONFigure:LTE:SIGN:RFSettings:EATTenuation:OUTPut {:.2}; OPC?",rx_loss))?;
        Ok(())
    }

    pub fn set_udc(&mut self,udc:usize)->std::io::Result<()>{
        self.instr.m_send(&format!("CONFigure:LTE:SIGN:CELL:ULDL {}",udc))?;
        Ok(())
    }

    pub fn keep_rrc_connection(&mut self,onoff:bool)->std::io::Result<()> {
        self.instr.m_send(&format!("CONFigure:LTE:SIGN:CONNection:KRRC {}",if onoff{"ON"}else{"OFF"}))
    }

    pub fn get_bler(&mut self)->std::io::Result<f64>{
        self.instr.m_send("STOP:LTE:SIGN:EBLer")?;
        std::thread::sleep(std::time::Duration::from_millis(200));
        self.instr.m_send("CONFigure:LTE:SIGN:EBLer:REPetition CONT")?;
        self.instr.m_send("INIT:LTE:SIGN:EBLer")?;
        std::thread::sleep(std::time::Duration::from_millis(200));
        if let Some(dd) = self.instr.m_query("FETCh:LTE:SIGN:EBLer:RELative?"){
            if let Some(sd) = dd.split(",").collect::<Vec<&str>>().get(3){
                if let Ok(dds) = sd.parse::<f64>(){
                    return Ok(dds);
                }
            };
        }
        Err(std::io::ErrorKind::InvalidData.into())
    }

    pub fn set_data_aplication(&mut self)->std::io::Result<()>{
        self.instr.m_send("CONFigure:LTE:SIGN:CONNection:CTYPe DAPPlication; OPC?")?;
        Ok(())
    }

    pub fn sign_on(&mut self)->std::io::Result<()>{
        self.instr.m_send("SOURce:LTE:SIGN:CELL:STATe ON; OPC?")?;
        Ok(())
    }

    pub fn set_pow(&mut self,max_min:bool)->std::io::Result<()> {
        if max_min{
            self.instr.m_send("CONF:LTE:SIGN:UL:PUSC:TPC:SET MAXPower; OPC?")?;
        }else{
            self.instr.m_send("CONFigure:LTE:SIGN:UL:PUSCh:TPC:SET CLOop; OPC?")?;
        }
        // std::thread::sleep(std::time::Duration::from_millis(200));
        Ok(())
    }
    pub fn set_epre(&mut self,epre:f64)->std::io::Result<()>{
        self.instr.m_send(&format!("CONFigure:LTE:SIGN:DL:RSEPre:LEVel {:.1}",epre))
    }
    
    pub fn do_hand(&mut self,band:usize,width:usize)->std::io::Result<()> {
        self.instr.m_send("PREP:LTE:HAND:DEST \"LTE Sig\"; OPC?")?;
        self.instr.m_send(&format!("PREP:LTE:SIGN:HAND:ENH {},OB{},KEEP,B{}0,KEEP; OPC?",self.lte_mode(&band),band,width))?;
        self.instr.m_send("CALL:LTE:SIGN:PSW:ACT HAND; OPC?")
    }
}
