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

export interface PostGetProfileData extends ResponseType {
  user_id: number;
  email: string;
  first_name: string;
  last_name: string;
  username: string;
  avatar: string;
  date_of_birth: string;
  homebase: number;
  homebase2: number | null;
  bio: string;
  links: {
    f?: SocialData;
    t?: SocialData;
    i?: SocialData;
    y?: SocialData;
    www?: SocialData;
    other?: SocialData;
  };
}

export interface PostSetProfileData {
  token: string;
  user?: {
    email?: string;
    first_name?: string;
    last_name?: string;
    date_of_birth?: string;
    username?: string;
    homebase?: number;
    homebase2?: number;
    bio?: string;
    f?: string;
    t?: string;
    i?: string;
    y?: string;
    www?: string;
    other?: string;
  };
  photo?: {
    type: string;
    uri: string;
    name: string;
  };
}

export interface PostSetProfileDataReturn extends ResponseType {
  updated: string[];
  not_updated: string[];
}

export type SocialData = {
  link: string;
  active: 0 | 1;
};

export interface PostGetProfileInfoReturn extends ResponseType {
  username: string;
  email: string;
  first_name: string;
  last_name: string;
  date_of_birth: string;
  avatar_full_size: string;
  avatar: string;
  bio: string;
  links: {
    f?: SocialData;
    t?: SocialData;
    i?: SocialData;
    y?: SocialData;
    www?: SocialData;
    other?: SocialData;
  };
  homebase: number;
  homebase_name: string;
  homebase_flag: string;
  homebase2: number;
  homebase2_name: string;
  homebase2_flag: string;
  series: Series[];
  scores: Score[];
}

export type Series = {
  id: number;
  score: number;
  name: string;
  icon_png: string;
};

export type Score = {
  name: string;
  score: number;
};

export interface PostGetProfileRegionsReturn extends ResponseType {
  data: NM | MQP | UN_UNP_TCC | YES | SLOW | WHS;
}

export type NM = {
  all_count: number;
  firsts: Record<string, number | null>;
  last: Record<string, number | null>;
  megaregions: {
    id: number;
    name: string;
    regions: {
      id: number;
      name: string;
      flag1: string;
      flag2: string | null;
    }[];
    transits: number[];
    visits: number[];
  }[];
  transits_count: number;
  visits_count: number;
};

export type MQP = {
  all_count: number;
  megaregions: {
    id: number;
    name: string;
    regions: {
      id: number;
      name: string;
      flag1: string;
      flag2: string | null;
    }[];
    transits: number[];
    visits: number[];
  }[];
  transits_count: number;
  visits_count: number;
};

export type UN_UNP_TCC = [
  {
    name: string;
    name_en: string;
    flag1: string;
    flag2: string | null;
  }[],
  string[],
  number,
  number
];

export type YES = [
  {
    id: number;
    name: string;
    name_en: string;
    flag1: string;
    flag2: string | null;
  }[],
  Record<
    string,
    {
      year: number;
      score: number;
      country: string;
    }
  >
];

export type SLOW = [
  {
    country_id: number;
    country: string;
    flag: string;
    mega: number[];
    visited: 0 | 1;
    slow11: 0 | 1;
    slow31: 0 | 1;
    slow101: 0 | 1;
    yes: number;
  }[],
  Record<string, string>
];

export type WHS = {
  name: string;
  flag: string;
  visited: boolean;
}[];

export const userApi = {
  getProfileData: (token: string) =>
    request.postForm<PostGetProfileData>(API.GET_USER_SETTINGS_DATA, { token }),
  setProfileData: (data: PostSetProfileData) => {
    const formData = new FormData();

    formData.append('token', JSON.stringify(data.token));
    formData.append('user', JSON.stringify(data.user));
    formData.append('photo', {
      ...data.photo
    } as unknown as Blob);

    return request.postForm<PostSetProfileDataReturn>(API.SET_USER_SETTINGS_DATA, formData);
  },
  getProfileInfo: (token: string) =>
    request.postForm<PostGetProfileInfoReturn>(API.PROFILE_INFO, { token }),
  getProfileInfoPublic: (uid: number) =>
    request.postForm<Exclude<PostGetProfileInfoReturn, { email: null }>>(API.PROFILE_INFO_PUBLIC, {
      uid
    }),
  getProfileRegions: (uid: number, type: string) =>
    request.postForm<PostGetProfileRegionsReturn>(API.GET_PROFILE_REGIONS, { uid, type })
};
