use crate::{
    connection::context::Context,
    http::{
        meta::HttpMetadata,
        protocol::{header::HeaderKey, header::Headers, method::HttpMethod},
        types::Executor,
        websocket::{BinaryHandler, TextHandler, WSCodec, WSFrame},
    },
};
use base64::Engine;
use base64::engine::general_purpose::STANDARD;
use futures::{FutureExt, StreamExt};
use sha1::{Digest, Sha1};
use std::{
    pin::Pin,
    sync::Arc,
    task::{Context as TaskContext, Poll},
};
use tokio::io::{AsyncRead, AsyncWrite, AsyncWriteExt, ReadBuf};
use tokio::sync::Mutex;
use tokio_util::codec::Framed;

use futures::future::BoxFuture;

/// 用于组合 Context 中的 reader 和 writer
struct CombinedStream {
    reader: Box<dyn tokio::io::AsyncRead + Send + Unpin>,
    writer: Box<dyn tokio::io::AsyncWrite + Send + Unpin>,
}

/// 所有 WebSocket 连接的写端收集器，用于从外部推送消息
#[derive(Clone)]
pub struct WsSenderList {
    pub senders: Arc<Mutex<Vec<tokio::sync::mpsc::UnboundedSender<WSFrame>>>>,
}

impl WsSenderList {
    pub fn new() -> Self {
        Self {
            senders: Arc::new(Mutex::new(Vec::new())),
        }
    }

    /// 向所有已连接的 WebSocket 客户端广播文本消息
    pub async fn broadcast(&self, text: &str) {
        let mut guard = self.senders.lock().await;
        guard.retain(|tx| tx.send(WSFrame::Text(text.to_string())).is_ok());
    }

    /// 获取发送器数量（调试用）
    pub async fn len(&self) -> usize {
        self.senders.lock().await.len()
    }
}

impl AsyncRead for CombinedStream {
    fn poll_read(
        mut self: Pin<&mut Self>,
        cx: &mut TaskContext<'_>,
        buf: &mut ReadBuf<'_>,
    ) -> Poll<std::io::Result<()>> {
        Pin::new(&mut self.reader).poll_read(cx, buf)
    }
}

impl AsyncWrite for CombinedStream {
    fn poll_write(
        mut self: Pin<&mut Self>,
        cx: &mut TaskContext<'_>,
        buf: &[u8],
    ) -> Poll<std::io::Result<usize>> {
        Pin::new(&mut self.writer).poll_write(cx, buf)
    }
    fn poll_flush(mut self: Pin<&mut Self>, cx: &mut TaskContext<'_>) -> Poll<std::io::Result<()>> {
        Pin::new(&mut self.writer).poll_flush(cx)
    }
    fn poll_shutdown(
        mut self: Pin<&mut Self>,
        cx: &mut TaskContext<'_>,
    ) -> Poll<std::io::Result<()>> {
        Pin::new(&mut self.writer).poll_shutdown(cx)
    }
}

#[derive(Clone)]
pub struct WebSocket {
    pub on_text: Option<TextHandler>,
    pub on_binary: Option<BinaryHandler>,
}

impl WebSocket {
    pub fn new() -> Self {
        Self {
            on_text: None,
            on_binary: None,
        }
    }

    /// 设置文本消息处理器
    pub fn on_text<F>(mut self, handler: F) -> Self
    where
        F: Fn(&WebSocket, &mut Context, String) -> BoxFuture<'static, bool> + Send + Sync + 'static,
    {
        self.on_text = Some(Arc::new(handler));
        self
    }

    /// 设置二进制消息处理器
    pub fn on_binary<F>(mut self, handler: F) -> Self
    where
        F: Fn(&WebSocket, &mut Context, Vec<u8>) -> BoxFuture<'static, bool>
            + Send
            + Sync
            + 'static,
    {
        self.on_binary = Some(Arc::new(handler));
        self
    }

    /// 设置统一的帧处理器 (兼容旧API)
    #[allow(unused)]
    pub fn set_handler<F>(mut self, handler: F) -> Self
    where
        F: Fn(&WebSocket, &mut Context, WSFrame) -> BoxFuture<'static, bool>
            + Send
            + Sync
            + 'static,
    {
        self
    }

    /// 判断请求是否是 WebSocket 握手
    pub fn check(method: HttpMethod, headers: &Headers) -> bool {
        if method != HttpMethod::GET {
            return false;
        }
        let upgrade = headers
            .get(&HeaderKey::Upgrade)
            .map(|v| v.eq_ignore_ascii_case("websocket"))
            .unwrap_or(false);
        let connection = headers
            .get(&HeaderKey::Connection)
            .map(|v| v.to_ascii_lowercase().contains("upgrade"))
            .unwrap_or(false);
        upgrade && connection
    }

