/** Default layout matching dsd-g1 hardcoded HUD (128x64). */
export const CANVAS_W = 128;
export const CANVAS_H = 64;
export const MAX_CUSTOM_RETICLES = 10;
export const CUSTOM_RETICLE_ID_MIN = 16;
export const CUSTOM_RETICLE_ID_MAX = 25;
export const RETICLE_SCALE_MIN = 2;
export const RETICLE_SCALE_MAX = 60;

/** Minimum drawable sizes per element/mode (pixels). */
export const MIN_SIZE = {
  range: { numeric: { w: 16, h: 8 } },
  heading: { numeric: { w: 20, h: 8 }, graphic: { w: 8, h: 8 } },
  stability: { numeric: { w: 12, h: 8 }, graphic: { w: 8, h: 2 } },
  height: { numeric: { w: 12, h: 8 } },
  temp: { numeric: { w: 12, h: 8 } },
  ammo: { numeric: { w: 12, h: 8 }, graphic: { w: 3, h: 8 } },
  pitch: { numeric: { w: 12, h: 8 }, graphic: { w: 6, h: 1 } },
  reticle: { none: { w: 2, h: 2 } }
};

export const MODE_LABELS = {
  numeric: "数值",
  graphic: "图形",
  none: "—"
};

/** pitch graphic shapes */
export const PITCH_SHAPES = [
  { id: 0, label: "双侧横线" },
  { id: 1, label: "单侧横线" },
  { id: 2, label: "双侧短杠" },
  { id: 3, label: "双侧括号" }
];

/**
 * movable: can drag on canvas
 * modes: allowed modes (JSON values)
 */
export const ELEMENT_DEFS = [
  { id: "range", label: "测距", modes: ["numeric"], movable: true, size: true, digit: true },
  { id: "heading", label: "航向", modes: ["numeric", "graphic"], movable: true, size: true, digit: true },
  { id: "stability", label: "稳定度", modes: ["numeric", "graphic"], movable: true, size: true, digit: true },
  { id: "height", label: "高度", modes: ["numeric"], movable: true, size: true, digit: true },
  { id: "temp", label: "温度", modes: ["numeric"], movable: true, size: true, digit: true },
  { id: "ammo", label: "弹药", modes: ["numeric", "graphic"], movable: true, size: true, digit: true },
  { id: "pitch", label: "俯仰", modes: ["numeric", "graphic"], movable: true, size: true, digit: true, shapes: true },
  { id: "reticle", label: "准星", modes: ["none"], movable: false, size: false, digit: false }
];

export function defById(id) {
  return ELEMENT_DEFS.find((d) => d.id === id);
}

export function defaultLayout() {
  return {
    product: "dsd_g1",
    schema: 1,
    canvas: { w: CANVAS_W, h: CANVAS_H },
    elements: [
      { id: "range", enabled: true, mode: "numeric", x: 24, y: 8, w: 32, h: 8, digitScale: 1, shape: 0 },
      { id: "heading", enabled: true, mode: "graphic", x: 86, y: 4, w: 18, h: 8, digitScale: 1, shape: 0 },
      { id: "stability", enabled: true, mode: "graphic", x: 44, y: 61, w: 24, h: 3, digitScale: 1, shape: 0 },
      { id: "height", enabled: false, mode: "numeric", x: 90, y: 50, w: 14, h: 8, digitScale: 1, shape: 0 },
      { id: "temp", enabled: false, mode: "numeric", x: 90, y: 58, w: 12, h: 8, digitScale: 1, shape: 0 },
      { id: "ammo", enabled: true, mode: "graphic", x: 24, y: 52, w: 4, h: 12, digitScale: 1, shape: 0 },
      { id: "pitch", enabled: true, mode: "graphic", x: 2, y: 30, w: 10, h: 3, digitScale: 1, shape: 0 },
      { id: "reticle", enabled: true, mode: "none", x: 60, y: 28, w: 9, h: 9, digitScale: 1, shape: 0 }
    ],
    reticle: { scaleBox: 9, defaultStyle: 0 },
    reticles: []
  };
}

/** Sync reticle element box to screen center; position not user-editable. */
export function syncReticleBox(layout) {
  const box = Math.max(RETICLE_SCALE_MIN, Math.min(RETICLE_SCALE_MAX, layout.reticle.scaleBox | 0 || 9));
  layout.reticle.scaleBox = box;
  const el = layout.elements.find((e) => e.id === "reticle");
  if (!el) return;
  el.mode = "none";
  el.w = box;
  el.h = box;
  el.x = Math.floor((CANVAS_W - box) / 2);
  el.y = Math.floor((CANVAS_H - box) / 2);
}

/** Coerce imported/draft layouts to current rules. */
export function normalizeLayout(doc) {
  const base = defaultLayout();
  if (!doc || typeof doc !== "object") return base;
  const out = {
    product: "dsd_g1",
    schema: 1,
    canvas: { w: CANVAS_W, h: CANVAS_H },
    elements: [],
    reticle: {
      scaleBox: Math.max(RETICLE_SCALE_MIN, Math.min(RETICLE_SCALE_MAX, doc.reticle?.scaleBox ?? 9)),
      defaultStyle: doc.reticle?.defaultStyle ?? 0
    },
    reticles: Array.isArray(doc.reticles) ? doc.reticles.slice(0, MAX_CUSTOM_RETICLES) : []
  };
  for (const def of ELEMENT_DEFS) {
    const src = (doc.elements || []).find((e) => e.id === def.id) || base.elements.find((e) => e.id === def.id);
    let mode = src.mode;
    if (!def.modes.includes(mode)) mode = def.modes[0];
    out.elements.push({
      id: def.id,
      enabled: src.enabled !== false,
      mode,
      x: src.x | 0,
      y: src.y | 0,
      w: src.w | 0,
      h: src.h | 0,
      digitScale: Math.max(1, Math.min(3, src.digitScale | 0 || 1)),
      shape: Math.max(0, Math.min(3, src.shape | 0 || 0))
    });
  }
  syncReticleBox(out);
  return out;
}

/** Built-in style 0 cross preview (relative pixels). */
export const BUILTIN_STYLE0 = [
  [-4, 0], [-3, 0], [-2, 0], [-1, 0], [0, 0], [1, 0], [2, 0], [3, 0], [4, 0],
  [0, -4], [0, -3], [0, -2], [0, -1], [0, 1], [0, 2], [0, 3], [0, 4]
];
