// 气象数据模拟工具

/**
 * 生成日期数组
 * @param {number} days - 天数
 * @returns {Array} 日期数组
 */
const generateDates = (days) => {
  const dates = [];
  const today = new Date();

  for (let i = days - 1; i >= 0; i--) {
    const date = new Date(today);
    date.setDate(date.getDate() - i);
    dates.push(`${date.getMonth() + 1}/${date.getDate()}`);
  }

  return dates;
};

/**
 * 生成随机数值数组
 * @param {number} count - 数据点数量
 * @param {number} min - 最小值
 * @param {number} max - 最大值
 * @param {number} base - 基准值
 * @param {number} volatility - 波动率 (0-1)
 * @returns {Array} 数值数组
 */
const generateValues = (count, min, max, base = 0, volatility = 0.1) => {
  const values = [];
  let current = base || min + (max - min) * Math.random();

  for (let i = 0; i < count; i++) {
    // 添加随机波动
    const change = (max - min) * volatility * (Math.random() - 0.5);
    current = Math.max(min, Math.min(max, current + change));

    // 对于特定类型添加趋势
    if (i % 10 === 0 && Math.random() > 0.7) {
      current += (max - min) * 0.05 * (Math.random() - 0.5);
    }

    values.push(parseFloat(current.toFixed(1)));
  }

  return values;
};

/**
 * 生成指定天数的土壤数据
 * @param {number} days - 天数
 * @returns {Object} 土壤数据对象
 */
const generateSoilData = (days) => {
  const dates = generateDates(days);
  const count = dates.length;

  return {
    // 土壤温度数据 (10-30°C)
    temperature: {
      7: {
        dates: generateDates(7),
        values: generateValues(7, 15, 28, 22, 0.1),
      },
      30: {
        dates: generateDates(30),
        values: generateValues(30, 10, 30, 23, 0.12),
      },
      90: {
        dates: generateDates(90),
        values: generateValues(90, 8, 32, 22, 0.15),
      },
    },

    // 土壤湿度数据 (30-90%)
    humidity: {
      7: {
        dates: generateDates(7),
        values: generateValues(7, 40, 85, 65, 0.15),
      },
      30: {
        dates: generateDates(30),
        values: generateValues(30, 30, 90, 60, 0.2),
      },
      90: {
        dates: generateDates(90),
        values: generateValues(90, 25, 95, 62, 0.2),
      },
    },

    // 土壤pH值数据 (4.0-9.0)
    ph: {
      7: {
        dates: generateDates(7),
        values: generateValues(7, 6.0, 7.5, 6.8, 0.05),
      },
      30: {
        dates: generateDates(30),
        values: generateValues(30, 5.5, 8.0, 6.7, 0.07),
      },
      90: {
        dates: generateDates(90),
        values: generateValues(90, 4.0, 9.0, 6.6, 0.1),
      },
    },

    // 土壤电导率(EC)数据 (0.1-5.0 mS/cm)
    ec: {
      7: {
        dates: generateDates(7),
        values: generateValues(7, 0.5, 2.0, 1.2, 0.1),
      },
      30: {
        dates: generateDates(30),
        values: generateValues(30, 0.3, 3.0, 1.3, 0.15),
      },
      90: {
        dates: generateDates(90),
        values: generateValues(90, 0.1, 5.0, 1.2, 0.2),
      },
    },

    // 土壤氮含量数据 (50-500 mg/kg)
    nitrogen: {
      7: {
        dates: generateDates(7),
        values: generateValues(7, 150, 350, 250, 0.1),
      },
      30: {
        dates: generateDates(30),
        values: generateValues(30, 100, 400, 260, 0.15),
      },
      90: {
        dates: generateDates(90),
        values: generateValues(90, 50, 500, 250, 0.2),
      },
    },

    // 土壤磷含量数据 (10-200 mg/kg)
    phosphorus: {
      7: {
        dates: generateDates(7),
        values: generateValues(7, 30, 70, 45, 0.08),
      },
      30: {
        dates: generateDates(30),
        values: generateValues(30, 20, 100, 48, 0.1),
      },
      90: {
        dates: generateDates(90),
        values: generateValues(90, 10, 200, 50, 0.15),
      },
    },

    // 土壤钾含量数据 (50-400 mg/kg)
    potassium: {
      7: {
        dates: generateDates(7),
        values: generateValues(7, 120, 250, 180, 0.07),
      },
      30: {
        dates: generateDates(30),
        values: generateValues(30, 100, 300, 190, 0.1),
      },
      90: {
        dates: generateDates(90),
        values: generateValues(90, 50, 400, 180, 0.15),
      },
    },
  };
};

/**
 * 生成实时土壤数据
 * @returns {Object} 实时土壤数据
 */
const generateRealtimeSoilData = () => {
  return {
    temperature: parseFloat((20 + Math.random() * 8).toFixed(1)),
    humidity: parseFloat((50 + Math.random() * 30).toFixed(1)),
    ph: parseFloat((6.0 + Math.random() * 1.5).toFixed(1)),
    ec: parseFloat((0.5 + Math.random() * 1.5).toFixed(1)),
    nitrogen: parseFloat((150 + Math.random() * 200).toFixed(1)),
    phosphorus: parseFloat((20 + Math.random() * 60).toFixed(1)),
    potassium: parseFloat((100 + Math.random() * 200).toFixed(1)),
    updateTime: new Date().toLocaleTimeString("zh-CN"),
  };
};

// 导出模拟数据
export const soilMockData = generateSoilData();
export const realtimeSoilData = generateRealtimeSoilData();

/**
 * 获取指定土壤类型和时间范围的格式化数据（用于SoilChart组件）
 * @param {string} soilType - 土壤类型
 * @param {number} days - 时间范围（7/30/90天）
 * @returns {Object} 格式化的数据 { dates: [], values: [] }
 */
const getFormattedSoilData = (soilType, days) => {
  // 验证参数
  if (!soilMockData[soilType] || !soilMockData[soilType][days]) {
    console.error(`无效的土壤类型或时间范围: ${soilType}, ${days}`);
    return { dates: [], values: [] };
  }

  return soilMockData[soilType][days];
};

// 导出工具函数供其他组件使用
export {
  generateDates,
  generateValues,
  generateSoilData,
  generateRealtimeSoilData,
  getFormattedSoilData,
};