    /// 完成 WebSocket 握手
    pub async fn handshake(
        writer: &mut (dyn AsyncWrite + Send + Unpin),
        headers: &Headers,
    ) -> anyhow::Result<()> {
        let key = headers
            .get(&HeaderKey::SecWebSocketKey)
            .ok_or_else(|| anyhow::anyhow!("missing Sec-WebSocket-Key"))?;

        let mut sha = Sha1::new();
        sha.update(key.as_bytes());
        sha.update(b"258EAFA5-E914-47DA-95CA-C5AB0DC85B11");
        let accept_key = STANDARD.encode(sha.finalize());

        let response = format!(
            "HTTP/1.1 101 Switching Protocols\r\n\
            Upgrade: websocket\r\n\
            Connection: Upgrade\r\n\
            Sec-WebSocket-Accept: {}\r\n\r\n",
            accept_key
        );

        writer.write_all(response.as_bytes()).await?;
        writer.flush().await?;
        Ok(())
    }

    /// WebSocket 核心运行循环（支持外部推送）
    pub async fn run(ws: &WebSocket, ctx: &mut Context) -> anyhow::Result<()> {
        let reader = ctx
            .reader
            .take()
            .ok_or_else(|| anyhow::anyhow!("Reader missing"))?;
        let writer = ctx
            .writer
            .take()
            .ok_or_else(|| anyhow::anyhow!("Writer missing"))?;

        let io = CombinedStream { reader, writer };
        let framed = Framed::new(io, WSCodec);

        let (mut sink, mut stream) = framed.split();

        // 外部推送通道
        let (out_tx, mut out_rx) = tokio::sync::mpsc::unbounded_channel::<WSFrame>();

        // 注册到全局列表
        {
            if let Some(list) = ctx.global.get::<WsSenderList>().await {
                list.senders.lock().await.push(out_tx.clone());
            }
        }

        // 后台写任务：将外部推送的消息发到 WebSocket
        tokio::spawn(async move {
            use futures::SinkExt;
            while let Some(frame) = out_rx.recv().await {
                if let Err(e) = sink.send(frame).await {
                    tracing::debug!("WS send error: {:?}", e);
                    break;
                }
            }
        });

        while let Some(result) = stream.next().await {
            let frame = match result {
                Ok(f) => f,
                Err(e) => {
                    return Err(anyhow::anyhow!("Protocol error: {}", e));
                }
            };

            let close_connection = match frame {
                WSFrame::Text(text) => {
                    if let Some(ref handler) = ws.on_text {
                        handler(ws, ctx, text).await
                    } else {
                        true
                    }
                }
                WSFrame::Binary(data) => {
                    if let Some(ref handler) = ws.on_binary {
                        handler(ws, ctx, data).await
                    } else {
                        true
                    }
                }
                WSFrame::Ping(p) => {
                    let _ = out_tx.send(WSFrame::Pong(p));
                    true
                }
                WSFrame::Close(_code, _reason) => {
                    // 连接关闭，不回复
                    break;
                }
                _ => true,
            };

            if !close_connection {
                break;
            }
        }
        Ok(())
    }

    /// 生成 WebSocket 中件间
    pub fn to_middleware(ws: WebSocket) -> Box<Executor> {
        let ws = Arc::new(ws);

        Box::new(move |ctx: &mut Context| {
            let ws = ws.clone();
            (async move {
                let meta = match ctx.local.get_value::<HttpMetadata>() {
                    Some(m) => m,
                    None => {
                        return true;
                    }
                };

                if !Self::check(meta.method, &meta.headers) {
                    return true;
                }

                // 初始化全局 WS 发送器列表
                if ctx.global.get::<WsSenderList>().await.is_none() {
                    ctx.global.set(WsSenderList::new()).await;
                }

                // 进行握手
                {
                    let w = match ctx.writer.as_deref_mut() {
                        Some(w) => w,
                        None => {
                            tracing::error!("WebSocket: no writer available");
                            return false;
                        }
                    };
                    if let Err(e) = Self::handshake(w, &meta.headers).await {
                        tracing::warn!("WS Handshake Error: {:?}", e);
                        return false;
                    }
                }

                // 启动循环 (内部会接管 reader/writer)
                if let Err(e) = Self::run(&ws, ctx).await {
                    tracing::debug!("WS Connection Ended: {:?}", e);
                }

                false // 拦截，不继续执行后续 HTTP 中间件
            })
            .boxed()
        })
    }

    /// 严格按照 RFC 6455 解析 Close 帧负载，返回借用的 &str 以优化性能
    pub fn parse_close_payload(payload: &[u8]) -> anyhow::Result<(u16, Option<&str>)> {
        let len = payload.len();

        // 1. 空负载：协议规定视为 1005 (No Status Rcvd)
        if len == 0 {
            return Ok((1005, None));
        }

        // 2. 异常长度：如果有载荷但不足 2 字节，属于协议错误
        if len < 2 {
            anyhow::bail!("Incomplete close status code");
        }

        // 3. 提取状态码 (Big-Endian)
        let code = u16::from_be_bytes([payload[0], payload[1]]);

        // 4. 解析原因 (必须是有效的 UTF-8)
        let reason = if len > 2 {
            let s = std::str::from_utf8(&payload[2..])?;
            Some(s)
        } else {
            None
        };

        Ok((code, reason))
    }
}
