import { Ranking } from '..';
import { filterAgeType, filterCountryType, filterRankingType } from './types';

export const applyModalSort = (
  allRanking: Ranking[],
  age: filterAgeType,
  ranking: filterRankingType,
  country: filterCountryType
) => {
  let filteredLocalData = allRanking;

  if (age) {
    filteredLocalData = filteredLocalData.filter(
      (user) => user.age >= age.min && user.age <= age.max
    );
  }

  if (country) {
    filteredLocalData = filteredLocalData.filter(
      (user) => user.country === country.two.toLowerCase()
    );
  }

  if (ranking) {
    switch (ranking?.label) {
      case 'NM':
        filteredLocalData = filteredLocalData.sort(
          (a: Ranking, b: Ranking) => b.score_nm - a.score_nm
        );
        break;
      case 'DARE':
        filteredLocalData = filteredLocalData.sort(
          (a: Ranking, b: Ranking) => b.score_dare - a.score_dare
        );
        break;
      case 'UN':
        filteredLocalData = filteredLocalData.sort(
          (a: Ranking, b: Ranking) => b.score_un - a.score_un
        );
        break;
      case 'UN+':
        filteredLocalData = filteredLocalData.sort(
          (a: Ranking, b: Ranking) => b.score_unp - a.score_unp
        );
        break;
      case 'TCC':
        filteredLocalData = filteredLocalData.sort(
          (a: Ranking, b: Ranking) => b.score_tcc - a.score_tcc
        );
        break;
      case 'DEEP':
        //TODO: Crash
        filteredLocalData = filteredLocalData.sort(
          (a: Ranking, b: Ranking) => b.score_deep - a.score_deep
        );
        break;
      case 'YES':
        const YESFilteredUsers = filteredLocalData.filter((user) => user.score_yes !== 10000);
        filteredLocalData = YESFilteredUsers.sort(
          (a: Ranking, b: Ranking) => a.score_yes - b.score_yes
        );
        break;
      case 'SLOW':
        const SLOWFilteredUsers = allRanking.filter(
          (user) => user.score_slow < 4500 && user.score_slow > 0
        );
        filteredLocalData = SLOWFilteredUsers.sort(
          (a: Ranking, b: Ranking) => b.score_slow - a.score_slow
        );
        break;
      case 'WHS':
        filteredLocalData = filteredLocalData.sort(
          (a: Ranking, b: Ranking) => b.score_whs - a.score_whs
        );
        break;
      case 'KYE':
        filteredLocalData = filteredLocalData.sort(
          (a: Ranking, b: Ranking) => b.score_kye - a.score_kye
        );
        break;
      case 'TBT':
        filteredLocalData = filteredLocalData.sort(
          (a: Ranking, b: Ranking) => b.score_tbt - a.score_tbt
        );
        break;
    }
  }

  return filteredLocalData;
};
