use std::marker::PhantomData;
use std::fmt;
use context::Context;
use object::{ToObject, Object};
use object;
use rvalue::{RValue, ToRValue};
use rvalue;
use lvalue::{LValue, ToLValue};
use lvalue;

use crate::with_lib;

/// Parameter represents a parameter to a function. A series of parameteres
/// can be combined to form a function signature.
#[derive(Copy, Clone, PartialEq)]
pub struct Parameter<'ctx> {
    marker: PhantomData<&'ctx Context<'ctx>>,
    ptr: *mut gccjit_sys::gcc_jit_param
}

impl<'ctx> ToObject<'ctx> for Parameter<'ctx> {
    fn to_object(&self) -> Object<'ctx> {
        with_lib(|lib| {
            unsafe {
                object::from_ptr(lib.gcc_jit_param_as_object(self.ptr))
            }
        })
    }
}

impl<'ctx> fmt::Debug for Parameter<'ctx> {
    fn fmt<'a>(&self, fmt: &mut fmt::Formatter<'a>) -> Result<(), fmt::Error> {
        let obj = self.to_object();
        obj.fmt(fmt)
    }
}

impl<'ctx> ToRValue<'ctx> for Parameter<'ctx> {
    fn to_rvalue(&self) -> RValue<'ctx> {
        with_lib(|lib| {
            unsafe {
                let ptr = lib.gcc_jit_param_as_rvalue(self.ptr);
                rvalue::from_ptr(ptr)
            }
        })
    }
}

impl<'ctx> ToLValue<'ctx> for Parameter<'ctx> {
    fn to_lvalue(&self) -> LValue<'ctx> {
        with_lib(|lib| {
            unsafe {
                let ptr = lib.gcc_jit_param_as_lvalue(self.ptr);
                lvalue::from_ptr(ptr)
            }
        })
    }
}


pub unsafe fn from_ptr<'ctx>(ptr: *mut gccjit_sys::gcc_jit_param) -> Parameter<'ctx> {
    Parameter {
        marker: PhantomData,
        ptr
    }
}

pub unsafe fn get_ptr<'ctx>(loc: &Parameter<'ctx>) -> *mut gccjit_sys::gcc_jit_param {
    loc.ptr
}

