// Match Session Manager — handles ALL multiplayer edge cases
// Used by Chess, Ludo, Domino for consistent connection handling

import * as store from './store.js';
import * as net from './net.js';
import * as bus from './bus.js';

const PING_INTERVAL = 10000;      // Ping server every 10s to show "online"
const DISCONNECT_THRESHOLD = 30000; // Consider opponent disconnected after 30s
const ABANDON_THRESHOLD = 60000;    // Auto-win after 60s disconnect
const POLL_INTERVAL = 2000;         // Poll for opponent moves every 2s
const RECONNECT_GRACE = 5000;       // Wait 5s on page load before assuming fresh start

let currentSession = null;

export function create(matchId, gameType, options = {}) {
  destroy(); // Clean up any existing session

  currentSession = {
    matchId,
    gameType,               // 'chess' | 'ludo' | 'domino'
    myUserId: store.get('auth.userId'),
    lastServerPing: Date.now(),
    lastOpponentActivity: Date.now(),
    pollTimer: null,
    pingTimer: null,
    disconnectTimer: null,
    isActive: true,
    isBackground: false,
    onOpponentMove: options.onOpponentMove || null,
    onOpponentDisconnect: options.onOpponentDisconnect || null,
    onOpponentReconnect: options.onOpponentReconnect || null,
    onOpponentAbandon: options.onOpponentAbandon || null,
    onConnectionLost: options.onConnectionLost || null,
    onConnectionRestored: options.onConnectionRestored || null,
    opponentDisconnected: false,
    connectionLost: false  // Track whether we are currently in a disconnected state
  };

  // Save match ID to localStorage for tab refresh recovery
  localStorage.setItem('el3ab_active_match', JSON.stringify({
    matchId, gameType, timestamp: Date.now()
  }));

  startPolling();
  startPinging();
  startDisconnectWatch();
  setupVisibilityHandler();

  return currentSession;
}

export function destroy() {
  if (!currentSession) return;
  currentSession.isActive = false;
  clearInterval(currentSession.pollTimer);
  clearInterval(currentSession.pingTimer);
  clearInterval(currentSession.disconnectTimer);
  if (currentSession._visibilityHandler) {
    document.removeEventListener('visibilitychange', currentSession._visibilityHandler);
  }
  localStorage.removeItem('el3ab_active_match');
  currentSession = null;
  pollInFlight = false;
}

// Check if player has an active match to resume (after tab refresh)
export function getRecoverableMatch() {
  try {
    const saved = localStorage.getItem('el3ab_active_match');
    if (!saved) return null;
    const data = JSON.parse(saved);
    // Only recover if less than 5 minutes old
    if (Date.now() - data.timestamp > 300000) {
      localStorage.removeItem('el3ab_active_match');
      return null;
    }
    return data;
  } catch (e) { return null; }
}

// Record that opponent made an action (keeps disconnect timer alive)
export function markOpponentActive() {
  if (!currentSession) return;
  currentSession.lastOpponentActivity = Date.now();
  if (currentSession.opponentDisconnected) {
    currentSession.opponentDisconnected = false;
    currentSession.onOpponentReconnect?.();
  }
}

// ===== POLLING =====
let pollInFlight = false;

function startPolling() {
  if (!currentSession) return;
  currentSession.pollTimer = setInterval(async () => {
    if (!currentSession || !currentSession.isActive) return;
    if (currentSession.isBackground) return;
    if (pollInFlight) return;

    pollInFlight = true;
    try {
      const endpoint = currentSession.gameType === 'ludo' ? 'ludo-match.php' : currentSession.gameType === 'domino' ? 'domino-match.php' : currentSession.gameType === 'backgammon' ? 'backgammon-match.php' : 'game.php';
      const data = await net.post(endpoint, {
        action: 'get',
        match_id: currentSession.matchId
      });

      if (!data || data.error) return;

      currentSession.lastServerPing = Date.now();
      // Only fire onConnectionRestored if we previously fired onConnectionLost
      if (currentSession.connectionLost) {
        currentSession.connectionLost = false;
        currentSession.onConnectionRestored?.();
      }

      if (data.status === 'abandoned') {
        currentSession.onOpponentAbandon?.();
        return;
      }

      currentSession.onOpponentMove?.(data);

    } catch (e) {
      if (!currentSession) return;
      if (Date.now() - currentSession.lastServerPing > 10000 && !currentSession.connectionLost) {
        currentSession.connectionLost = true;
        currentSession.onConnectionLost?.();
      }
    } finally {
      pollInFlight = false;
    }
  }, POLL_INTERVAL);
}

// ===== HEARTBEAT (let server know we're still connected) =====
function startPinging() {
  if (!currentSession) return;
  currentSession.pingTimer = setInterval(async () => {
    if (!currentSession || !currentSession.isActive) return;
    try {
      const endpoint = currentSession.gameType === 'ludo' ? 'ludo-match.php' : currentSession.gameType === 'domino' ? 'domino-match.php' : currentSession.gameType === 'backgammon' ? 'backgammon-match.php' : 'game.php';
      await net.post(endpoint, {
        action: 'heartbeat',
        match_id: currentSession.matchId
      });
    } catch (e) {}
  }, PING_INTERVAL);
}

// ===== DISCONNECT DETECTION =====
function startDisconnectWatch() {
  if (!currentSession) return;
  currentSession.disconnectTimer = setInterval(() => {
    if (!currentSession || !currentSession.isActive) return;

    const elapsed = Date.now() - currentSession.lastOpponentActivity;

    if (elapsed > ABANDON_THRESHOLD && !currentSession.opponentDisconnected) {
      // Opponent abandoned — auto-claim win
      currentSession.onOpponentAbandon?.();
    } else if (elapsed > DISCONNECT_THRESHOLD && !currentSession.opponentDisconnected) {
      // Opponent disconnected but within grace period
      currentSession.opponentDisconnected = true;
      currentSession.onOpponentDisconnect?.();
    }
  }, 5000);
}

// ===== TAB VISIBILITY (handles tab switch/minimize) =====
function setupVisibilityHandler() {
  const handler = () => {
    if (!currentSession) return;

    if (document.hidden) {
      currentSession.isBackground = true;
    } else {
      currentSession.isBackground = false;
      currentSession.lastServerPing = Date.now();

      if (currentSession.onOpponentMove) {
        const endpoint = currentSession.gameType === 'ludo' ? 'ludo-match.php' : currentSession.gameType === 'domino' ? 'domino-match.php' : currentSession.gameType === 'backgammon' ? 'backgammon-match.php' : 'game.php';
        net.post(endpoint, { action: 'get', match_id: currentSession.matchId })
          .then(data => {
            if (data && !data.error && currentSession) {
              currentSession.onOpponentMove(data);
              markOpponentActive();
            }
          })
          .catch(() => {});
      }
    }
  };
  currentSession._visibilityHandler = handler;
  document.addEventListener('visibilitychange', handler);
}

// ===== UTILITY =====
export function isConnected() {
  if (!currentSession) return false;
  return (Date.now() - currentSession.lastServerPing) < 15000;
}

export function isOpponentConnected() {
  if (!currentSession) return false;
  return !currentSession.opponentDisconnected;
}

export function getMatchId() {
  return currentSession?.matchId || null;
}

export function getSession() {
  return currentSession;
}
