//! HttpPlugin — Web UI + REST API
//!
//! 基于 warp 的 HTTP 服务，提供播放控制、配置管理、视频管理等 API。

mod routes;

use crate::core::config::AppConfig;
use crate::core::message::{Destination, Envelope, Message};
use crate::core::plugin::{Platform, Plugin, PluginContext, PluginInfo};
use anyhow::{Context, Result};
use serde::Serialize;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::mpsc;
use std::sync::{Arc, Condvar, Mutex};
use std::thread::JoinHandle;
use tokio::sync::broadcast;
use tokio::sync::{oneshot, Mutex as AsyncMutex};

#[derive(Serialize)]
struct WsEvent<'a, T> {
    #[serde(rename = "type")]
    event_type: &'a str,
    data: T,
}

fn encode_ws_event<T: Serialize>(event_type: &str, data: T) -> Option<String> {
    match serde_json::to_string(&WsEvent { event_type, data }) {
        Ok(payload) => Some(payload),
        Err(error) => {
            eprintln!("[HttpPlugin] failed to serialize websocket event '{event_type}': {error}");
            None
        }
    }
}

struct PendingWifiResponse {
    version: u64,
    payload: Option<String>,
}

pub(crate) struct HttpState {
    wifi_request_lock: AsyncMutex<()>,
    wifi_response: Mutex<PendingWifiResponse>,
    wifi_response_cv: Condvar,
    last_wifi_result: Mutex<Option<String>>,
    config: Mutex<Arc<AppConfig>>,
    player_status: Mutex<crate::core::message::PlayerStatusData>,
    ble_ready: AtomicBool,
    ws_events: broadcast::Sender<String>,
    /// 动态插件管理状态（由 Custom 消息更新）
    plugin_states: Mutex<Vec<crate::core::service_manager::PluginStateInfo>>,
    /// AI 对话管线（HTTP 路由直接调用）
    ai_pipeline: Mutex<Option<std::sync::Arc<crate::plugins::ai::ChatPipeline>>>,
    /// AI 模型管理器共享句柄（HTTP 模型管理 API 调用）
    ai_models: Mutex<Option<std::sync::Arc<std::sync::Mutex<crate::plugins::ai::model_manager::ModelManager>>>>,
    /// Live2D 说话状态（设备端 Firefox 轮询，驱动嘴部动效）
    live2d_talking: AtomicBool,
    /// 向其他插件（如 live2d）转发消息的出口（init 时由 PluginContext.tx 注入）
    outbox: mpsc::Sender<Envelope>,
}

impl HttpState {
    fn new(config: Arc<AppConfig>, tx: mpsc::Sender<Envelope>) -> Self {
        let (ws_events, _) = broadcast::channel(32);
        let player_status = crate::core::message::PlayerStatusData {
            running: false,
            paused: !config.playback.auto_start,
            in_transition: false,
            current_index: 0,
            playlist_length: config.playlist.len(),
            current_video: config.playlist.first().map(|item| item.id.clone()),
        };

        Self {
            wifi_request_lock: AsyncMutex::new(()),
            wifi_response: Mutex::new(PendingWifiResponse {
                version: 0,
                payload: None,
            }),
            wifi_response_cv: Condvar::new(),
            last_wifi_result: Mutex::new(None),
            config: Mutex::new(config),
            player_status: Mutex::new(player_status),
            ble_ready: AtomicBool::new(false),
            ws_events,
            plugin_states: Mutex::new(Vec::new()),
            ai_pipeline: Mutex::new(None),
            ai_models: Mutex::new(None),
            live2d_talking: AtomicBool::new(false),
            outbox: tx,
        }
    }

    /// 向指定插件发送消息（用于把 AI 回复文字推给 live2d 做设备端字幕）。
    pub(crate) fn send_message(&self, to: Destination, msg: Message) {
        if let Err(e) = self.outbox.send(Envelope {
            from: "http".to_string(),
            to,
            message: msg,
        }) {
            eprintln!("[HttpPlugin] 发送消息失败: {e}");
        }
    }

