use std::string;

pub struct HandShake<'a> {
    len_pack: u8,
    len_dom: u8,
    port_pos: usize,
    datagram: &'a mut [u8],
    //host: &'a [u8],
    //port: &'a mut [u8],
}

impl<'a> HandShake<'a>{
    pub fn new(data: &'a mut[u8]) -> HandShake {
        let len_pack = data[0];
        println!("{}", len_pack);
        let len_dom = data[4];
        println!("{}", len_dom);
        HandShake {
            len_pack: len_pack,
            len_dom: len_dom,
            datagram: data,
            port_pos: (len_pack - 2) as usize,
            //host: (&mut data[5 .. ((len_dom + 5) as usize)]),
            //port: (&mut data[((len_pack - 2) as usize) .. ((len_pack - 1) as usize)]),
        }
    }

    pub fn getHostName(&self) -> String {
        String::from_utf8(self.datagram[5 .. ((self.len_dom+5) as usize)].to_vec()).unwrap()
    }

    pub fn get_port(&self) -> u16 {
        u16::from_be_bytes([(self.datagram[(self.len_pack - 2) as usize]),
         (self.datagram[(self.len_pack - 1) as usize])])
    }

    pub fn replace_port(&mut self, n_port: u16){
        let s_port = n_port.to_ne_bytes();
        self.datagram[self.port_pos]=s_port[0];
        self.datagram[self.port_pos+1]=s_port[1];
    }


    pub fn get_raw(&self) -> &[u8]{
        self.datagram
    }
}

