/**
 * Worksheet Editor - TypeScript Interfaces
 *
 * Types for the visual worksheet editor using Fabric.js
 */

import type { Canvas, Object as FabricObject } from 'fabric'

// Tool Types
export type EditorTool =
  | 'select'
  | 'text'
  | 'rectangle'
  | 'circle'
  | 'line'
  | 'arrow'
  | 'image'
  | 'ai-image'
  | 'table'

// Text Alignment
export type TextAlign = 'left' | 'center' | 'right' | 'justify'

// Font Weight
export type FontWeight = 'normal' | 'bold'

// Font Style
export type FontStyle = 'normal' | 'italic'

// Object Type
export type WorksheetObjectType =
  | 'text'
  | 'image'
  | 'rectangle'
  | 'circle'
  | 'line'
  | 'arrow'
  | 'table'
  | 'ai-image'

// Base Object Properties
export interface BaseObjectProps {
  id: string
  type: WorksheetObjectType
  left: number
  top: number
  width?: number
  height?: number
  angle: number
  opacity: number
  fill?: string
  stroke?: string
  strokeWidth?: number
  locked?: boolean
}

// Text Object Properties
export interface TextObjectProps extends BaseObjectProps {
  type: 'text'
  text: string
  fontFamily: string
  fontSize: number
  fontWeight: FontWeight
  fontStyle: FontStyle
  textAlign: TextAlign
  lineHeight: number
  charSpacing: number
  underline?: boolean
  linethrough?: boolean
}

// Image Object Properties
export interface ImageObjectProps extends BaseObjectProps {
  type: 'image' | 'ai-image'
  src: string
  originalWidth: number
  originalHeight: number
  cropX?: number
  cropY?: number
  cropWidth?: number
  cropHeight?: number
}

// Shape Object Properties
export interface ShapeObjectProps extends BaseObjectProps {
  type: 'rectangle' | 'circle' | 'line' | 'arrow'
  rx?: number // Corner radius for rectangles
  ry?: number
}

// Table Object Properties
export interface TableObjectProps extends BaseObjectProps {
  type: 'table'
  rows: number
  cols: number
  cellWidth: number
  cellHeight: number
  cellData: string[][]
}

// Union type for all objects
export type WorksheetObject =
  | TextObjectProps
  | ImageObjectProps
  | ShapeObjectProps
  | TableObjectProps

// Page
export interface WorksheetPage {
  id: string
  index: number
  canvasJSON: string // Serialized Fabric.js canvas state
  thumbnail?: string
}

// Worksheet Document
export interface WorksheetDocument {
  id: string
  title: string
  description?: string
  pages: WorksheetPage[]
  pageFormat: PageFormat
  createdAt: string
  updatedAt: string
}

// Page Format
export interface PageFormat {
  width: number   // in mm
  height: number  // in mm
  orientation: 'portrait' | 'landscape'
  margins: {
    top: number
    right: number
    bottom: number
    left: number
  }
}

// Default A4 Format
export const DEFAULT_PAGE_FORMAT: PageFormat = {
  width: 210,
  height: 297,
  orientation: 'portrait',
  margins: {
    top: 15,
    right: 15,
    bottom: 15,
    left: 15
  }
}

// Canvas Scale (mm to pixels at 96 DPI)
export const MM_TO_PX = 3.7795275591 // 1mm = 3.78px at 96 DPI

// AI Image Generation
export interface AIImageRequest {
  prompt: string
  style: AIImageStyle
  width: number
  height: number
}

export type AIImageStyle =
  | 'realistic'
  | 'cartoon'
  | 'sketch'
  | 'clipart'
  | 'educational'

export interface AIImageResponse {
  image_base64: string
  prompt_used: string
  error?: string
}

// Editor State
export interface EditorState {
  activeTool: EditorTool
  activeObject: FabricObject | null
  selectedObjects: FabricObject[]
  zoom: number
  showGrid: boolean
  snapToGrid: boolean
  gridSize: number
  currentPageIndex: number
}

// History Entry for Undo/Redo
export interface HistoryEntry {
  canvasJSON: string
  timestamp: number
  action: string
}

// Typography Presets
export interface TypographyPreset {
  id: string
  name: string
  fontFamily: string
  fontSize: number
  fontWeight: FontWeight
  lineHeight: number
}

// Default Typography Presets
export const DEFAULT_TYPOGRAPHY_PRESETS: TypographyPreset[] = [
  { id: 'h1', name: 'Überschrift 1', fontFamily: 'Arial', fontSize: 32, fontWeight: 'bold', lineHeight: 1.2 },
  { id: 'h2', name: 'Überschrift 2', fontFamily: 'Arial', fontSize: 24, fontWeight: 'bold', lineHeight: 1.3 },
  { id: 'h3', name: 'Überschrift 3', fontFamily: 'Arial', fontSize: 18, fontWeight: 'bold', lineHeight: 1.4 },
  { id: 'body', name: 'Fließtext', fontFamily: 'Arial', fontSize: 12, fontWeight: 'normal', lineHeight: 1.5 },
  { id: 'small', name: 'Klein', fontFamily: 'Arial', fontSize: 10, fontWeight: 'normal', lineHeight: 1.4 },
  { id: 'caption', name: 'Bildunterschrift', fontFamily: 'Arial', fontSize: 9, fontWeight: 'normal', lineHeight: 1.3 },
]

// Available Fonts
export const AVAILABLE_FONTS = [
  { name: 'Arial', family: 'Arial, sans-serif' },
  { name: 'Times New Roman', family: 'Times New Roman, serif' },
  { name: 'Georgia', family: 'Georgia, serif' },
  { name: 'Verdana', family: 'Verdana, sans-serif' },
  { name: 'Comic Sans MS', family: 'Comic Sans MS, cursive' },
  { name: 'OpenDyslexic', family: 'OpenDyslexic, sans-serif' },
  { name: 'Schulschrift', family: 'Schulschrift, cursive' },
  { name: 'Courier New', family: 'Courier New, monospace' },
]

// Export Format
export type ExportFormat = 'pdf' | 'png' | 'jpg' | 'json'

// Export Options
export interface ExportOptions {
  format: ExportFormat
  quality?: number // 0-1 for images
  includeBackground?: boolean
  scale?: number
}
