#pragma once

#include "core/core.h"

#include "rephash.h"

enum maximal_depth { maxDepth = 128 };		// maximul depth

// clear transposition table!
void clearTrans();
// helps removing too old entries!
void ageTrans();
// increase height of trans_table!
void incTransHeight();

// clear killer/pv slots (impl.note: called from within clearTrans())
void clearSlots();
// clear history heuristic (impl.note: called from within clearTrans())
void clearHistory();
// advance killer/pv slots one move forward
void advanceSlots();

enum max_pv_length { maxPV = 2*(maxDepth+1) };

typedef struct PVMove {
    Move m;
    i32 score;
} PVMove;

typedef PVMove PVMoves[ maxPV ];

extern void (*searchCallback)( i32 depth, i32 bestScore, i32 progress, const Move &bestmove, const Move &curmove,
                                   PVMoves *pv, i32 nodes, void *param );
extern void *searchCallbackParam;

// rephash:		current position repetition hash
// goodmoves:	(height 0 only): ptr to 'good' moves
// height:		starts at 0 (root_search)
// stalenum:	number of stalemate move indices
// stalemates:	stalemate move indices (to handle 3-rep rule properly;
//				engine should accept stalemate in drawish/losing positions only)
//				NOTE: 50 move rule NOT handled!
// moveorder:	move indices to be tried first
// moveScores:	individual move scores
// pv:			new pv; index[0] = best, index[1] = temporary
// timelimit:	ptr to move time limit (topvalue) in ms (0 = infinite)
// nummoves:	optional ptr to var (number of root moves searched)
i32 search( RepHash *rh, Move *goodmoves, Move &bestmove, Board &cb, i32 turn, i32 depth, i32 height, i32 alpha, i32 beta,
            i32 stalenum = 0, u8 *stalemates = 0, u8 *moveorder = 0, i32 *moveScores = 0,
            PVMoves *pv = 0, PVMoves *lastpv = 0,
            u8 verify = 0, u8 usenull = 1, u8 usednull = 0, u32 *timelimit = 0, i32 *rootmoves = 0,
            Move *rootverified = 0);

// real node search count (for stats)
extern i32 nodesSearched;				// last search
extern i64 totalNodesSearched;			// total
extern i32 drawCuts;					// draw-by-rep search cuts (last search)
extern volatile bool stopSearch;		// stop search flag
