import Logs from '../libs/logs.js';

/**
 * 精确浮点数字
 * @param {number} number
 * @param {number} x
 * @returns {number}
 */
const fixFloat = (number, x=2) => {
  return parseFloat(number.toFixed(x));
}

/**
 * 计算盈利
 */
const triangleProfitCalc = (betInfo) => {
  const {
    cross_type,
    inner_index,
    inner_rebate,
    gold_side_a: x,
    gold_side_b: y,
    gold_side_c: z,
    odds_side_a: a,
    odds_side_b: b,
    odds_side_c: c,
    rebate_side_a: A = 0,
    rebate_side_b: B = 0,
    rebate_side_c: C = 0,
    rebate_type_side_a: TA = 0,
    rebate_type_side_b: TB = 0,
    rebate_type_side_c: TC = 0,
  } = betInfo;

  /**
   * cross_type:
   *  la: 全输
   *  wa: 全赢
   *  lh: 半输
   *  wh: 半赢
   *  dr: 和局
   *  la_wh_wa, la_dr_wa, la_lh_wa, lh_dr_wa, lh_lh_wa, la_la_wa
   */

  let inner_rebate_value = 0;
  if (inner_index == 0) {
    inner_rebate_value = x * inner_rebate;
  }
  else if (inner_index == 1) {
    inner_rebate_value = y * inner_rebate;
  }
  else if (inner_index == 2) {
    inner_rebate_value = z * inner_rebate;
  }

  const k1 = TA == 1 ? a + A : a * (1 + A);
  const k2 = 1 - A;
  const k3 = TB == 1 ? b + B : b * (1 + B);
  const k4 = 1 - B;
  const k5 = TC == 1 ? c + C : c * (1 + C);
  const k6 = 1 - C;

  let win_side_a = 0, win_side_b = 0, win_side_c = 0;
  win_side_a = k1*x - k4*y - k6*z;
  win_side_b = k3*y - k2*x - k6*z;

  switch (cross_type) {
    case 'la_wh_wa': // 全输 半赢 全赢
      win_side_c = k5*z - k2*x + k3*y/2;
      break;
    case 'la_dr_wa': // 全输 和局 全赢
      win_side_c = k5*z - k2*x;
      break;
    case 'la_lh_wa': // 全输 半输 全赢
      win_side_c = k5*z - k2*x - k4*y/2;
      break;
    case 'lh_dr_wa': // 半输 和局 全赢
      win_side_c = k5*z - k2*x/2;
      break;
    case 'lh_lh_wa': // 半输 半输 全赢
      win_side_c = k5*z - k2*x/2 - k4*y/2;
      break;
    case 'la_la_wa': // 全输 全输 全赢
      win_side_c = k5*z - k2*x - k4*y;
      break;
    case 'la_wa_rv': // 两盘口输赢
      win_side_c = 0;
      break;
    default:
      win_side_c = 0;
  }

  win_side_a = fixFloat(win_side_a + inner_rebate_value);
  win_side_b = fixFloat(win_side_b + inner_rebate_value);
  win_side_c = fixFloat(win_side_c + inner_rebate_value);
  let win_average = 0;
  if (cross_type == 'la_wa_rv') {
    win_side_c = 0;
    win_average = fixFloat((win_side_a + win_side_b) / 2);
  }
  else {
    win_average = fixFloat((win_side_a + win_side_b + win_side_c) / 3);
  }

  return { win_side_a, win_side_b, win_side_c, win_average }
}

const triangleGoldCalc = (betInfo) => {
  const {
    cross_type, inner_index, inner_base,
    odds_side_a: a,
    odds_side_b: b,
    odds_side_c: c,
    rebate_side_a: A = 0,
    rebate_side_b: B = 0,
    rebate_side_c: C = 0,
    rebate_type_side_a: TA = 0,
    rebate_type_side_b: TB = 0,
    rebate_type_side_c: TC = 0,
  } = betInfo;
  if (typeof a !== 'number' || typeof b !== 'number' || typeof c !== 'number') {
    return;
  }
  const k1 = TA == 1 ? a + A : a * (1 + A);
  const k2 = 1 - A;
  const k3 = TB == 1 ? b + B : b * (1 + B);
  const k4 = 1 - B;
  const k5 = TC == 1 ? c + C : c * (1 + C);
  const k6 = 1 - C;
  let x = inner_base;
  let y = (k1 + k2) * x / (k3 + k4);
  let z;
  switch (cross_type) {
    case 'la_wh_wa': // 全输 半赢 全赢
      z = k3 * y / 2 / (k5 + k6);
      break;
    case 'la_dr_wa': // 全输 和局 全赢
      z = k3 * y / (k5 + k6);
      break;
    case 'la_lh_wa': // 全输 半输 全赢
      z = (k3 + k4 / 2) * y / (k5 + k6);
      break;
    case 'lh_dr_wa': // 半输 和局 全赢
      z = (k3 * y - k2 * x / 2) / (k5 + k6);
      break;
    case 'lh_lh_wa': // 半输 半输 全赢
      z = ((k3 + k4/2) * y - k2 * x / 2) / (k5 + k6);
      break;
    case 'la_la_wa': // 全输 全输 全赢
      z = (k3 + k4) * y / (k5 + k6);
      break;
    case 'la_wa_rv': // 两盘口输赢
      z = 0;
      break;
    default:
      z = 0;
  }

  if (inner_index == 1) {
    const scale = inner_base / y;
    x = x * scale;
    y = inner_base;
    z = z * scale;
  }
  else if (inner_index == 2) {
    const scale = inner_base / z;
    x = x * scale;
    y = y * scale;
    z = inner_base;
  }

  if (x < 0 || y < 0 || z < 0) {
    return;
  }

  return {
    gold_side_a: fixFloat(x),
    gold_side_b: fixFloat(y),
    gold_side_c: fixFloat(z),
  };

}

const eventSolutions = (betInfo, showGolds=false) => {
  const goldsInfo = triangleGoldCalc(betInfo);
  if (!goldsInfo) {
    return;
  }
  const profitInfo = triangleProfitCalc({ ...betInfo, ...goldsInfo }, showGolds);

  const { gold_side_a, gold_side_b, gold_side_c } = goldsInfo;
  const { win_side_a, win_side_b, win_side_c, win_average } = profitInfo;

  const {
    cross_type, inner_index, inner_rebate, inner_base,
    odds_side_a, odds_side_b, odds_side_c,
    rebate_side_a, rebate_side_b, rebate_side_c,
    rebate_type_side_a, rebate_type_side_b, rebate_type_side_c,
  } = betInfo;

  const win_average_rate = fixFloat(win_average / inner_base * 100);
  const win_profit_rate = fixFloat(win_average / (gold_side_a + gold_side_b + gold_side_c) * 100);

  let result = {
    odds_side_a, odds_side_b, odds_side_c,
    rebate_side_a, rebate_side_b, rebate_side_c,
    rebate_type_side_a, rebate_type_side_b, rebate_type_side_c,
    win_average, win_average_rate, win_profit_rate, cross_type,
    inner_index, inner_base, inner_rebate,
  }

  if (showGolds) {
    result = {
      ...result,
      gold_side_a, gold_side_b, gold_side_c,
      win_side_a, win_side_b, win_side_c,
    }
  }
  return result;
}

export { eventSolutions };
export default { eventSolutions };
