import _ from "lodash";

import type {
  Uci,
  San,
  Variant,
  Fen,
  GameNodeId,
  Shape,
  RichLegalSan,
} from "./chessops";

export type { Uci, San, Variant, Fen, GameNodeId, Shape, RichLegalSan };

import {
  PROMOTION_PIECES,
  PROMOTION_PIECE_NAMES,
  PROMOTION_PIECES_EXT,
  MOVE_RATINGS,
  storeKey,
  Pos_,
  Pos,
  Game_,
  Game,
  GameNode_,
  GameNode,
  chessgroundDests,
} from "./chessops";

export {
  PROMOTION_PIECES,
  PROMOTION_PIECE_NAMES,
  PROMOTION_PIECES_EXT,
  MOVE_RATINGS,
  storeKey,
  Pos_,
  Pos,
  Game_,
  Game,
  GameNode_,
  GameNode,
  chessgroundDests,
};

import type {
  EngineState,
  Commands,
  GoPayload,
  ScoreType,
  ScoreValue,
  ScoreNumerical,
  PvItem,
  AnalysisInfo,
  SendAnalysisInfo,
} from "./uciengine";

export type {
  EngineState,
  Commands,
  GoPayload,
  ScoreType,
  ScoreValue,
  ScoreNumerical,
  PvItem,
  AnalysisInfo,
  SendAnalysisInfo,
};

import {
  DepthItem,
  UciEngine,
  UciEngineNode,
  UciEngineBrowser,
  MATE_SCORE,
  PV_MAX_LENGTH,
  INFINITE,
} from "./uciengine";

export {
  DepthItem,
  UciEngine,
  UciEngineNode,
  UciEngineBrowser,
  MATE_SCORE,
  PV_MAX_LENGTH,
  INFINITE,
};

export function engineTest() {
  const game = Game();

  const sendAnalysisInfo = (info: AnalysisInfo) => {
    // do something with analysis info
  };

  const engine =
    typeof window !== "undefined"
      ? new UciEngineBrowser(
          "./binweb/stockfish.wasm.js",
          sendAnalysisInfo
        ).spawn()
      : new UciEngineNode("./bin/stockfish", sendAnalysisInfo).spawn();

  engine.go({
    depth: INFINITE,
    multipv: 2,
    bestmoveCallback: (info) => console.log("first go done", info.bestmove),
  });

  setTimeout((_) => {
    game.playSan("Nf3");
    engine.go({
      depth: INFINITE,
      multipv: 2,
      fen: game.current.fen,
      bestmoveCallback: (info) => console.log("second go done", info.bestmove),
    });
  }, 2000);

  setTimeout((_) => {
    engine.stop();
    setTimeout(() => engine.kill(), 2000);
  }, 4000);
}
