/**
 * Live Preview Types
 */

export interface PreviewInstance {
  id: string;
  className: string;
  name: string;
  position: { x: number; y: number; z: number };
  rotation: { x: number; y: number; z: number };
  scale: { x: number; y: number; z: number };
  color: { r: number; g: number; b: number };
  transparency: number;
  material: string;
  shape: 'Block' | 'Ball' | 'Cylinder' | 'Wedge';
  visible: boolean;
  children: PreviewInstance[];
  properties: Record<string, any>;
}

export interface PreviewLight {
  id: string;
  type: 'point' | 'spot' | 'directional' | 'ambient';
  position: { x: number; y: number; z: number };
  color: { r: number; g: number; b: number };
  intensity: number;
  range?: number;
  angle?: number;
  castShadow: boolean;
}

export interface PreviewCamera {
  position: { x: number; y: number; z: number };
  target: { x: number; y: number; z: number };
  fov: number;
  near: number;
  far: number;
}

export interface PreviewScene {
  instances: PreviewInstance[];
  lights: PreviewLight[];
  camera: PreviewCamera;
  skybox: string | null;
  ambientColor: { r: number; g: number; b: number };
  ambientIntensity: number;
  fogEnabled: boolean;
  fogColor: { r: number; g: number; b: number };
  fogNear: number;
  fogFar: number;
}

export interface ConsoleOutput {
  id: string;
  type: 'log' | 'warn' | 'error' | 'info';
  message: string;
  timestamp: number;
  source?: string;
  line?: number;
}

export interface PreviewSettings {
  showGrid: boolean;
  showAxes: boolean;
  showStats: boolean;
  showWireframe: boolean;
  shadowsEnabled: boolean;
  antialias: boolean;
  autoRotate: boolean;
  backgroundColor: string;
}

export const DEFAULT_PREVIEW_SETTINGS: PreviewSettings = {
  showGrid: true,
  showAxes: true,
  showStats: false,
  showWireframe: false,
  shadowsEnabled: true,
  antialias: true,
  autoRotate: false,
  backgroundColor: '#1a1a2e',
};

export const DEFAULT_CAMERA: PreviewCamera = {
  position: { x: 10, y: 8, z: 10 },
  target: { x: 0, y: 0, z: 0 },
  fov: 70,
  near: 0.1,
  far: 1000,
};

export function createDefaultScene(): PreviewScene {
  return {
    instances: [
      // Default baseplate
      {
        id: 'baseplate',
        className: 'Part',
        name: 'Baseplate',
        position: { x: 0, y: -0.5, z: 0 },
        rotation: { x: 0, y: 0, z: 0 },
        scale: { x: 100, y: 1, z: 100 },
        color: { r: 0.3, g: 0.5, b: 0.3 },
        transparency: 0,
        material: 'Grass',
        shape: 'Block',
        visible: true,
        children: [],
        properties: {},
      },
      // Default spawn
      {
        id: 'spawnlocation',
        className: 'SpawnLocation',
        name: 'SpawnLocation',
        position: { x: 0, y: 0.5, z: 0 },
        rotation: { x: 0, y: 0, z: 0 },
        scale: { x: 6, y: 1, z: 6 },
        color: { r: 0.2, g: 0.6, b: 0.2 },
        transparency: 0,
        material: 'Plastic',
        shape: 'Block',
        visible: true,
        children: [],
        properties: {},
      },
    ],
    lights: [
      {
        id: 'sunlight',
        type: 'directional',
        position: { x: 50, y: 100, z: 50 },
        color: { r: 1, g: 0.98, b: 0.9 },
        intensity: 1.5,
        castShadow: true,
      },
      {
        id: 'ambient',
        type: 'ambient',
        position: { x: 0, y: 0, z: 0 },
        color: { r: 0.4, g: 0.4, b: 0.5 },
        intensity: 0.5,
        castShadow: false,
      },
    ],
    camera: DEFAULT_CAMERA,
    skybox: null,
    ambientColor: { r: 0.5, g: 0.5, b: 0.6 },
    ambientIntensity: 0.5,
    fogEnabled: false,
    fogColor: { r: 0.8, g: 0.85, b: 0.9 },
    fogNear: 100,
    fogFar: 500,
  };
}

/**
 * Convert Roblox API instance to preview instance
 */
export function convertToPreviewInstance(instance: any, id: string): PreviewInstance {
  const props = instance.properties || {};

  const position = props.Position || { X: 0, Y: 0, Z: 0 };
  const size = props.Size || { X: 4, Y: 1, Z: 2 };
  const color = props.Color || { R: 0.6, G: 0.6, B: 0.6 };

  return {
    id,
    className: instance.ClassName,
    name: instance.Name || instance.ClassName,
    position: {
      x: position.X || 0,
      y: position.Y || 0,
      z: position.Z || 0,
    },
    rotation: { x: 0, y: 0, z: 0 },
    scale: {
      x: size.X || 4,
      y: size.Y || 1,
      z: size.Z || 2,
    },
    color: {
      r: color.R || 0.6,
      g: color.G || 0.6,
      b: color.B || 0.6,
    },
    transparency: props.Transparency || 0,
    material: props.Material || 'Plastic',
    shape: props.Shape || 'Block',
    visible: true,
    children: (instance.children || []).map((child: any, i: number) =>
      convertToPreviewInstance(child, `${id}-${i}`)
    ),
    properties: props,
  };
}

/**
 * Material to Three.js mapping
 */
export const MATERIAL_PROPERTIES: Record<string, {
  roughness: number;
  metalness: number;
  emissive?: boolean;
}> = {
  Plastic: { roughness: 0.5, metalness: 0.0 },
  SmoothPlastic: { roughness: 0.2, metalness: 0.0 },
  Wood: { roughness: 0.8, metalness: 0.0 },
  Metal: { roughness: 0.3, metalness: 0.9 },
  Glass: { roughness: 0.1, metalness: 0.0 },
  Neon: { roughness: 0.0, metalness: 0.0, emissive: true },
  Grass: { roughness: 0.9, metalness: 0.0 },
  Sand: { roughness: 1.0, metalness: 0.0 },
  Brick: { roughness: 0.9, metalness: 0.0 },
  Concrete: { roughness: 0.95, metalness: 0.0 },
  Ice: { roughness: 0.1, metalness: 0.0 },
  Marble: { roughness: 0.2, metalness: 0.1 },
  Granite: { roughness: 0.85, metalness: 0.05 },
  ForceField: { roughness: 0.0, metalness: 0.0, emissive: true },
};