    /// 注册 AI 对话管线（AiPlugin init 时调用）
    pub(crate) fn register_ai_pipeline(&self, pipeline: std::sync::Arc<crate::plugins::ai::ChatPipeline>) {
        if let Ok(mut slot) = self.ai_pipeline.lock() {
            *slot = Some(pipeline);
        }
    }

    /// 注册 AI 模型管理器（AiPlugin init 时调用）
    pub(crate) fn register_ai_models(
        &self,
        models: std::sync::Arc<std::sync::Mutex<crate::plugins::ai::model_manager::ModelManager>>,
    ) {
        if let Ok(mut slot) = self.ai_models.lock() {
            *slot = Some(models);
        }
    }

    /// 获取 AI 对话管线（HTTP 路由调用）
    pub(crate) fn ai_pipeline(&self) -> Option<std::sync::Arc<crate::plugins::ai::ChatPipeline>> {
        self.ai_pipeline.lock().ok().and_then(|slot| slot.clone())
    }

    /// 获取 AI 模型管理器（HTTP 路由调用）
    pub(crate) fn ai_models(
        &self,
    ) -> Option<std::sync::Arc<std::sync::Mutex<crate::plugins::ai::model_manager::ModelManager>>> {
        self.ai_models.lock().ok().and_then(|slot| slot.clone())
    }

    /// 设置 Live2D 说话状态（对话开始/结束时调用）
    pub(crate) fn set_live2d_talking(&self, talking: bool) {
        self.live2d_talking.store(talking, Ordering::SeqCst);
    }

    /// 获取 Live2D 说话状态（设备端轮询接口调用）
    pub(crate) fn live2d_talking(&self) -> bool {
        self.live2d_talking.load(Ordering::SeqCst)
    }

    fn publish_wifi_result(&self, payload: String) {
        if let Ok(mut state) = self.wifi_response.lock() {
            state.version += 1;
            state.payload = Some(payload.clone());
            self.wifi_response_cv.notify_all();
        }

        if let Ok(mut last_wifi_result) = self.last_wifi_result.lock() {
            *last_wifi_result = Some(payload.clone());
        }

        let ws_payload = match serde_json::from_str::<serde_json::Value>(&payload) {
            Ok(value) => encode_ws_event("wifi_update", value),
            Err(_) => encode_ws_event("wifi_update", serde_json::json!({ "raw": payload })),
        };

        if let Some(ws_payload) = ws_payload {
            self.publish_ws(ws_payload);
        }
    }

    pub(crate) fn config(&self) -> Arc<AppConfig> {
        Arc::clone(
            &self
                .config
                .lock()
                .expect("http config state poisoned"),
        )
    }

    fn replace_config(&self, config: Arc<AppConfig>) {
        if let Ok(mut current) = self.config.lock() {
            *current = Arc::clone(&config);
        }

        if let Ok(mut player_status) = self.player_status.lock() {
            player_status.playlist_length = config.playlist.len();
            if player_status.current_video.is_none() {
                player_status.current_video = config.playlist.first().map(|item| item.id.clone());
            }
        }
    }

    pub(crate) fn player_status(&self) -> crate::core::message::PlayerStatusData {
        self.player_status
            .lock()
            .expect("http player status state poisoned")
            .clone()
    }

    fn update_player_status(&self, status: crate::core::message::PlayerStatusData) {
        if let Ok(mut current) = self.player_status.lock() {
            *current = status;
        }
    }

    pub(crate) fn ble_ready(&self) -> bool {
        self.ble_ready.load(Ordering::SeqCst)
    }

    fn publish_ws(&self, payload: String) {
        let _ = self.ws_events.send(payload);
    }

