use std::{net::SocketAddr, sync::Arc};

use anyhow::Result;
use async_trait::async_trait;
use futures_util::stream::{SplitSink, SplitStream, StreamExt};
use futures_util::SinkExt;
use log::{debug, error, info, trace, warn};
use serde_json::Value;
use tokio::io::AsyncWriteExt;
use tokio::sync::{mpsc::Sender, Mutex};
use tokio::time::{timeout, Duration};
use tokio_rustls::server::TlsStream;
use tokio_tungstenite::tungstenite::Message as TokioMessage;
use tokio_tungstenite::WebSocketStream;

use crate::{
    config::Config,
    doslimit::{ConnectionLimits, GlobalLimits},
    query::Query,
    rpc::{daemon::connection::Connection, rpcstats::RpcStats},
    signal::{NetworkNotifier, Waiter},
    util::Channel,
};

use super::{
    communication::Communcation, server::ConnectionId, ssl_utils::create_tls_acceptor, Message,
};
use crate::query::scripthash_subscriptions::ScripthashSubscriptions;

pub struct WssCommunication {
    writer: SplitSink<WebSocketStream<TlsStream<tokio::net::TcpStream>>, TokioMessage>,
    reader: Option<SplitStream<WebSocketStream<TlsStream<tokio::net::TcpStream>>>>,
}

impl WssCommunication {
    pub async fn init(stream: TlsStream<tokio::net::TcpStream>) -> Result<Self> {
        let ws = tokio_tungstenite::accept_async(stream).await?;
        let (writer, reader) = ws.split();

        Ok(Self {
            reader: Some(reader),
            writer,
        })
    }
}

#[async_trait]
impl Communcation for WssCommunication {
    async fn send_values(&mut self, values: &[Value]) -> Result<()> {
        for value in values {
            let line = value.to_string();
            self.writer.feed(TokioMessage::Text(line.into())).await?;
        }
        self.writer.flush().await?;
        Ok(())
    }

    fn start_request_receiver(&mut self, rpc_queue_sender: Sender<Message>, idle_timeout: u64) {
        let reader = self.reader.take();

        crate::thread::spawn_task("wss_peer_reader", async move {
            // Give the reader to this task
            let mut reader = reader.unwrap();

            loop {
                Waiter::shutdown_check()?;
                let msg = timeout(Duration::from_secs(idle_timeout), reader.next()).await;

                let msg = match msg {
                    Ok(Some(message)) => message,
                    Ok(None) => break, // Connection closed
                    Err(_) => {
                        let _ = rpc_queue_sender.send(Message::Done).await;
                        return Err(anyhow!("Idle timeout {} exceeded", idle_timeout));
                    }
                };
                let msg = match msg {
                    Ok(msg) => msg,
                    Err(err) => {
                        trace!("wss peer error: {}", err);
                        let _ = rpc_queue_sender.send(Message::Done).await;
                        break;
                    }
                };

                if msg.is_text() {
                    let request = match msg.into_text() {
                        Ok(r) => r,
                        Err(e) => {
                            trace!("wss peer parse error: {}", e);
                            let _ = rpc_queue_sender.send(Message::Done).await;
                            break;
                        }
                    };
                    rpc_queue_sender
                        .send(Message::Request(request.to_string()))
                        .await?;
                } else if msg.is_binary() {
                    trace!("Ignoring binary message from wss peer");
                } else if msg.is_close() {
                    trace!("wss close event received");
                    let _ = rpc_queue_sender.send(Message::Done).await;
                    break;
                }
            }

            Ok(())
        });
    }

    async fn shutdown(&mut self) {
        let _ = self.writer.close().await;
        drop(self.reader.take());
    }
}

