import { PostGetGroupMembersReturn } from '@api/chat';
import { Ranking } from '../TravellersScreen';
import {
  filterAgeType,
  filterCountryType,
  filterRankingType
} from '../TravellersScreen/utils/types';
import { User } from '@api/regions';

export const applyModalSort = (
  allRanking: {
    uid: number;
    name: string;
    avatar: string | null;
    admin: 0 | 1;
    added: string;
    ranking_data: User;
  }[],
  age: filterAgeType,
  ranking: filterRankingType,
  country: filterCountryType
) => {
  let filteredLocalData = allRanking;
  let indexCounter = 0;

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

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

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

  filteredLocalData = filteredLocalData.map((item) => {
    return item.ranking_data?.dod !== 1
      ? { ...item, displayIndex: indexCounter++ }
      : { ...item, displayIndex: -1 };
  });

  return filteredLocalData;
};
