//
// types.rs
//
// Copyright (C) 2024 Posit Software, PBC. All rights reserved.
//
//

// Windows specific types required by R functions we need
//
// Generated by:
// - Cloning libR-sys and doing the Windows specific setup to be able to generate bindings
// - Adding `#define Win32` to `wrapper.h` (otherwise not everything Windows specific comes through)
// - Adding `R_ext/RStartup.h` to `wrapper.h`
// - `$env:R_HOME = "C:\Program Files\R\R-4.3.2"`
// - `$env:LIBRSYS_LIBCLANG_INCLUDE_PATH="$env:RTOOLS43_HOME\x86_64-w64-mingw32.static.posix\include"`
// - `cargo build --target x86_64-pc-windows-gnu --features use-bindgen`
// - `target/x86_64-pc-windows-gnu/debug/libR-sys-*/out/bindings.rs` holds the bindings, relevant parts were copied over here

#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#![allow(unused)]
#![warn(unused_unsafe)]

use crate::Rboolean;

// Start of copy from libR-sys `bindings.rs`

#[repr(C)]
#[derive(Copy, Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct __BindgenBitfieldUnit<Storage> {
    storage: Storage,
}
impl<Storage> __BindgenBitfieldUnit<Storage> {
    #[inline]
    pub const fn new(storage: Storage) -> Self {
        Self { storage }
    }
}
impl<Storage> __BindgenBitfieldUnit<Storage>
where
    Storage: AsRef<[u8]> + AsMut<[u8]>,
{
    #[inline]
    pub fn get_bit(&self, index: usize) -> bool {
        debug_assert!(index / 8 < self.storage.as_ref().len());
        let byte_index = index / 8;
        let byte = self.storage.as_ref()[byte_index];
        let bit_index = if cfg!(target_endian = "big") {
            7 - (index % 8)
        } else {
            index % 8
        };
        let mask = 1 << bit_index;
        byte & mask == mask
    }
    #[inline]
    pub fn set_bit(&mut self, index: usize, val: bool) {
        debug_assert!(index / 8 < self.storage.as_ref().len());
        let byte_index = index / 8;
        let byte = &mut self.storage.as_mut()[byte_index];
        let bit_index = if cfg!(target_endian = "big") {
            7 - (index % 8)
        } else {
            index % 8
        };
        let mask = 1 << bit_index;
        if val {
            *byte |= mask;
        } else {
            *byte &= !mask;
        }
    }
    #[inline]
    pub fn get(&self, bit_offset: usize, bit_width: u8) -> u64 {
        debug_assert!(bit_width <= 64);
        debug_assert!(bit_offset / 8 < self.storage.as_ref().len());
        debug_assert!((bit_offset + (bit_width as usize)) / 8 <= self.storage.as_ref().len());
        let mut val = 0;
        for i in 0..(bit_width as usize) {
            if self.get_bit(i + bit_offset) {
                let index = if cfg!(target_endian = "big") {
                    bit_width as usize - 1 - i
                } else {
                    i
                };
                val |= 1 << index;
            }
        }
        val
    }
    #[inline]
    pub fn set(&mut self, bit_offset: usize, bit_width: u8, val: u64) {
        debug_assert!(bit_width <= 64);
        debug_assert!(bit_offset / 8 < self.storage.as_ref().len());
        debug_assert!((bit_offset + (bit_width as usize)) / 8 <= self.storage.as_ref().len());
        for i in 0..(bit_width as usize) {
            let mask = 1 << i;
            let val_bit_is_set = val & mask == mask;
            let index = if cfg!(target_endian = "big") {
                bit_width as usize - 1 - i
            } else {
                i
            };
            self.set_bit(index + bit_offset, val_bit_is_set);
        }
    }
}

pub const RSTART_VERSION: u32 = 1;

