// 模拟数据源实现
import { DataSource } from './DataSource';
import { futuresList, generateFuturesOverview, generateFutureData, generateKlineData, riskAlerts } from '../../utils/mockData';
import { logger } from '../../utils/logger';

export class MockDataSource implements DataSource {
  private initialized: boolean = false;

  async initialize(): Promise<boolean> {
    // 模拟初始化
    this.initialized = true;
    logger.log('模拟数据源初始化成功');
    return true;
  }

  async getContractList(): Promise<any[]> {
    if (!this.initialized) {
      throw new Error('模拟数据源未初始化');
    }

    // 返回模拟合约列表
    return futuresList.map(item => ({
      symbol: item.code,
      name: item.name,
      exchange: this.getExchangeBySymbol(item.code),
      product_class: 'futures',
      price_tick: 0.01,
      size: 1,
      margin_rate: 0.05
    }));
  }

  async getContractDetail(symbol: string): Promise<any> {
    if (!this.initialized) {
      throw new Error('模拟数据源未初始化');
    }

    // 返回模拟合约详情
    const future = futuresList.find(item => item.code === symbol);
    if (!future) {
      throw new Error(`合约${symbol}不存在`);
    }

    return {
      symbol: future.code,
      name: future.name,
      exchange: this.getExchangeBySymbol(future.code),
      product_class: 'futures',
      price_tick: 0.01,
      size: 1,
      margin_rate: 0.05,
      delivery_month: '202412',
      trading_hours: '09:00-11:30, 13:30-15:00, 21:00-02:30',
      pre_close: 2000,
      open: 2005,
      high: 2010,
      low: 1995,
      last_price: 2003,
      volume: 10000,
      open_interest: 50000
    };
  }

  async getKlineData(symbol: string, period: string, count: number): Promise<any[]> {
    if (!this.initialized) {
      throw new Error('模拟数据源未初始化');
    }

    // 返回模拟K线数据
    const klineData = generateKlineData(count);
    return klineData.map(item => ({
      datetime: item.time * 1000000000, // 转换为纳秒
      open: item.open,
      high: item.high,
      low: item.low,
      close: item.close,
      volume: item.volume,
      open_interest: Math.floor(Math.random() * 100000) + 50000
    }));
  }

  async getTickData(symbol: string): Promise<any> {
    if (!this.initialized) {
      throw new Error('模拟数据源未初始化');
    }

    // 返回模拟实时行情数据
    const future = futuresList.find(item => item.code === symbol);
    if (!future) {
      throw new Error(`合约${symbol}不存在`);
    }

    const pre_close = 2000;
    const last_price = pre_close + (Math.random() * 20 - 10);
    const price_change = last_price - pre_close;

    return {
      datetime: Date.now() * 1000000, // 转换为纳秒
      last_price: +last_price.toFixed(2),
      pre_close,
      open: pre_close + (Math.random() * 10 - 5),
      high: Math.max(last_price, pre_close + (Math.random() * 15 - 5)),
      low: Math.min(last_price, pre_close + (Math.random() * 15 - 10)),
      volume: Math.floor(Math.random() * 10000) + 5000,
      open_interest: Math.floor(Math.random() * 100000) + 50000,
      price_change: +price_change.toFixed(2),
      bid_price1: last_price - 0.01,
      bid_volume1: Math.floor(Math.random() * 100) + 50,
      ask_price1: last_price + 0.01,
      ask_volume1: Math.floor(Math.random() * 100) + 50
    };
  }

  async getMarketOverview(): Promise<any[]> {
    if (!this.initialized) {
      throw new Error('模拟数据源未初始化');
    }

    // 返回模拟市场概览
    const overview = generateFuturesOverview();
    return overview.map(item => ({
      symbol: item.code,
      name: item.name,
      price: item.currentPrice,
      change: item.changePercent,
      change_percent: item.changePercent,
      volume: Math.floor(Math.random() * 100000) + 50000,
      open_interest: Math.floor(Math.random() * 1000000) + 500000
    }));
  }

  async getHistoricalTrades(symbol: string, start: number, end: number): Promise<any[]> {
    if (!this.initialized) {
      throw new Error('模拟数据源未初始化');
    }

    // 返回模拟历史成交数据
    const trades = [];
    const count = 100; // 生成100条模拟数据
    const basePrice = 2000;
    
    for (let i = 0; i < count; i++) {
      const price = basePrice + (Math.random() * 10 - 5);
      const volume = Math.floor(Math.random() * 100) + 10;
      const timestamp = start + Math.floor((end - start) * (i / count));
      
      trades.push({
        datetime: timestamp * 1000000, // 转换为纳秒
        price: +price.toFixed(2),
        volume,
        direction: Math.random() > 0.5 ? 'BUY' : 'SELL'
      });
    }
    
    return trades;
  }

  async close(): Promise<void> {
    // 模拟关闭
    this.initialized = false;
    logger.log('模拟数据源已关闭');
  }

  // 根据合约代码获取交易所
  private getExchangeBySymbol(symbol: string): string {
    // 简单的交易所映射
    const exchangeMap: Record<string, string> = {
      'AU': 'SHFE',
      'AG': 'SHFE',
      'CU': 'SHFE',
      'NI': 'SHFE',
      'SN': 'SHFE',
      'AL': 'SHFE',
      'ZN': 'SHFE',
      'FG': 'CZCE',
      'SJS': 'CZCE',
      'SCA': 'CZCE',
      'JM': 'DCE',
      'RB': 'SHFE',
      'ALO': 'SHFE',
      'MA': 'DCE',
      'PVC': 'DCE',
      'FU': 'SHFE',
      'SC': 'INE',
      'L': 'DCE',
      'NR': 'SHFE',
      'BU': 'SHFE',
      'LU': 'INE',
      'P': 'DCE',
      'LC': 'SHFE',
      'SI': 'SHFE',
      'PGS': 'SHFE',
      'IC': 'CFFEX',
      'IM': 'CFFEX',
      'IH': 'CFFEX'
    };
    
    return exchangeMap[symbol] || 'SHFE';
  }
}