/**
 * Shared type definitions for the Riichi Mahjong Tutorial
 */

// ============ Canvas Types ============
export interface CanvasContext {
  ctx: CanvasRenderingContext2D;
  canvas: HTMLCanvasElement;
}

export interface Point {
  x: number;
  y: number;
}

export interface Rectangle {
  x: number;
  y: number;
  width: number;
  height: number;
}

export interface Size {
  width: number;
  height: number;
}

// ============ Tile Types ============
export type TileFamily = 1 | 2 | 3 | 4 | 5;

export interface TileIdentifier {
  family: TileFamily;
  value: number;
  isRed?: boolean;
}

export type TileFamilyName = 'man' | 'pin' | 'sou' | 'wind' | 'dragon';

export const TILE_FAMILY_MAP: Record<TileFamily, TileFamilyName> = {
  1: 'man',
  2: 'pin',
  3: 'sou',
  4: 'wind',
  5: 'dragon',
};

// ============ Game Types ============
export type PlayerPosition = 0 | 1 | 2 | 3;

export type WindType = 'east' | 'south' | 'west' | 'north';

export const WIND_ORDER: WindType[] = ['east', 'south', 'west', 'north'];

export interface PlayerState {
  position: PlayerPosition;
  wind: WindType;
  score: number;
  isDealer: boolean;
  hasRiichi: boolean;
}

export type GamePhase = 
  | 'waiting'
  | 'drawing'
  | 'discarding'
  | 'calling'
  | 'winning'
  | 'end';

export interface GameState {
  phase: GamePhase;
  currentPlayer: PlayerPosition;
  roundWind: WindType;
  roundNumber: number;
  honba: number;
  riichiSticks: number;
}

// ============ Group Types ============
export type GroupType = 'pair' | 'triplet' | 'sequence' | 'quad';

export interface GroupDefinition {
  type: GroupType;
  tiles: TileIdentifier[];
  isOpen: boolean;
}

// ============ Yaku Types ============
export interface YakuResult {
  name: string;
  han: number;
  isYakuman?: boolean;
}

export interface ScoreResult {
  yakus: YakuResult[];
  han: number;
  fu: number;
  basePoints: number;
  totalPoints: number;
}

// ============ UI Types ============
export type ButtonAction = 
  | 'pass'
  | 'chii'
  | 'pon'
  | 'kan'
  | 'ron'
  | 'tsumo'
  | 'riichi';

export interface ButtonState {
  action: ButtonAction;
  enabled: boolean;
  highlighted?: boolean;
}

// ============ Animation Types ============
export interface AnimationConfig {
  duration: number;
  easing: 'linear' | 'ease-in' | 'ease-out' | 'ease-in-out';
  delay?: number;
}

export interface AnimationState {
  startTime: number;
  progress: number;
  isComplete: boolean;
}

// ============ Event Types ============
export interface TileClickEvent {
  tileIndex: number;
  tile: TileIdentifier;
  position: Point;
}

export interface GameEvent {
  type: string;
  player: PlayerPosition;
  data?: unknown;
  timestamp: number;
}

// ============ Callback Types ============
export type CleanupFunction = () => void;
export type RenderFunction = (deltaTime: number) => void;
export type UpdateFunction = (deltaTime: number) => void;
