use crate::{VectorValue, ffi};
use glib::translate::*;

impl VectorValue {
    #[doc(alias = "shumate_vector_value_get_string")]
    #[doc(alias = "get_string")]
    pub fn string(&self) -> Option<&glib::GStr> {
        unsafe {
            let mut string = std::ptr::null();
            let ret = from_glib(ffi::shumate_vector_value_get_string(
                self.to_glib_none().0,
                &mut string,
            ));
            if ret {
                Some(glib::GStr::from_ptr(string))
            } else {
                None
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use glib::prelude::*;

    #[test]
    fn test_new_color() {
        let color = gdk::RGBA::WHITE;
        let new_color = gdk::RGBA::BLACK;
        let mut value = VectorValue::new_color(&color);

        assert_eq!(value.color(), Some(color));

        value.set_color(&new_color);
        assert_eq!(color, gdk::RGBA::WHITE); // check the gdk::RGBA was not mutated
        assert_eq!(value.color(), Some(new_color));

        value.unset();

        assert!(value.is_null());
        assert!(value.color().is_none());
    }

    #[test]
    fn test_from_value() {
        let number = 42.0_f64;
        let new_number = 1.0_f64;
        let number_val = number.to_value();
        let mut value = VectorValue::from_value(&number_val);

        assert_eq!(value.number(), Some(number));

        value.set_number(new_number);
        assert_eq!(number_val.get::<f64>(), Ok(number)); // check the GValue was not mutated
        assert_eq!(value.number(), Some(new_number));

        value.unset();

        assert!(value.is_null());
        assert!(value.number().is_none());
    }

    #[test]
    fn test_new_string() {
        let string = "hello world";
        let new_string = "bye world";
        let mut value = VectorValue::new_string(string);

        assert_eq!(value.string().unwrap(), string);

        value.set_string(new_string);
        assert_eq!(string, "hello world"); // check the string was not mutated
        assert_eq!(value.string().unwrap(), new_string);

        value.unset();

        assert!(value.is_null());
        assert!(value.string().is_none());
    }
}
