import React from 'react';
import { keygen, getValue, getPercentage } from 'sports-util';

import {
  soccerOverall,
  soccerHome,
  soccerAway,
  soccerFirstHalf,
  soccerSecondHalf,
  amOverall,
  amAway,
  amHome,
  amFirstHalf,
  amSecondHalf,
} from '../constants/standingTable';

export function Column(key, label, className, template, type) {
  return {
    key,
    label,
    className,
    template,
    type,
    getLabel() {
      return this.label ? this.label : this.key;
    },
  };
}

export function goalScore(index, data, type, className) {
  return (
    <td className={`${className}`} key={keygen()}>
      {getValue(data, 'data.goalsfor')}:{getValue(data, 'data.goalsagainst')}
    </td>
  );
}

export function goalDifference(index, data, type, className) {
  return difference(
    getValue(data, 'data.goalsfor'),
    getValue(data, 'data.goalsagainst'),
    index,
    className
  );
}

export function pointsDifference(index, data) {
  return difference(getValue(data, 'data.pointsfor'), getValue(data, 'data.pointsagainst'), index);
}

export function teamName(index, data) {
  return (
    <td key={keygen()} className={`team-name`}>
      {/* <TeamLogo enet_pulse_id={getValue(data, "participant.id")} /> */}
      {getValue(data, 'participant.name')}
    </td>
  );
}

export function winsPercentage(index, data, type, className) {
  let result = 0;
  switch (type) {
    case 'overall':
      result = getPercentage(getValue(data, 'data.wins'), getValue(data, 'data.played'));
      break;
    case 'homeWin':
      result = getPercentage(getValue(data, 'data.winshome'), getValue(data, 'data.playedhome'));
      break;
    case 'awayWin':
      result = getPercentage(getValue(data, 'data.winsaway'), getValue(data, 'data.playedaway'));
      break;
    case '1st Half':
      result = getPercentage(getValue(data, 'data.wins1h'), getValue(data, 'data.played'));
      break;
    case '2nd Half':
      result = getPercentage(getValue(data, 'data.wins2h'), getValue(data, 'data.played'));
      break;
    default:
      result = getPercentage(getValue(data, 'data.wins'), getValue(data, 'data.playedhome'));
      break;
  }
  return (
    <td className={`${className}`} key={keygen()}>
      {result.toFixed(2)}
    </td>
  );
}

export function sortByTabs(data, type, sportName) {
  if (sportName === 'am. football' || sportName === 'basketball') {
    data.sort(function (a, b) {
      const firstTeam = a?.statistics;
      const secondTeam = b?.statistics;
      switch (type) {
        case 'overall':
          return getPercentage(firstTeam?.wins, firstTeam?.played) >
            getPercentage(secondTeam?.wins, secondTeam?.played)
            ? -1
            : 1;
        case 'home':
          return getPercentage(firstTeam?.winshome, firstTeam?.playedhome) >
            getPercentage(secondTeam?.winshome, secondTeam?.playedhome)
            ? -1
            : 1;
        case 'away':
          return getPercentage(firstTeam?.winsaway, firstTeam?.playedaway) >
            getPercentage(secondTeam?.winsaway, secondTeam?.playedaway)
            ? -1
            : 1;
        case '1st Half':
          return getPercentage(firstTeam?.wins1h, firstTeam?.played1h) >
            getPercentage(secondTeam?.wins1h, secondTeam?.played1h)
            ? -1
            : 1;
        case '2nd Half':
          return getPercentage(firstTeam?.wins2h, firstTeam?.played2h) >
            getPercentage(secondTeam?.wins2h, secondTeam?.played2h)
            ? -1
            : 1;

        default:
          return null;
      }
    });
  } else {
    data.sort(function (a, b) {
      switch (type) {
        case 'overall':
          return a?.statistics?.rank - b?.statistics?.rank;
        case 'home':
          return a?.statistics?.pointshome > b?.statistics?.pointshome ? -1 : 1;
        case 'away':
          return a?.statistics?.pointsaway > b?.statistics?.pointsaway ? -1 : 1;
        case '1st Half':
          return a?.statistics?.points1h > b?.statistics?.points1h ? -1 : 1;
        case '2nd Half':
          return a?.statistics?.points2h > b?.statistics?.points2h ? -1 : 1;

        default:
          return null;
      }
    });
  }

  return data;
}

export function difference(first, second, index, className) {
  const result = first - second;
  const getClass = () => {
    if (result > 0) {
      return 'color-green';
    }
    if (result === 0) {
      return '';
    }
    return 'color-red';
  };
  return (
    <td key={keygen()} className={`${getClass()} ${className}`}>
      {result > 0 ? '+' : ''}
      {result}
    </td>
  );
}

export function buildRows(data, columns, type) {
  const rows = [];
  data.forEach((d, index) => {
    rows.push(
      columns &&
        columns.map((c) => {
          if (c.template) {
            return c.template(index, d, c.type, c.className);
          }
          if (c.modifier) {
            // eslint-disable-next-line react/no-array-index-key
            return (
              <td className={c.className} key={`${keygen()}`}>
                {c.modifier(d)}
              </td>
            );
          }

          const getValueRanking = getValue(d, c.key, index + 1);

          return (
            // eslint-disable-next-line react/no-array-index-key
            <td className={c.className} key={`${keygen()}`}>
              {getValueRanking}
            </td>
          );
        })
    );
  });
  return rows;
}

export function getSoccerColumns(tab) {
  switch (tab) {
    case 'overall':
      return soccerOverall;
    case 'home':
      return soccerHome;
    case 'away':
      return soccerAway;
    case '1st Half':
      return soccerFirstHalf;
    case '2nd Half':
      return soccerSecondHalf;
    default:
      return null;
  }
}

export function getFootballAndBasketballColumns(tab, sportName) {
  switch (tab) {
    case 'overall':
      sportName === 'am. football' && amOverall.splice(4, 0, new Column('data.draws', 'draws'));
      return amOverall;
    case 'home':
      sportName === 'am. football' && amHome.splice(4, 0, new Column('data.drawshome', 'draws'));
      return amHome;
    case 'away':
      sportName === 'am. football' && amAway.splice(4, 0, new Column('data.drawsaway', 'draws'));
      return amAway;
    case '1st Half':
      sportName === 'am. football' && amFirstHalf.splice(4, 0, new Column('data.draws1h', 'draws'));
      return amFirstHalf;
    case '2nd Half':
      sportName === 'am. football' &&
        amSecondHalf.splice(4, 0, new Column('data.draws2h', 'draws'));
      return amSecondHalf;
    default:
      return null;
  }
}

export function getSportColumns(sportName, tab) {
  switch (sportName) {
    case 'am. football':
    case 'basketball':
      return getFootballAndBasketballColumns(tab, sportName);
    default:
      return getSoccerColumns(tab);
  }
}
