/**
 * Phone Generator - 手机号生成器
 */

const { randomInt, padZero } = require('../../utils');

class PhoneGenerator {
  constructor() {
    this.countryFormats = {
      CN: {
        name: '中国',
        code: '+86',
        format: '1##########',
        prefixes: ['13', '14', '15', '16', '17', '18', '19']
      },
      US: {
        name: '美国',
        code: '+1',
        format: '(###) ###-####',
        prefixes: ['201', '202', '203', '212', '213', '214', '215']
      },
      UK: {
        name: '英国',
        code: '+44',
        format: '7### ######',
        prefixes: ['7400', '7500', '7600', '7700', '7800', '7900']
      }
    };
  }

  /**
   * 生成手机号
   * @param {Object} options - 选项
   * @returns {string}
   */
  generate(options = {}) {
    const country = options.country || 'CN';
    const withCountryCode = options.withCountryCode !== false;

    const config = this.countryFormats[country];
    if (!config) {
      throw new Error(`Unsupported country: ${country}`);
    }

    let phone = this.generateByCountry(country);

    if (withCountryCode) {
      phone = `${config.code} ${phone}`;
    }

    return phone;
  }

  /**
   * 根据国家生成手机号
   * @param {string} country - 国家代码
   * @returns {string}
   */
  generateByCountry(country) {
    const config = this.countryFormats[country];

    switch (country) {
      case 'CN':
        return this.generateChinaPhone();
      case 'US':
        return this.generateUSPhone();
      case 'UK':
        return this.generateUKPhone();
      default:
        return this.generateChinaPhone();
    }
  }

  /**
   * 生成中国手机号
   * @returns {string}
   */
  generateChinaPhone() {
    const config = this.countryFormats.CN;
    const prefix = config.prefixes[randomInt(0, config.prefixes.length - 1)];
    const suffix = this.generateDigits(9);
    return `${prefix}${suffix}`;
  }

  /**
   * 生成美国手机号
   * @returns {string}
   */
  generateUSPhone() {
    const config = this.countryFormats.US;
    const areaCode = config.prefixes[randomInt(0, config.prefixes.length - 1)];
    const exchange = this.generateDigits(3);
    const number = this.generateDigits(4);
    return `(${areaCode}) ${exchange}-${number}`;
  }

  /**
   * 生成英国手机号
   * @returns {string}
   */
  generateUKPhone() {
    const config = this.countryFormats.UK;
    const prefix = config.prefixes[randomInt(0, config.prefixes.length - 1)];
    const suffix = this.generateDigits(6);
    return `${prefix} ${suffix}`;
  }

  /**
   * 生成指定长度的数字
   * @param {number} length - 长度
   * @returns {string}
   */
  generateDigits(length) {
    let result = '';
    for (let i = 0; i < length; i++) {
      result += randomInt(0, 9);
    }
    return result;
  }

  /**
   * 获取支持的国家列表
   * @returns {Array}
   */
  getSupportedCountries() {
    return Object.keys(this.countryFormats).map(code => ({
      code,
      name: this.countryFormats[code].name
    }));
  }

  /**
   * 批量生成
   * @param {number} count - 数量
   * @param {Object} options - 选项
   * @returns {Array}
   */
  generateBatch(count, options = {}) {
    const phones = [];
    for (let i = 0; i < count; i++) {
      phones.push(this.generate(options));
    }
    return phones;
  }
}

module.exports = PhoneGenerator;
