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

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 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;
    flag1: string;
    flag2: string | null;
    rip: boolean;
    recent_score_increase: boolean;
  }[];
}

export interface PostGetSeriesRanking extends ResponseType {
  data: RankingData;
}

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
    })
};
