use std::borrow::Borrow;
use std::cell::UnsafeCell;
use std::ops::{Deref, DerefMut};
use std::ptr;
use crate::context::blocked::BlockedClient;
use crate::{raw, Context, ValkeyResult};
pub struct ValkeyGILGuardScope<'ctx, 'mutex, T, G: ValkeyLockIndicator> {
    _context: &'ctx G,
    mutex: &'mutex ValkeyGILGuard<T>,
}
impl<'ctx, 'mutex, T, G: ValkeyLockIndicator> Deref for ValkeyGILGuardScope<'ctx, 'mutex, T, G> {
    type Target = T;
    fn deref(&self) -> &Self::Target {
        unsafe { &*self.mutex.obj.get() }
    }
}
impl<'ctx, 'mutex, T, G: ValkeyLockIndicator> DerefMut for ValkeyGILGuardScope<'ctx, 'mutex, T, G> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        unsafe { &mut *self.mutex.obj.get() }
    }
}
pub unsafe trait ValkeyLockIndicator {}
pub struct ValkeyGILGuard<T> {
    obj: UnsafeCell<T>,
}
impl<T> ValkeyGILGuard<T> {
    pub fn new(obj: T) -> ValkeyGILGuard<T> {
        ValkeyGILGuard {
            obj: UnsafeCell::new(obj),
        }
    }
    pub fn lock<'mutex, 'ctx, G: ValkeyLockIndicator>(
        &'mutex self,
        context: &'ctx G,
    ) -> ValkeyGILGuardScope<'ctx, 'mutex, T, G> {
        ValkeyGILGuardScope {
            _context: context,
            mutex: self,
        }
    }
}
impl<T: Default> Default for ValkeyGILGuard<T> {
    fn default() -> Self {
        Self::new(T::default())
    }
}
unsafe impl<T> Sync for ValkeyGILGuard<T> {}
unsafe impl<T> Send for ValkeyGILGuard<T> {}
pub struct ContextGuard {
    ctx: Context,
}
unsafe impl ValkeyLockIndicator for ContextGuard {}
impl Drop for ContextGuard {
    fn drop(&mut self) {
        unsafe {
            raw::RedisModule_ThreadSafeContextUnlock.unwrap()(self.ctx.ctx);
            raw::RedisModule_FreeThreadSafeContext.unwrap()(self.ctx.ctx);
        };
    }
}
impl Deref for ContextGuard {
    type Target = Context;
    fn deref(&self) -> &Self::Target {
        &self.ctx
    }
}
impl Borrow<Context> for ContextGuard {
    fn borrow(&self) -> &Context {
        &self.ctx
    }
}
pub struct DetachedFromClient;
pub struct ThreadSafeContext<B: Send> {
    pub(crate) ctx: *mut raw::RedisModuleCtx,
    #[allow(dead_code)]
    blocked_client: B,
}
unsafe impl<B: Send> Send for ThreadSafeContext<B> {}
unsafe impl<B: Send> Sync for ThreadSafeContext<B> {}
impl ThreadSafeContext<DetachedFromClient> {
    #[must_use]
    pub fn new() -> Self {
        let ctx = unsafe { raw::RedisModule_GetThreadSafeContext.unwrap()(ptr::null_mut()) };
        Self {
            ctx,
            blocked_client: DetachedFromClient,
        }
    }
}
impl Default for ThreadSafeContext<DetachedFromClient> {
    fn default() -> Self {
        Self::new()
    }
}
impl ThreadSafeContext<BlockedClient> {
    #[must_use]
    pub fn with_blocked_client(blocked_client: BlockedClient) -> Self {
        let ctx = unsafe { raw::RedisModule_GetThreadSafeContext.unwrap()(blocked_client.inner) };
        Self {
            ctx,
            blocked_client,
        }
    }
    #[allow(clippy::must_use_candidate)]
    pub fn reply(&self, r: ValkeyResult) -> raw::Status {
        let ctx = Context::new(self.ctx);
        ctx.reply(r)
    }
}
impl<B: Send> ThreadSafeContext<B> {
    pub fn lock(&self) -> ContextGuard {
        unsafe { raw::RedisModule_ThreadSafeContextLock.unwrap()(self.ctx) };
        let ctx = unsafe { raw::RedisModule_GetThreadSafeContext.unwrap()(ptr::null_mut()) };
        let ctx = Context::new(ctx);
        ContextGuard { ctx }
    }
}
impl<B: Send> Drop for ThreadSafeContext<B> {
    fn drop(&mut self) {
        unsafe { raw::RedisModule_FreeThreadSafeContext.unwrap()(self.ctx) };
    }
}