#[allow(clippy::too_many_arguments)]
pub async fn start_wss_server(
    conn_acceptor: Channel<Option<(tokio::net::TcpStream, SocketAddr)>>,
    cert_file: std::path::PathBuf,
    key_file: std::path::PathBuf,
    global_limits: Arc<GlobalLimits>,
    config: Arc<Config>,
    query: Arc<Query>,
    stats: Arc<RpcStats>,
    connection_limits: ConnectionLimits,
    network_notifier: Arc<NetworkNotifier>,
    rpc_queue_senders: Arc<Mutex<std::collections::HashMap<ConnectionId, Sender<Message>>>>,
    subscription_manager: Arc<ScripthashSubscriptions>,
) -> Result<()> {
    // Create TLS acceptor from certificate and key files
    let tls_acceptor = match create_tls_acceptor(&cert_file, &key_file).await {
        Ok(acceptor) => acceptor,
        Err(e) => {
            error!("Failed to create TLS acceptor: {}", e);
            return Err(e);
        }
    };

    while let Some(conn) = conn_acceptor.receiver().await.recv().await {
        let (mut stream, addr) = match conn {
            Some(c) => c,
            None => break,
        };

        let global_limits = global_limits.clone();
        let tls_acceptor = tls_acceptor.clone();

        let mut connections = match global_limits.inc_connection(&addr.ip()).await {
            Err(e) => {
                trace!("[{}] dropping wss peer - {}", addr, e);
                let _ = stream.shutdown().await;
                drop(stream);
                continue;
            }
            Ok(n) => n,
        };

        // explicitely scope the shadowed variables for the new thread
        let query_cpy = Arc::clone(&query);
        let stats_cpy = Arc::clone(&stats);
        let network_notifier_cpy = Arc::clone(&network_notifier);
        let config = Arc::clone(&config);
        let rpc_queue = Channel::bounded(config.rpc_buffer_size);
        let conn_id = ConnectionId::new();
        let subscription_manager_cpy = Arc::clone(&subscription_manager);
        let rpc_queue_senders_cpy = Arc::clone(&rpc_queue_senders);

        crate::thread::spawn_task("wss_peer", async move {
            if connections != (0, 0) {
                debug!(
                    "[{} wss] connected peer ({:?} out of {:?} connection slots used)",
                    addr,
                    connections,
                    global_limits.connection_limits(),
                );
            }

            // Accept the TLS connection
            let tls_stream = match tls_acceptor.accept(stream).await {
                Ok(stream) => stream,
                Err(e) => {
                    trace!("[{} wss] {}", addr, e);

                    // Cleanup connection limits
                    match global_limits.dec_connection(&addr.ip()).await {
                        Ok(n) => connections = n,
                        Err(e) => warn!("Failed to decrement connection count: {}", e),
                    };

                    debug!(
                        "[{} wss] disconnected peer ({:?} out of {:?} connection slots used)",
                        addr,
                        connections,
                        global_limits.connection_limits(),
                    );
                    return Ok(());
                }
            };

            let communication = match WssCommunication::init(tls_stream).await {
                Ok(c) => c,
                Err(e) => {
                    trace!("[{} wss] {}", addr, e);
                    // Cleanup connection limits
                    match global_limits.dec_connection(&addr.ip()).await {
                        Ok(n) => connections = n,
                        Err(e) => warn!("Failed to decrement connection count: {}", e),
                    };
                    debug!(
                        "[{} wss] disconnected peer ({:?} out of {:?} connection slots used)",
                        addr,
                        connections,
                        global_limits.connection_limits(),
                    );
                    return Ok(());
                }
            };

            rpc_queue_senders_cpy
                .lock()
                .await
                .insert(conn_id, rpc_queue.sender());

            let conn = Connection::new(
                config,
                query_cpy,
                addr,
                stats_cpy,
                connection_limits,
                Box::new(communication),
                network_notifier_cpy,
                conn_id,
                subscription_manager_cpy.clone(),
            );
            let conn_id_for_cleanup = conn_id;
            conn.run(rpc_queue).await;
            subscription_manager_cpy
                .remove_connection(conn_id_for_cleanup)
                .await;
            rpc_queue_senders_cpy
                .lock()
                .await
                .remove(&conn_id_for_cleanup);

            // Always cleanup connection limits, regardless of how the connection ended
            match global_limits.dec_connection(&addr.ip()).await {
                Ok(n) => connections = n,
                Err(e) => warn!("Failed to decrement connection count: {}", e),
            };

            debug!(
                "[{} wss] disconnected peer ({:?} out of {:?} connection slots used)",
                addr,
                connections,
                global_limits.connection_limits(),
            );

            Ok(())
        });
    }

    info!("WSS RPC connections no longer accepted");
    Ok(())
}
