// 分析服务
import { generateFutureData, futuresList } from '../utils/mockData';

// 获取AI分析
export const fetchAIAnalysis = async (symbol: string) => {
  // 模拟API请求延迟
  await new Promise(resolve => setTimeout(resolve, 500));
  const future = futuresList.find(item => item.code === symbol);
  if (!future) {
    throw new Error('品种不存在');
  }
  const data = generateFutureData(symbol, future.name);
  
  return {
    symbol,
    technical: {
      trend: data.overallView,
      indicators: data.indicators,
      strength: data.adx > 40 ? '强' : data.adx > 20 ? '弱' : '无'
    },
    fundamental: {
      industry: future.type,
      supplyDemand: ['平衡', '供过于求', '供不应求'][Math.floor(Math.random() * 3)],
      policyImpact: ['中性', '利好', '利空'][Math.floor(Math.random() * 3)]
    },
    sentiment: {
      marketSentiment: ['中性', '乐观', '悲观'][Math.floor(Math.random() * 3)],
      positioning: ['多头', '空头', '平衡'][Math.floor(Math.random() * 3)],
      volumeAnalysis: '正常'
    },
    prediction: {
      trend: ['上涨', '下跌', '震荡'][Math.floor(Math.random() * 3)],
      winRate: data.winRate,
      expectedReturn: +(Math.random() * 10 - 2).toFixed(2),
      timeHorizon: '1-3天'
    }
  };
};

// 获取多维度分析
export const fetchMultiDimensionAnalysis = async (symbol: string) => {
  // 模拟API请求延迟
  await new Promise(resolve => setTimeout(resolve, 400));
  
  return {
    technical: {
      trend: ['多头', '空头', '震荡'][Math.floor(Math.random() * 3)],
      support: +(Math.random() * 500 + 1500).toFixed(2),
      resistance: +(Math.random() * 500 + 2500).toFixed(2),
      indicators: {
        macd: ['金叉', '死叉', '走平'][Math.floor(Math.random() * 3)],
        rsi: Math.floor(Math.random() * 40) + 30,
        kdj: ['金叉', '死叉', '走平'][Math.floor(Math.random() * 3)]
      }
    },
    capital: {
      fundFlow: ['流入', '流出', '平衡'][Math.floor(Math.random() * 3)],
      openInterest: Math.floor(Math.random() * 100000) + 50000,
      volume: Math.floor(Math.random() * 1000000) + 100000,
      largePositions: Math.floor(Math.random() * 50) + 20
    },
    policy: {
      industryPolicy: ['中性', '利好', '利空'][Math.floor(Math.random() * 3)],
      macroEconomy: ['稳定', '向好', '向坏'][Math.floor(Math.random() * 3)],
      internationalFactors: ['稳定', '有利', '不利'][Math.floor(Math.random() * 3)]
    }
  };
};

// 获取趋势预测
export const fetchTrendPrediction = async (symbol: string) => {
  // 模拟API请求延迟
  await new Promise(resolve => setTimeout(resolve, 300));
  
  return {
    short_term: {
      trend: ['上涨', '下跌', '震荡'][Math.floor(Math.random() * 3)],
      confidence: Math.floor(Math.random() * 30) + 60,
      timeHorizon: '1-3天'
    },
    medium_term: {
      trend: ['上涨', '下跌', '震荡'][Math.floor(Math.random() * 3)],
      confidence: Math.floor(Math.random() * 25) + 55,
      timeHorizon: '1-2周'
    },
    long_term: {
      trend: ['上涨', '下跌', '震荡'][Math.floor(Math.random() * 3)],
      confidence: Math.floor(Math.random() * 20) + 50,
      timeHorizon: '1-3月'
    }
  };
};

// 获取胜率评估
export const fetchWinRateAssessment = async (symbol: string) => {
  // 模拟API请求延迟
  await new Promise(resolve => setTimeout(resolve, 300));
  const winRate = Math.floor(Math.random() * 30) + 40;
  
  return {
    win_rate: winRate,
    risk_reward: +(Math.random() * 2 + 0.5).toFixed(2),
    backtest_results: {
      period: '过去3个月',
      total_trades: Math.floor(Math.random() * 50) + 50,
      win_trades: Math.floor(winRate / 100 * 100),
      loss_trades: Math.floor((100 - winRate) / 100 * 100),
      profit_factor: +(Math.random() * 2 + 0.5).toFixed(2)
    }
  };
};

// 获取技术指标
export const fetchTechnicalIndicators = async (symbol: string) => {
  // 模拟API请求延迟
  await new Promise(resolve => setTimeout(resolve, 200));
  
  return {
    macd: {
      value: +(Math.random() * 2 - 1).toFixed(2),
      signal: +(Math.random() * 2 - 1).toFixed(2),
      histogram: +(Math.random() * 1 - 0.5).toFixed(2),
      status: ['金叉', '死叉', '走平'][Math.floor(Math.random() * 3)]
    },
    rsi: {
      value: Math.floor(Math.random() * 40) + 30,
      status: ['超买', '超卖', '正常'][Math.floor(Math.random() * 3)]
    },
    kdj: {
      k: +(Math.random() * 100).toFixed(2),
      d: +(Math.random() * 100).toFixed(2),
      j: +(Math.random() * 100).toFixed(2),
      status: ['金叉', '死叉', '走平'][Math.floor(Math.random() * 3)]
    },
    bollinger: {
      upper: +(Math.random() * 500 + 2500).toFixed(2),
      middle: +(Math.random() * 500 + 2000).toFixed(2),
      lower: +(Math.random() * 500 + 1500).toFixed(2),
      status: ['突破上轨', '突破下轨', '通道内'][Math.floor(Math.random() * 3)]
    }
  };
};