    pub(crate) fn ws_snapshots(&self) -> Vec<String> {
        let mut snapshots = Vec::new();

        if let Some(payload) = encode_ws_event("status_update", self.player_status()) {
            snapshots.push(payload);
        }

        let config = self.config();
        if let Some(payload) = encode_ws_event("config_update", config.as_ref()) {
            snapshots.push(payload);
        }

        if let Some(payload) = encode_ws_event(
            "ble_update",
            serde_json::json!({ "ready": self.ble_ready() }),
        ) {
            snapshots.push(payload);
        }

        if let Ok(last_wifi_result) = self.last_wifi_result.lock() {
            if let Some(raw) = last_wifi_result.as_ref() {
                let payload = match serde_json::from_str::<serde_json::Value>(raw) {
                    Ok(value) => encode_ws_event("wifi_update", value),
                    Err(_) => encode_ws_event("wifi_update", serde_json::json!({ "raw": raw })),
                };

                if let Some(payload) = payload {
                    snapshots.push(payload);
                }
            }
        }

        snapshots
    }

    pub(crate) fn ws_subscribe(&self) -> broadcast::Receiver<String> {
        self.ws_events.subscribe()
    }

    fn set_ble_ready(&self, ready: bool) {
        self.ble_ready.store(ready, Ordering::SeqCst);
        if let Some(payload) = encode_ws_event("ble_update", serde_json::json!({ "ready": ready }))
        {
            self.publish_ws(payload);
        }
    }

    pub(crate) fn plugin_states(&self) -> Vec<crate::core::service_manager::PluginStateInfo> {
        self.plugin_states
            .lock()
            .map(|s| s.clone())
            .unwrap_or_default()
    }

    fn update_plugin_states(&self, json: &str) {
        if let Ok(states) = serde_json::from_str::<Vec<crate::core::service_manager::PluginStateInfo>>(json) {
            if let Ok(mut current) = self.plugin_states.lock() {
                *current = states;
            }
        }
    }
}

pub struct HttpPlugin {
    ctx: Option<PluginContext>,
    state: Option<Arc<HttpState>>,
    shutdown_tx: Option<oneshot::Sender<()>>,
    server_thread: Option<JoinHandle<()>>,
    /// AI 对话管线（main.rs 注册时传入，init 时存入 HttpState）
    ai_pipeline: Option<std::sync::Arc<crate::plugins::ai::ChatPipeline>>,
    /// AI 模型管理器（main.rs 注册时传入）
    ai_models: Option<std::sync::Arc<std::sync::Mutex<crate::plugins::ai::model_manager::ModelManager>>>,
}

impl HttpPlugin {
    pub fn new() -> Self {
        Self {
            ctx: None,
            state: None,
            shutdown_tx: None,
            server_thread: None,
            ai_pipeline: None,
            ai_models: None,
        }
    }

    /// 设置 AI 对话管线（main.rs 注册 AiPlugin 后调用）
    pub fn set_ai_pipeline(&mut self, pipeline: std::sync::Arc<crate::plugins::ai::ChatPipeline>) {
        self.ai_pipeline = Some(pipeline);
    }

    /// 设置 AI 模型管理器（main.rs 注册 AiPlugin 后调用）
    pub fn set_ai_models(&mut self, models: std::sync::Arc<std::sync::Mutex<crate::plugins::ai::model_manager::ModelManager>>) {
        self.ai_models = Some(models);
    }
}

impl Default for HttpPlugin {
    fn default() -> Self {
        Self::new()
    }
}

impl Plugin for HttpPlugin {
    fn id(&self) -> &str {
        "http"
    }

    fn info(&self) -> PluginInfo {
        PluginInfo {
            name: "HTTP API".to_string(),
            version: "0.2.0".to_string(),
            description: "Web UI + REST API (warp)".to_string(),
            platform: Platform::Any,
        }
    }

    fn dependencies(&self) -> Vec<String> {
        vec![crate::core::plugin_ids::VIDEO.to_string()]
    }

