import React from "react";
import { Text as SvgText, type SvgProps } from "react-native-svg";

export interface TextItemConfig {
  text: string;
  fontSize?: number;
  fontId?: string;
  offsetY?: number; // Dikey konum kaydırma (yukarı / aşağı)
  visible?: boolean;
}

export interface QrCustomFields {
  restaurantName: TextItemConfig;
  subtitle: TextItemConfig;
  tableNo: TextItemConfig;
  ctaText: TextItemConfig;
  wifiText: TextItemConfig;
  socialText: TextItemConfig;
}

export interface QrTemplateProps extends SvgProps {
  qrBase64?: string;
  fields?: Partial<QrCustomFields>;
  hideQr?: boolean;
}

import { Platform } from "react-native";

// Türkçe karakterleri (İ, ı, Ş, ş, Ğ, ğ, Ü, ü, Ö, ö, Ç, ç) eksiksiz destekleyen sistem fontları
export const FONT_OPTIONS = [
  {
    id: "sans",
    label: "Modern Sans",
    fontFamily: Platform.OS === "ios" ? "System" : "sans-serif",
  },
  {
    id: "serif",
    label: "Zarif Serif",
    fontFamily: Platform.OS === "ios" ? "Georgia" : "serif",
  },
  {
    id: "times",
    label: "Klasik (Times)",
    fontFamily: Platform.OS === "ios" ? "Times New Roman" : "serif",
  },
  {
    id: "palatino",
    label: "Lüks Palatino",
    fontFamily: Platform.OS === "ios" ? "Palatino" : "serif",
  },
  {
    id: "trebuchet",
    label: "Dinamik",
    fontFamily: Platform.OS === "ios" ? "Trebuchet MS" : "sans-serif",
  },
];

export function getFontFamily(fontId?: string, defaultFamily = "Georgia"): string {
  if (!fontId) {
    return defaultFamily.includes(",") ? defaultFamily.split(",")[0].trim().replace(/['"]/g, "") : defaultFamily;
  }
  const found = FONT_OPTIONS.find((f) => f.id === fontId);
  if (found) return found.fontFamily;
  if (fontId.includes(",")) {
    return fontId.split(",")[0].trim().replace(/['"]/g, "");
  }
  return fontId;
}

// Çok satırlı (Enter / \n) ve dikey kaydırmalı (offsetY) SVG metin render fonksiyonu
export function renderSvgLines({
  text,
  x,
  y,
  offsetY = 0,
  lineHeight,
  fill,
  fontFamily,
  fontSize,
  fontWeight = "normal",
  letterSpacing,
  textAnchor = "middle",
}: {
  text?: string;
  x: number;
  y: number;
  offsetY?: number;
  lineHeight?: number;
  fill?: string;
  fontFamily?: string;
  fontSize?: number;
  fontWeight?: string | number;
  letterSpacing?: number;
  textAnchor?: "middle" | "start" | "end";
}) {
  if (!text) return null;
  const lines = text.split("\n");
  const computedLineHeight = lineHeight || (fontSize ? fontSize * 1.25 : 18);
  const startY = y + (offsetY || 0);

  return (
    <>
      {lines.map((line, idx) => (
        <SvgText
          key={idx}
          x={x}
          y={startY + idx * computedLineHeight}
          fill={fill}
          fontFamily={fontFamily}
          fontSize={fontSize}
          fontWeight={fontWeight}
          letterSpacing={letterSpacing}
          textAnchor={textAnchor}
        >
          {line}
        </SvgText>
      ))}
    </>
  );
}
