export type AvatarEditorRatio = "16:9" | "9:16";

export type AvatarEditorLayerId = "solidBackground" | "background" | "avatar" | "sticker";

export interface AvatarEditorLayer {
  id: AvatarEditorLayerId;
  label: string;
  tool: "avatar" | "content" | "subtitle" | "background" | "sticker";
  x: number;
  y: number;
  width: number;
  height: number;
  zIndex: number;
  locked?: boolean;
}

export interface AvatarEditorPoint {
  x: number;
  y: number;
}

function clamp(value: number, min: number, max: number): number {
  return Math.min(max, Math.max(min, value));
}

function normalizeZIndexes(layers: AvatarEditorLayer[]): AvatarEditorLayer[] {
  return [...layers]
    .sort((a, b) => a.zIndex - b.zIndex)
    .map((layer, index) => ({ ...layer, zIndex: index * 10 }));
}

function assignZIndexes(layers: AvatarEditorLayer[]): AvatarEditorLayer[] {
  return layers.map((layer, index) => ({ ...layer, zIndex: index * 10 }));
}

export function createDefaultAvatarEditorLayers(ratio: AvatarEditorRatio): AvatarEditorLayer[] {
  const isPortrait = ratio === "9:16";
  return [
    {
      id: "solidBackground",
      label: "纯色背景",
      tool: "background",
      x: 0,
      y: 0,
      width: 100,
      height: 100,
      zIndex: 0,
      locked: true,
    },
    {
      id: "background",
      label: "背景",
      tool: "background",
      x: 0,
      y: 0,
      width: 100,
      height: 100,
      zIndex: 10,
      locked: true,
    },
    {
      id: "avatar",
      label: "数字人",
      tool: "avatar",
      x: isPortrait ? 30 : 42,
      y: isPortrait ? 24 : 12,
      width: isPortrait ? 40 : 23,
      height: isPortrait ? 70 : 88,
      zIndex: 20,
    },
    {
      id: "sticker",
      label: "贴图",
      tool: "sticker",
      x: isPortrait ? 62 : 46,
      y: isPortrait ? 42 : 12,
      width: isPortrait ? 24 : 8,
      height: isPortrait ? 40 : 12,
      zIndex: 30,
    },
  ];
}

export function moveAvatarEditorLayer(
  layers: AvatarEditorLayer[],
  layerId: AvatarEditorLayerId,
  nextPosition: AvatarEditorPoint,
): AvatarEditorLayer[] {
  return layers.map((layer) => {
    if (layer.id !== layerId || layer.locked) return layer;
    return {
      ...layer,
      x: clamp(nextPosition.x, 0, 100 - layer.width),
      y: clamp(nextPosition.y, 0, 100 - layer.height),
    };
  });
}

export function bringAvatarEditorLayerForward(
  layers: AvatarEditorLayer[],
  layerId: AvatarEditorLayerId,
): AvatarEditorLayer[] {
  const ordered = normalizeZIndexes(layers);
  const index = ordered.findIndex((layer) => layer.id === layerId);
  if (index < 0 || index >= ordered.length - 1) return ordered;
  const next = [...ordered];
  [next[index], next[index + 1]] = [next[index + 1], next[index]];
  return assignZIndexes(next);
}

export function sendAvatarEditorLayerBackward(
  layers: AvatarEditorLayer[],
  layerId: AvatarEditorLayerId,
): AvatarEditorLayer[] {
  const ordered = normalizeZIndexes(layers);
  const index = ordered.findIndex((layer) => layer.id === layerId);
  if (index <= 0) return ordered;
  const next = [...ordered];
  [next[index - 1], next[index]] = [next[index], next[index - 1]];
  return assignZIndexes(next);
}
