/**
 * useCoverStyle - 封面视觉升级（文字海报风格）
 * 
 * 提供统一的封面样式计算，类似 Apple Books 风格：
 * 超大首字母（半透明装饰）+ 书名 + 章节进度
 */

// 统一音色封面配色方案
const VOICE_COLORS: Record<string, string[]> = {
  cherry: ['#667eea', '#764ba2'],
  ethan: ['#f093fb', '#f5576c'],
  serena: ['#4facfe', '#00f2fe'],
  chelsie: ['#43e97b', '#38f9d7'],
  momo: ['#fa709a', '#fee140'],
  vivian: ['#a8edea', '#fed6e3'],
  moon: ['#5ee7df', '#b490ca'],
  maia: ['#d299c2', '#fef9d7'],
  kai: ['#89f7fe', '#66a6ff'],
  nofish: ['#cd9cf2', '#f6f3ff'],
};

// 通用书籍封面配色（基于 ID 轮转）
const BOOK_COLORS = [
  ['#4f46e5', '#818cf8'],
  ['#ef4444', '#f87171'],
  ['#f97316', '#fb923c'],
  ['#22c55e', '#4ade80'],
  ['#3b82f6', '#60a5fa'],
  ['#a855f7', '#c084fc'],
  ['#ec4899', '#f472b6'],
  ['#14b8a6', '#2dd4bf'],
];

/**
 * 根据音色ID获取渐变色
 */
export function getCoverGradient(voiceId?: string | null): string {
  if (voiceId && VOICE_COLORS[voiceId]) {
    const colors = VOICE_COLORS[voiceId];
    return `linear-gradient(135deg, ${colors[0]} 0%, ${colors[1]} 100%)`;
  }
  return `linear-gradient(135deg, #667eea 0%, #764ba2 100%)`;
}

/**
 * 基于数值ID获取轮转渐变色
 */
export function getBookGradient(id: number): string {
  const colors = BOOK_COLORS[id % BOOK_COLORS.length] || BOOK_COLORS[0];
  return `linear-gradient(135deg, ${colors[0]} 0%, ${colors[1]} 100%)`;
}

/**
 * 获取字符串哈希对应的渐变色
 */
export function getHashGradient(str: string): string {
  const gradients = [
    'linear-gradient(135deg, #667eea 0%, #764ba2 100%)',
    'linear-gradient(135deg, #f093fb 0%, #f5576c 100%)',
    'linear-gradient(135deg, #4facfe 0%, #00f2fe 100%)',
    'linear-gradient(135deg, #43e97b 0%, #38f9d7 100%)',
    'linear-gradient(135deg, #fa709a 0%, #fee140 100%)',
    'linear-gradient(135deg, #a8edea 0%, #fed6e3 100%)',
    'linear-gradient(135deg, #5ee7df 0%, #b490ca 100%)',
  ];
  let hash = 0;
  for (let i = 0; i < str.length; i++) {
    hash = str.charCodeAt(i) + ((hash << 5) - hash);
  }
  return gradients[Math.abs(hash) % gradients.length];
}

/**
 * 获取标题首字母作为装饰文字
 */
export function getTitleLetter(title: string): string {
  if (!title) return '?';
  const first = title.trim().charAt(0);
  // 中文字符直接返回
  if (/[\u4e00-\u9fa5]/.test(first)) {
    return first;
  }
  return first.toUpperCase();
}

/**
 * 获取增强封面样式对象（Apple Books 风格）
 * 返回完整的封面展示数据
 */
export interface CoverStyle {
  background: string;
  decorLetter: string;
  decoratedTitle?: string;
  progress?: number;
  progressText?: string;
}

export function getCoverStyle(params: {
  id?: number;
  voiceId?: string | null;
  title?: string;
  progress?: number;
  totalChapters?: number;
  completedChapters?: number;
}): CoverStyle {
  const { id, voiceId, title, progress, totalChapters, completedChapters } = params;
  
  // 确定渐变色
  let background: string;
  if (id !== undefined) {
    background = getBookGradient(id);
  } else if (voiceId) {
    background = getCoverGradient(voiceId);
  } else {
    background = getCoverGradient(null);
  }
  
  // 装饰首字母
  const decorLetter = getTitleLetter(title || '');
  
  // 进度信息
  let progressText: string | undefined;
  let computedProgress: number | undefined;
  
  if (totalChapters && totalChapters > 0 && completedChapters !== undefined) {
    computedProgress = Math.round((completedChapters / totalChapters) * 100);
    progressText = `${completedChapters}/${totalChapters}章`;
  } else if (progress !== undefined) {
    computedProgress = progress;
    progressText = `${progress}%`;
  }
  
  return {
    background,
    decorLetter,
    decoratedTitle: title,
    progress: computedProgress,
    progressText,
  };
}

/**
 * 导出配色方案供直接使用
 */
export { VOICE_COLORS, BOOK_COLORS };
