import { request } from '../../../utils';
import { API } from '../../../types';
import { ResponseType } from '../response-type';
import { PostGetUsersWhoVisitedDataReturn } from '@api/regions';

export interface PostGetSeries extends ResponseType {
  series: { id: number; name: string; icon: string }[];
  items: {
    id: number;
    series_id: number;
    name: string;
    region: number;
    pointJSON: any;
    polygonJSON: string;
    visited?: 0 | 1;
  }[];
}

export interface PostGetSeriesGroups extends ResponseType {
  data: {
    id: number;
    name: string;
  }[];
}

export interface PostGetSeriesList extends ResponseType {
  data: {
    id: number;
    name: string;
    icon: string;
    count: number;
    new: number;
    score: number;
    icon_png: string;
    count2: number;
  }[];
}

export interface PostGetSeriesListForFilters extends ResponseType {
  data: {
    id: number;
    name: string;
    icon: string;
    count: number;
    new: number;
    new_icon: string;
    label: boolean;
  }[];
}

export interface PostGetItems extends ResponseType {
  groups: {
    name: string;
    icon: string;
    series_id: number;
    series_name: string;
    items: {
      item_id: number;
      icon: string | null;
      name: string;
      readonly: boolean;
      info: string;
      new: boolean;
      checked: boolean;
      checked_double: boolean;
      series_id: number;
      double: boolean;
    }[];
  }[];
}

export interface PostSetToggleItemReturn extends ResponseType {
  modified_items: number[];
}

export interface PostSetToggleItem {
  token: string;
  series_id: string;
  item_id: string;
  checked: 0 | 1;
  double: 0 | 1;
}

export interface GroupsData {
  name: string;
  id: number;
  series:
    | {
        id: number;
        name: string;
      }[]
    | null;
}

export interface PostGetSeriesGroupsRanking extends ResponseType {
  data: GroupsData[];
}

export interface RankingData {
  max_points: number;
  max_pages: number;
  ranking: {
    user_id: number;
    score: number;
    avatar: string | null;
    first_name: string;
    last_name: string;
    age: number;
    authenticated: 0 | 1;
    badge_nm: 0 | 1;
    badge_un: 0 | 1;
    badge_un_25: 0 | 1;
    badge_un_50: 0 | 1;
    badge_un_75: 0 | 1;
    badge_un_100: 0 | 1;
    badge_un_150: 0 | 1;
    badge_un_193: 0 | 1;
    badge_premium: 0 | 1;
    flag1: string;
    flag2: string | null;
    rip: boolean;
    recent_score_increase: boolean;
  }[];
}

export interface PostGetSeriesRanking extends ResponseType {
  data: RankingData;
}

export interface PostGetDataFromPoint extends ResponseType {
  nm: { id: number };
  dare: { id: number };
  country: string;
}

export interface PostGetSuggestionData extends ResponseType {
  nm: {
    id: number;
    region_name: string;
  }[];
  dare: {
    id: number;
    name: string;
  }[];
  series: {
    id: number;
    name: string;
  }[];
  grouped: {
    [key: string]: { id: number; name: string; group_name: string | null }[];
  };
}

export interface SubmitSuggestionReturn extends ResponseType {}

export type SubmitSuggestionTypes = {
  token: string;
  comment: string;
  nm: number;
  dare: number;
  series: number;
  name: string;
  link: string;
  lat: number;
  lng: number;
  item: number;
};

export interface PostGetSeriesIcons extends ResponseType {
  data: {
    id: number;
    new_icon_png: string;
    new_icon_visited_png: string;
  }[];
}

export const seriesApi = {
  getSeries: (token: string | null, regions: string) =>
    request.postForm<PostGetSeries>(API.SERIES, { token, regions }),
  getSeriesGroups: () => request.postForm<PostGetSeriesGroups>(API.SERIES_GROUPS),
  getSeriesWithGroup: (token: string, group: string) =>
    request.postForm<PostGetSeriesList>(API.SERIES_WITH_GROUP, { token, group }),
  getItemsForSeries: (token: string, series_id: string) =>
    request.postForm<PostGetItems>(API.GET_ITEMS_FOR_SERIES, { token, series_id }),
  setToggleItem: (
    token: string,
    series_id: number,
    item_id: number,
    checked: 0 | 1,
    double: 0 | 1
  ) =>
    request.postForm<PostSetToggleItemReturn>(API.TOGGLE_ITEM_SERIES, {
      token,
      series_id,
      item_id,
      checked,
      double
    }),
  getSeriesGroupsRanking: () =>
    request.postForm<PostGetSeriesGroupsRanking>(API.GET_SERIES_GROUPS_RANKING),
  getSeriesRanking: (id: number, page: number, page_size: number) =>
    request.postForm<PostGetSeriesRanking>(API.GET_SERIES_RANKING, {
      id,
      page,
      page_size
    }),
  getDataFromPoint: (token: string, lat: number, lng: number) =>
    request.postForm<PostGetDataFromPoint>(API.GET_DATA_FROM_POINT, { token, lat, lng }),
  getSuggestionData: () => request.postForm<PostGetSuggestionData>(API.GET_SUGGESTION_DATA),
  submitSuggestion: (data: SubmitSuggestionTypes) =>
    request.postForm<SubmitSuggestionReturn>(API.SUBMIT_SUGGESTION, data),
  getList: () => request.postForm<PostGetSeriesListForFilters>(API.GET_SERIES_LIST),
  getIcons: () => request.postForm<PostGetSeriesIcons>(API.GET_ICONS),
  getUsersWhoTickedSeries: (
    token: string,
    id: number,
    page: number,
    sort?: string,
    age?: number,
    country?: string
  ) =>
    request.postForm<PostGetUsersWhoVisitedDataReturn>(API.GET_USERS_WHO_TICKED_SERIES, {
      token,
      id,
      page,
      sort,
      age,
      country
    })
};
