import type {
  AppearanceBackgroundPosition,
  AppearanceDecorationScope,
  PortalAppearance,
} from "@shared/appearance";

export const backgroundPositionCssMap: Record<AppearanceBackgroundPosition, string> = {
  center: "center",
  top: "center top",
  bottom: "center bottom",
  left: "left center",
  right: "right center",
  top_left: "left top",
  top_right: "right top",
  bottom_left: "left bottom",
  bottom_right: "right bottom",
};

export function getBackgroundScopeForPath(path: string) {
  if (path === "/") return "home";
  if (path.startsWith("/associations/carte")) return "public_map";
  if (path.startsWith("/associations")) return "public_directory";
  if (path.startsWith("/dashboard/profile")) return "dashboard_profile";
  if (path.startsWith("/dashboard/documents")) return "dashboard_documents";
  if (path.startsWith("/dashboard/requests/new")) return "dashboard_new_request";
  if (path.startsWith("/dashboard/reservation-salle")) return "dashboard_reservation_salle";
  if (path.startsWith("/dashboard/materiel-evenementiel")) return "dashboard_material";
  if (path.startsWith("/dashboard/associations")) return "dashboard_directory";
  if (path.startsWith("/dashboard/requests")) return "dashboard_requests";
  if (path.startsWith("/dashboard")) return "dashboard_home";
  if (path === "/admin" || path.startsWith("/admin?")) {
    const query = path.split("?")[1] || "";
    const tab = new URLSearchParams(query).get("tab") || "dashboard";
    if (["logistics", "material-requests"].includes(tab)) return "admin_logistics";
    if (["directory", "ccds-directory"].includes(tab)) return "admin_directory";
    if (["compliance", "appearance"].includes(tab)) return "admin_compliance";
    return "admin_pilotage";
  }
  return "public";
}

export function getBackgroundScopeAliases(scope: string) {
  if (scope.startsWith("public_")) return [scope, "public"];
  if (scope.startsWith("dashboard_")) return [scope, "dashboard"];
  if (scope.startsWith("admin_")) return [scope, "admin"];
  return [scope];
}

export function isDecorationScopeActive(decorationScope: AppearanceDecorationScope, currentScope: string) {
  if (decorationScope === "all") return true;
  const aliases = getBackgroundScopeAliases(currentScope);
  return aliases.includes(decorationScope as string) || currentScope === decorationScope;
}

export function resolveBackgroundImageForScope(
  scope: string,
  appearance: PortalAppearance,
) {
  const aliases = getBackgroundScopeAliases(scope);

  for (const candidate of aliases) {
    const specificImage = appearance.backgroundTargetImageUrls?.[candidate as keyof typeof appearance.backgroundTargetImageUrls];
    if (specificImage) {
      return specificImage;
    }
  }

  return appearance.backgroundImageUrl || "";
}

export function resolveBackgroundOverlayOpacityForScope(
  scope: string,
  appearance: PortalAppearance,
) {
  const aliases = getBackgroundScopeAliases(scope);

  for (const candidate of aliases) {
    const specificOpacity =
      appearance.backgroundTargetOverlayOpacities?.[candidate as keyof typeof appearance.backgroundTargetOverlayOpacities];
    if (typeof specificOpacity === "number") {
      return specificOpacity;
    }
  }

  return appearance.backgroundOverlayOpacity;
}

export function resolveBackgroundPositionForScope(
  scope: string,
  appearance: PortalAppearance,
) {
  const aliases = getBackgroundScopeAliases(scope);

  for (const candidate of aliases) {
    const specificPosition =
      appearance.backgroundTargetPositions?.[candidate as keyof typeof appearance.backgroundTargetPositions];
    if (specificPosition) {
      return backgroundPositionCssMap[specificPosition];
    }
  }

  return appearance.backgroundMode === "repeat" ? "top left" : backgroundPositionCssMap.center;
}

export function resolveBackgroundBlurForScope(
  scope: string,
  appearance: PortalAppearance,
) {
  const aliases = getBackgroundScopeAliases(scope);

  for (const candidate of aliases) {
    const specificBlur =
      appearance.backgroundTargetBlurRadii?.[candidate as keyof typeof appearance.backgroundTargetBlurRadii];
    if (typeof specificBlur === "number") {
      return specificBlur;
    }
  }

  return appearance.backgroundBlurRadius || 0;
}

export function resolveBackgroundPresentationModeForScope(
  scope: string,
  appearance: PortalAppearance,
) {
  const aliases = getBackgroundScopeAliases(scope);

  for (const candidate of aliases) {
    const specificMode =
      appearance.backgroundTargetPresentationModes?.[candidate as keyof typeof appearance.backgroundTargetPresentationModes];
    if (specificMode) {
      return specificMode;
    }
  }

  return appearance.backgroundPresentationMode || "full";
}