    fn init(&mut self, ctx: PluginContext) -> Result<()> {
        let state = Arc::new(HttpState::new(
            Arc::clone(&ctx.config),
            ctx.tx.clone(),
        ));
        // 注册 AI 句柄（如果 main.rs 已设置）
        if let Some(pipeline) = self.ai_pipeline.take() {
            state.register_ai_pipeline(pipeline);
        }
        if let Some(ai_models) = self.ai_models.take() {
            state.register_ai_models(ai_models);
        }
        self.state = Some(state);
        self.ctx = Some(ctx);
        Ok(())
    }

    fn start(&mut self) -> Result<()> {
        self.stop()?;

        let ctx = self
            .ctx
            .as_ref()
            .context("http plugin context is not initialized")?;

        if !ctx.config.remote_control.enabled {
            println!("[HttpPlugin] Remote control disabled, skip HTTP server startup");
            return Ok(());
        }

        let host = ctx.config.remote_control.host.clone();
        let port = ctx.config.remote_control.port;
        let tx = ctx.tx.clone();
        let state = Arc::clone(
            self.state
                .as_ref()
                .context("http plugin state is not initialized")?,
        );

        let (shutdown_tx, shutdown_rx) = oneshot::channel();

        let server_thread = std::thread::spawn(move || {
            let runtime = match tokio::runtime::Builder::new_multi_thread()
                .enable_all()
                .build()
            {
                Ok(runtime) => runtime,
                Err(error) => {
                    eprintln!("[HttpPlugin] failed to create tokio runtime: {error}");
                    return;
                }
            };

            runtime.block_on(async move {
                let routes = routes::build_routes(tx.clone(), state);
                let addr: std::net::SocketAddr = match format!("{host}:{port}").parse() {
                    Ok(addr) => addr,
                    Err(error) => {
                        eprintln!("[HttpPlugin] invalid listen address {host}:{port}: {error}");
                        return;
                    }
                };

                if let Err(error) = tx.send(Envelope {
                    from: "http".to_string(),
                    to: crate::core::message::Destination::Manager,
                    message: Message::PluginReady("http".to_string()),
                }) {
                    eprintln!("[HttpPlugin] failed to report ready state: {error}");
                }

                println!("[HttpPlugin] listening on http://{addr}");
                warp::serve(routes)
                    .bind_with_graceful_shutdown(addr, async move {
                        let _ = shutdown_rx.await;
                    })
                    .1
                    .await;
            });
        });

        self.shutdown_tx = Some(shutdown_tx);
        self.server_thread = Some(server_thread);

        Ok(())
    }

    fn handle_message(&mut self, msg: Message) -> Result<()> {
        let state = match self.state.as_ref() {
            Some(state) => state,
            None => return Ok(()),
        };

        match msg {
            Message::WifiResult(payload) => state.publish_wifi_result(payload),
            Message::PlayerStatus(status) => {
                state.update_player_status(status.clone());
                if let Some(payload) = encode_ws_event("status_update", &status) {
                    state.publish_ws(payload);
                }
            }
            Message::ConfigReloaded(config) => {
                state.replace_config(Arc::new(config.clone()));
                if let Some(payload) = encode_ws_event("config_update", &config) {
                    state.publish_ws(payload);
                }
            }
            Message::StateChanged {
                old_state,
                new_state,
            } => {
                if let Some(payload) = encode_ws_event(
                    "state_update",
                    serde_json::json!({ "old_state": old_state, "new_state": new_state }),
                ) {
                    state.publish_ws(payload);
                }
            }
            Message::PluginReady(ref id) if id == "ble" => state.set_ble_ready(true),
            Message::Shutdown => state.set_ble_ready(false),
            Message::Custom { ref kind, ref payload } if kind == "plugin_states" => {
                state.update_plugin_states(payload);
            }
            _ => {}
        }

        Ok(())
    }

    fn stop(&mut self) -> Result<()> {
        if let Some(shutdown_tx) = self.shutdown_tx.take() {
            let _ = shutdown_tx.send(());
        }

        if let Some(server_thread) = self.server_thread.take() {
            server_thread
                .join()
                .map_err(|_| anyhow::anyhow!("http server thread panicked"))?;
        }

        Ok(())
    }
}
