/**
 * 移动 / 骑乘规则（moveCondition 查表与 Unity Movement.MoveNextCheck 一致）
 *
 * 骑乘联动、上下车见 PlayerController / VehicleController（非查表部分）
 */
import { GridType, MoverRole, getMoveCondition, lookupMove } from '../core/Define';

export type MoveCheckResult = -1 | 0 | 1;

/** Unity PlayerController.OnMoving：targetGridType === None 时载具跟随 */
export function vehicleFollowsLandingTile(liveGrid: GridType): boolean {
    return liveGrid === GridType.None;
}

/** 落到瓦片砖面时下车（None 保持骑乘；Ride 动态格由上车逻辑处理） */
export function shouldDismountOnTile(tileGrid: GridType): boolean {
    return tileGrid === GridType.Across
        || tileGrid === GridType.Jump
        || tileGrid === GridType.Block
        || tileGrid === GridType.Boundary;
}

/** 落格后尝试保持骑乘：脚下为 None，或动态 Ride 且同格有载具 */
export function canStayMountedAfterLanding(liveGrid: GridType): boolean {
    return liveGrid === GridType.None || liveGrid === GridType.Ride;
}

/** 移动落点世界坐标应使用的 mover 角色（对齐 Unity 共享 transform / 载具甲板） */
export function moverRoleForLandingCell(
    role: MoverRole,
    liveGrid: GridType,
    mounted: boolean,
): MoverRole {
    if (mounted && vehicleFollowsLandingTile(liveGrid)) return 'vehicle';
    if (!mounted && liveGrid === GridType.Ride) return 'vehicle';
    return role;
}

/**
 * 查表判定一步移动（对齐 Unity Movement.MoveNextCheck）
 * - 1：可移动
 * - 0：不动（仅多人模式表外组合；单人表外为 -1）
 * - -1：失败
 */
export function checkMoveStep(
    role: MoverRole,
    curGrid: GridType,
    nextGrid: GridType,
    isJump: boolean,
    mult = false,
): MoveCheckResult {
    const table = getMoveCondition(mult);
    const v = lookupMove(table, role, curGrid, nextGrid, isJump);
    if (v === undefined) return mult ? 0 : -1;
    return v as MoveCheckResult;
}
