use std::ffi::OsStr;
use std::time::{Duration, Instant};

fn which(cmd: &str) -> Option<std::path::PathBuf> {
    which::which(cmd).ok()
}

fn chrome_path() -> Option<std::path::PathBuf> {
    let mac =
        std::path::PathBuf::from("/Applications/Google Chrome.app/Contents/MacOS/Google Chrome");
    if mac.exists() {
        return Some(mac);
    }
    which("google-chrome")
        .or_else(|| which("google-chrome-stable"))
        .or_else(|| which("chromium"))
}

fn click_css(tab: &headless_chrome::Tab, css: &str) -> anyhow::Result<()> {
    tab.wait_for_element(css)?.click()?;
    Ok(())
}

fn wait_for_text(
    tab: &headless_chrome::Tab,
    needle: &str,
    timeout: Duration,
) -> anyhow::Result<()> {
    let deadline = Instant::now() + timeout;
    while Instant::now() < deadline {
        let js = format!(
            r#"(function() {{
  return document.body && (document.body.innerText || '').includes({n});
}})();"#,
            n = serde_json::to_string(needle).unwrap()
        );
        let v = tab.evaluate(&js, false)?;
        if v.value.and_then(|v| v.as_bool()).unwrap_or(false) {
            return Ok(());
        }
        std::thread::sleep(Duration::from_millis(200));
    }
    anyhow::bail!("timed out waiting for text: {needle}");
}

fn wait_for_blob_video(tab: &headless_chrome::Tab, timeout: Duration) -> anyhow::Result<()> {
    let deadline = Instant::now() + timeout;
    while Instant::now() < deadline {
        let js = r#"(function() {
  let v = document.querySelector('video');
  if (!v) return false;
  if (typeof v.src !== 'string') return false;
  return v.src.startsWith('blob:');
})();"#;
        let v = tab.evaluate(js, false)?;
        if v.value.and_then(|v| v.as_bool()).unwrap_or(false) {
            return Ok(());
        }
        std::thread::sleep(Duration::from_millis(200));
    }
    anyhow::bail!("timed out waiting for video blob src");
}

fn wait_for_video_element(tab: &headless_chrome::Tab, timeout: Duration) -> anyhow::Result<()> {
    let deadline = Instant::now() + timeout;
    while Instant::now() < deadline {
        let js = r#"(function() {
  return !!document.querySelector('video');
})();"#;
        let v = tab.evaluate(js, false)?;
        if v.value.and_then(|v| v.as_bool()).unwrap_or(false) {
            return Ok(());
        }
        std::thread::sleep(Duration::from_millis(200));
    }
    anyhow::bail!("timed out waiting for <video> element");
}

fn debug_player_state(tab: &headless_chrome::Tab) -> anyhow::Result<String> {
    let js = r#"(function() {
  let v = document.querySelector('video');
  let src = v ? (v.src || '') : null;
  let placeholder = document.querySelector('.placeholder');
  let placeholderText = placeholder ? (placeholder.innerText || '') : null;
  let status = document.querySelector('.source-status');
  let statusText = status ? (status.innerText || '') : null;
  let sources = Array.from(document.querySelectorAll('button[data-testid="global-watch"]')).length;
  return JSON.stringify({ hasVideo: !!v, videoSrc: src, placeholderText, statusText, sources });
})();"#;
    let v = tab.evaluate(js, false)?;
    Ok(v.value
        .and_then(|v| v.as_str().map(|s| s.to_string()))
        .unwrap_or_default())
}

fn click_global_watch(tab: &headless_chrome::Tab, stream_id: &str) -> anyhow::Result<bool> {
    let js = format!(
        r#"(function() {{
  let target = {sid};
  let btn = document.querySelector(`button[data-stream-id="${{target}}"]`);
  if (!btn) return false;
  // Some SPA frameworks attach delegated listeners; dispatch a real click event.
  btn.dispatchEvent(new MouseEvent('click', {{ bubbles: true, cancelable: true, view: window }}));
  return true;
}})();"#,
        sid = serde_json::to_string(stream_id).unwrap()
    );
    let v = tab.evaluate(&js, false)?;
    Ok(v.value.and_then(|v| v.as_bool()).unwrap_or(false))
}

#[test]
#[ignore]
fn e2e_remote_website_watch_existing_stream_id() -> anyhow::Result<()> {
    let chrome = match chrome_path() {
        Some(p) => p,
        None => return Ok(()), // skip
    };
    // We still want ffmpeg around for parity with other E2Es (and to discourage "works only without media tools").
    if which("ffmpeg").is_none() {
        return Ok(()); // skip
    }

    let site_url = std::env::var("EVERY_CHANNEL_SITE_URL")
        .unwrap_or_else(|_| "https://every.channel/".to_string());
    let stream_id = match std::env::var("EVERY_CHANNEL_STREAM_ID") {
        Ok(v) if !v.trim().is_empty() => v,
        _ => return Ok(()), // skip
    };

    let launch_options = headless_chrome::LaunchOptionsBuilder::default()
        .path(Some(chrome))
        .headless(true)
        .args(vec![
            OsStr::new("--autoplay-policy=no-user-gesture-required"),
            OsStr::new("--mute-audio"),
            OsStr::new("--disable-application-cache"),
            OsStr::new("--disable-service-worker"),
            OsStr::new("--disk-cache-size=0"),
        ])
        .build()
        .unwrap();
    let browser = headless_chrome::Browser::new(launch_options)?;
    let tab = browser.new_tab()?;
    tab.navigate_to(&site_url)?;
    tab.wait_until_navigated()?;

    click_css(&tab, "button[data-testid='global-refresh']")?;

    let deadline = Instant::now() + Duration::from_secs(60);
    loop {
        if click_global_watch(&tab, &stream_id)? {
            break;
        }
        if Instant::now() > deadline {
            anyhow::bail!("timed out waiting for stream_id to appear in global list");
        }
        std::thread::sleep(Duration::from_millis(250));
    }

    // Ensure the player is instantiated.
    if let Err(err) = wait_for_video_element(&tab, Duration::from_secs(90)) {
        let st = debug_player_state(&tab).unwrap_or_default();
        anyhow::bail!("{err}\nplayer_state={st}");
    }

    // We consider playback "started" when the video uses a blob: URL (MSE).
    if let Err(err) = wait_for_blob_video(&tab, Duration::from_secs(90)) {
        let st = debug_player_state(&tab).unwrap_or_default();
        anyhow::bail!("{err}\nplayer_state={st}");
    }

    Ok(())
}
