import { Platform } from "react-native";

export interface CanvasTextLayer {
  id: string;
  text: string;
  x: number; // px from canvas left
  y: number; // px from canvas top
  rotation: number; // degrees (-180 to 180)
  fontSize: number;
  fontId: string;
  color: string;
  bgBoxColor?: string; // Instagram badge kutusu
  bgStyle?: "none" | "solid" | "soft" | "outline";
  textAlign?: "left" | "center" | "right";
  hasShadow?: boolean;
  letterSpacing?: number;
  fontWeight?: "normal" | "600" | "700" | "800" | "900";
  curveDegree?: number; // -100 (Aşağı Kavis ◡) ile +100 (Yukarı Kavis ◠) arası kavis derecesi
}

export interface CanvasQrConfig {
  x: number;
  y: number;
  size: number;
  borderRadius: number;
  borderWidth: number;
  borderColor: string;
  padding: number;
  bgBoxColor: string;
}

export const CANVAS_FONT_OPTIONS = [
  {
    id: "sans",
    label: "MODERN",
    fontFamily: Platform.OS === "ios" ? "System" : "sans-serif",
  },
  {
    id: "serif",
    label: "ZARİF",
    fontFamily: Platform.OS === "ios" ? "Georgia" : "serif",
  },
  {
    id: "times",
    label: "KLASİK",
    fontFamily: Platform.OS === "ios" ? "Times New Roman" : "serif",
  },
  {
    id: "typewriter",
    label: "DAKTİLO",
    fontFamily: Platform.OS === "ios" ? "Courier" : "monospace",
  },
  {
    id: "palatino",
    label: "LÜKS",
    fontFamily: Platform.OS === "ios" ? "Palatino" : "serif",
  },
  {
    id: "trebuchet",
    label: "DİNAMİK",
    fontFamily: Platform.OS === "ios" ? "Trebuchet MS" : "sans-serif",
  },
  {
    id: "heavy",
    label: "GÜÇLÜ",
    fontFamily: Platform.OS === "ios" ? "Impact" : "sans-serif-black",
  },
];

