// 难度页当前用于保存“开始训练前上下文”的本地存储 key。
export const trainingDifficultyStorageKey = "training-selection";

// 当前是页面联调用的模拟数据：
// 1. 总难度 20 级
// 2. 已解锁到 Lv3
// 3. 前三关展示不同完成进度
const totalDifficultyLevel = 20;
const mockedUnlockedDifficultyId = "lv3";
const mockedDifficultyProgressMap = {
  lv1: 100,
  lv2: 90,
  lv3: 70,
};

const modeList = [
  {
    key: "endurance",
    title: "耐力训练",
  },
  {
    key: "precision",
    title: "精准训练",
  },
  {
    key: "rhythm",
    title: "节奏训练",
  },
  {
    key: "basic",
    title: "基础训练",
  },
  {
    key: "power",
    title: "力量训练",
  },
  {
    key: "focus",
    title: "专注训练",
  },
];

const createDifficultyId = (level) => `lv${level}`;

const createDifficultyLabel = (level) => `Lv${level}`;

// 根据等级生成模拟文案，方便一次性扩展到更多关卡。
const createDifficultySummary = (level) => {
  return [
    `箭靶划分为${Math.min(1 + Math.floor((level - 1) / 5), 4)}个区域`,
    `需${4 + level}次命中目标`,
    `${100 + Math.floor((level - 1) / 2) * 10}秒内完成所有射击`,
    "需使用20CM全环靶",
  ];
};

// 难度页的节点位置已经在页面内统一计算，
// 这里保留最核心的 id / label 即可，不再维护无效的 left / top / style 字段。
const createDifficultyNode = (level) => {
  return {
    id: createDifficultyId(level),
    label: createDifficultyLabel(level),
  };
};

const createDifficultyDetail = (level) => {
  const id = createDifficultyId(level);
  const label = createDifficultyLabel(level);

  return {
    id,
    label,
    title: `${label}难度`,
    summary: createDifficultySummary(level),
    startText: "开始",
    targetPaperType: "20CM全环靶",
  };
};

// 所有训练模式当前共用同一套难度定义。
const sharedDifficultyNodes = Array.from(
  { length: totalDifficultyLevel },
  (_, index) => createDifficultyNode(index + 1)
);

const sharedDifficultyDetails = Object.fromEntries(
  Array.from({ length: totalDifficultyLevel }, (_, index) => {
    const detail = createDifficultyDetail(index + 1);
    return [detail.id, detail];
  })
);

const createModeConfig = ({ key, title, reward = null }) => {
  return {
    key,
    title,
    nodes: sharedDifficultyNodes,
    details: sharedDifficultyDetails,
    activeDifficultyId: mockedUnlockedDifficultyId,
    progressMap: mockedDifficultyProgressMap,
    reward,
  };
};

// 难度页数据源入口：
// 页面通过 getTrainingDifficultyModeConfig(modeKey) 获取当前模式完整配置。
export const trainingDifficultyModeMap = Object.fromEntries(
  modeList.map((mode) => [mode.key, createModeConfig(mode)])
);

export const getTrainingDifficultyModeConfig = (modeKey) => {
  return trainingDifficultyModeMap[modeKey] || trainingDifficultyModeMap.precision;
};