pub const UImode_RGui: UImode = 0;
pub const UImode_RTerm: UImode = 1;
pub const UImode_LinkDLL: UImode = 2;
pub type UImode = u32;
#[doc = "= 0"]
pub const SA_TYPE_SA_NORESTORE: SA_TYPE = 0;
pub const SA_TYPE_SA_RESTORE: SA_TYPE = 1;
#[doc = "was === SA_RESTORE"]
pub const SA_TYPE_SA_DEFAULT: SA_TYPE = 2;
pub const SA_TYPE_SA_NOSAVE: SA_TYPE = 3;
pub const SA_TYPE_SA_SAVE: SA_TYPE = 4;
pub const SA_TYPE_SA_SAVEASK: SA_TYPE = 5;
pub const SA_TYPE_SA_SUICIDE: SA_TYPE = 6;
#[doc = "Startup Actions"]
pub type SA_TYPE = u32;
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct structRstart {
    pub R_Quiet: Rboolean,
    pub R_NoEcho: Rboolean,
    pub R_Interactive: Rboolean,
    pub R_Verbose: Rboolean,
    pub LoadSiteFile: Rboolean,
    pub LoadInitFile: Rboolean,
    pub DebugInitFile: Rboolean,
    pub RestoreAction: SA_TYPE,
    pub SaveAction: SA_TYPE,
    pub vsize: usize,
    pub nsize: usize,
    pub max_vsize: usize,
    pub max_nsize: usize,
    pub ppsize: usize,
    pub _bitfield_align_1: [u16; 0],
    pub _bitfield_1: __BindgenBitfieldUnit<[u8; 4usize]>,
    #[doc = "R_HOME"]
    pub rhome: *mut ::std::os::raw::c_char,
    #[doc = "HOME"]
    pub home: *mut ::std::os::raw::c_char,
    pub ReadConsole: ::std::option::Option<
        unsafe extern "C-unwind" fn(
            arg1: *const ::std::os::raw::c_char,
            arg2: *mut ::std::os::raw::c_uchar,
            arg3: ::std::os::raw::c_int,
            arg4: ::std::os::raw::c_int,
        ) -> ::std::os::raw::c_int,
    >,
    pub WriteConsole: ::std::option::Option<
        unsafe extern "C-unwind" fn(
            arg1: *const ::std::os::raw::c_char,
            arg2: ::std::os::raw::c_int,
        ),
    >,
    #[doc = "ProcessEvents under Unix"]
    pub CallBack: ::std::option::Option<unsafe extern "C-unwind" fn()>,
    pub ShowMessage:
        ::std::option::Option<unsafe extern "C-unwind" fn(arg1: *const ::std::os::raw::c_char)>,
    pub YesNoCancel: ::std::option::Option<
        unsafe extern "C-unwind" fn(arg1: *const ::std::os::raw::c_char) -> ::std::os::raw::c_int,
    >,
    #[doc = "Return value here is expected to be 1 for Yes, -1 for No and\n0 for Cancel: symbolic constants in graphapp.h"]
    pub Busy: ::std::option::Option<unsafe extern "C-unwind" fn(arg1: ::std::os::raw::c_int)>,
    pub CharacterMode: UImode,
    #[doc = "The following field has been added in R 2.5.0"]
    pub WriteConsoleEx: ::std::option::Option<
        unsafe extern "C-unwind" fn(
            arg1: *const ::std::os::raw::c_char,
            arg2: ::std::os::raw::c_int,
            arg3: ::std::os::raw::c_int,
        ),
    >,
    #[doc = "The following field has been added in R 4.0.0."]
    pub EmitEmbeddedUTF8: Rboolean,
    #[doc = "The following fields have been added in R 4.2.0 and are only\navailable with RstarVersion 1."]
    pub CleanUp: ::std::option::Option<
        unsafe extern "C-unwind" fn(
            arg1: SA_TYPE,
            arg2: ::std::os::raw::c_int,
            arg3: ::std::os::raw::c_int,
        ),
    >,
    pub ClearerrConsole: ::std::option::Option<unsafe extern "C-unwind" fn()>,
    pub FlushConsole: ::std::option::Option<unsafe extern "C-unwind" fn()>,
    pub ResetConsole: ::std::option::Option<unsafe extern "C-unwind" fn()>,
    pub Suicide:
        ::std::option::Option<unsafe extern "C-unwind" fn(s: *const ::std::os::raw::c_char)>,
}
impl structRstart {
    #[inline]
    pub fn NoRenviron(&self) -> Rboolean {
        unsafe { ::std::mem::transmute(self._bitfield_1.get(0usize, 16u8) as u32) }
    }
    #[inline]
    pub fn set_NoRenviron(&mut self, val: Rboolean) {
        unsafe {
            let val: u32 = ::std::mem::transmute(val);
            self._bitfield_1.set(0usize, 16u8, val as u64)
        }
    }
    #[inline]
    pub fn RstartVersion(&self) -> ::std::os::raw::c_int {
        u32::cast_signed(self._bitfield_1.get(16usize, 16u8) as u32)
    }
    #[inline]
    pub fn set_RstartVersion(&mut self, val: ::std::os::raw::c_int) {
        self._bitfield_1
            .set(16usize, 16u8, i32::cast_unsigned(val) as u64)
    }
    #[inline]
    pub fn new_bitfield_1(
        NoRenviron: Rboolean,
        RstartVersion: ::std::os::raw::c_int,
    ) -> __BindgenBitfieldUnit<[u8; 4usize]> {
        let mut __bindgen_bitfield_unit: __BindgenBitfieldUnit<[u8; 4usize]> = Default::default();
        __bindgen_bitfield_unit.set(0usize, 16u8, {
            let NoRenviron: u32 = unsafe { ::std::mem::transmute(NoRenviron) };
            NoRenviron as u64
        });
        __bindgen_bitfield_unit.set(16usize, 16u8, i32::cast_unsigned(RstartVersion) as u64);
        __bindgen_bitfield_unit
    }
}
pub type Rstart = *mut structRstart;