export function getCanvasFontFamily(fontId?: string): string {
  if (!fontId) return Platform.OS === "ios" ? "System" : "sans-serif";
  const found = CANVAS_FONT_OPTIONS.find((f) => f.id === fontId);
  if (found) {
    return found.fontFamily;
  }
  if (fontId.includes(",")) {
    const first = fontId.split(",")[0].trim().replace(/['"]/g, "");
    return first || (Platform.OS === "ios" ? "System" : "sans-serif");
  }
  return fontId;
}

export const CANVAS_COLOR_PALETTE = [
  { id: "gold", color: "#D4AF37", label: "Altın" },
  { id: "white", color: "#FFFFFF", label: "Beyaz" },
  { id: "black", color: "#121215", label: "Siyah" },
  { id: "cream", color: "#F6EFE3", label: "Krem" },
  { id: "emerald", color: "#10B981", label: "Yeşil" },
  { id: "crimson", color: "#EF4444", label: "Kırmızı" },
  { id: "amber", color: "#F59E0B", label: "Amber" },
  { id: "rose", color: "#EC4899", label: "Pembe" },
  { id: "indigo", color: "#6366F1", label: "Lila" },
  { id: "cyan", color: "#06B6D4", label: "Turkuaz" },
];

export const INSTAGRAM_BADGE_STYLES = [
  { id: "none", label: "Düz", bgBoxColor: undefined },
  { id: "dark", label: "Koyu", bgBoxColor: "rgba(18, 18, 21, 0.88)" },
  { id: "gold", label: "Altın", bgBoxColor: "rgba(212, 175, 55, 0.92)" },
  { id: "white", label: "Beyaz", bgBoxColor: "rgba(255, 255, 255, 0.92)" },
  { id: "glass", label: "Cam", bgBoxColor: "rgba(255, 255, 255, 0.2)" },
];

export const QR_SIZE_PRESETS = [
  { label: "Kompakt", size: 85 },
  { label: "Standart", size: 115 },
  { label: "Büyük", size: 145 },
  { label: "Geniş", size: 175 },
];

export const QR_RADIUS_PRESETS = [
  { label: "Düz", radius: 0 },
  { label: "Hafif", radius: 10 },
  { label: "Yuvarlak", radius: 18 },
  { label: "Oval", radius: 28 },
];

export const QR_BORDER_COLORS = [
  { id: "gold", color: "#D4AF37", label: "Altın" },
  { id: "black", color: "#121215", label: "Siyah" },
  { id: "white", color: "#FFFFFF", label: "Beyaz" },
  { id: "none", color: "transparent", label: "Yok" },
];

export function getDefaultQrConfigForTemplate(
  templateKey: string,
  canvasWidth: number,
  canvasHeight: number
): CanvasQrConfig {
  if (templateKey === "qr-1" || templateKey === "qr-8" || templateKey === "qr-9") {
    const size = 110;
    return {
      x: Math.round((canvasWidth - size) / 2),
      y: 102,
      size,
      borderRadius: 14,
      borderWidth: 1.5,
      borderColor: "#D4AF37",
      padding: 6,
      bgBoxColor: "#FFFFFF",
    };
  }

  // Standart Dikey Şablonlar (qr-2, qr-3, qr-4, qr-5, qr-6)
  const size = 125;
  return {
    x: Math.round((canvasWidth - size) / 2),
    y: Math.round(canvasHeight * 0.36),
    size,
    borderRadius: 14,
    borderWidth: 1.5,
    borderColor: "#D4AF37",
    padding: 6,
    bgBoxColor: "#FFFFFF",
  };
}

// Her şablon için başlangıç varsayılan katmanları (1:1 veya standart orana göre)
export function getDefaultLayersForTemplate(templateKey: string, restaurantName = "Gourmet Bistro"): CanvasTextLayer[] {
  if (templateKey === "qr-1") {
    return [
      {
        id: "rName",
        text: restaurantName.toUpperCase(),
        x: 40,
        y: 50,
        rotation: 0,
        fontSize: 18,
        fontId: "serif",
        color: "#D4AF37",
        fontWeight: "800",
        letterSpacing: 2,
        textAlign: "center",
      },
      {
        id: "sub",
        text: "RESTAURANT & BAR",
        x: 65,
        y: 78,
        rotation: 0,
        fontSize: 9,
        fontId: "sans",
        color: "#E5C378",
        fontWeight: "600",
        letterSpacing: 1.5,
        textAlign: "center",
      },
      {
        id: "cta",
        text: "MENÜYÜ GÖRMEK İÇİN OKUTUN",
        x: 35,
        y: 236,
        rotation: 0,
        fontSize: 10,
        fontId: "serif",
        color: "#D4AF37",
        fontWeight: "700",
        letterSpacing: 1,
        textAlign: "center",
      },
      {
        id: "table",
        text: "MASA NO: 01   •   WiFi: Bistro",
        x: 45,
        y: 265,
        rotation: 0,
        fontSize: 9,
        fontId: "sans",
        color: "#C5A059",
        fontWeight: "600",
        textAlign: "center",
      },
    ];
  }

  if (templateKey === "qr-8") {
    return [
      {
        id: "rName",
        text: "EXPLORE OUR MENU",
        x: 35,
        y: 35,
        rotation: 0,
        fontSize: 16,
        fontId: "sans",
        color: "#FFFFFF",
        fontWeight: "900",
        letterSpacing: 1,
        textAlign: "center",
      },
      {
        id: "sub",
        text: "THE DAILY SPECIALS",
        x: 50,
        y: 68,
        rotation: -4,
        fontSize: 13,
        fontId: "sans",
        color: "#D1A752",
        fontWeight: "800",
        textAlign: "center",
      },
      {
        id: "cta",
        text: "SCAN TO VIEW\nFULL MENU & SPECIALS",
        x: 55,
        y: 215,
        rotation: 0,
        fontSize: 9.5,
        fontId: "sans",
        color: "#FFFFFF",
        fontWeight: "800",
        textAlign: "center",
      },
      {
        id: "wifi",
        text: "SCAN TO ORDER & BROWSE",
        x: 55,
        y: 258,
        rotation: 0,
        fontSize: 9,
        fontId: "sans",
        color: "#D1A752",
        fontWeight: "800",
        textAlign: "center",
      },
    ];
  }

  // Varsayılan
  return [
    {
      id: "rName",
      text: restaurantName,
      x: 50,
      y: 45,
      rotation: 0,
      fontSize: 17,
      fontId: "serif",
      color: "#D4AF37",
      fontWeight: "800",
      textAlign: "center",
    },
    {
      id: "sub",
      text: "SCAN TO ORDER",
      x: 75,
      y: 75,
      rotation: 0,
      fontSize: 10,
      fontId: "sans",
      color: "#FFFFFF",
      fontWeight: "700",
      textAlign: "center",
    },
    {
      id: "cta",
      text: "DİJİTAL MENÜ",
      x: 75,
      y: 245,
      rotation: 0,
      fontSize: 11,
      fontId: "serif",
      color: "#D4AF37",
      fontWeight: "800",
      textAlign: "center",
    },
  ];
}
