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

export interface PostGetEventsListReturn extends ResponseType {
  data: SingleEvent[];
  nm: SingleEvent[];
  community: SingleEvent[];
  shared_trips: SingleEvent[];
  local_meetings: SingleEvent[];
}

export type SingleEvent = {
  id: number;
  active: 0 | 1;
  full: 0 | 1;
  closed: 0 | 1;
  name: string;
  url: string;
  date: string;
  date_from: string;
  date_to: string;
  address1: string;
  location: string;
  capacity?: number;
  participants?: number;
  registrations_info: 1 | 2 | 3 | 4 | 5;
  type: 1 | 2 | 3 | 4;
  photo: 0 | 1;
  available?: number;
  joined: 0 | 1;
  joining: number;
  interested: number;
  visible?: 0 | 1;
  archived: 0 | 1;
  time?: string;
  time_from?: string;
  time_to?: string;
  flag: string | null;
  country: string | null;
  nm_event: 0 | 1;
  star: 0 | 1;
  date_tentative: 0 | 1;
  wp_url?: string | null;
};

export type Participants = {
  uid: number;
  name: string;
  avatar: string | null;
  flag: string | null;
};

export type EventSettings = {
  name: string;
  url: string;
  date: string;
  date_from: string;
  date_to: string;
  address1: string;
  location: string;
  capacity: number;
  type: 1 | 2 | 3 | 4;
  registrations_info: 1 | 2 | 3 | 4 | 5;
  address2: string;
  details: string;
  details_small: string;
  free: 0 | 1;
  host: string;
  host_profile: number | null;
  price: string;
  shop_url: string;
  chat_token: string | null;
  all_participants_visible: 0 | 1;
  participants_can_add_photos: 0 | 1 | null;
  participants_can_add_files: 0 | 1 | null;
  host_data: {
    first_name: string;
    last_name: string;
    avatar: string | null;
    nm: number | null;
    un: number | null;
    flag: string;
  };
  nm_region?: number;
  nm_regions?: string;
};

export type EventAttachments = {
  id: number;
  filename: string;
  filetype: string;
  description: string;
  data: 0 | 1;
  preview: 0 | 1;
  type: 1 | 2 | 3;
};

export type EventPhotos = {
  id: number;
  filetype: string;
  data: 0 | 1;
  preview: 0 | 1;
  uid: number;
  name: string;
  avatar: string | null;
};

export type EventData = SingleEvent & {
  event_chat: string | null;
  settings: EventSettings;
  participants_data: Participants[];
  participants_full_data: User[];
  participants_approved_data: Participants[];
  participants_approved_full_data: User[];
  joined: 0 | 1;
  attachments: EventAttachments[];
  files: EventAttachments[];
  photos: EventPhotos[];
  user_id: number | boolean;
  photo_available: 0 | 1;
  photo_main: 0 | 1;
  photos_left: number;
  flag: string | null;
  country: string | null;
};

export interface PostGetEventReturn extends ResponseType {
  data: EventData;
}

export interface PostGetMorePhotosReturn extends ResponseType {
  photos: EventPhotos[];
  photos_left: number;
}

export interface PostUploadTempFileReturn extends ResponseType {
  filetype: string;
  name: string;
  temp_name: string;
}

export interface PostGetCanAddEventReturn extends ResponseType {
  can: boolean;
}

export interface PostGetPhotosForRegionReturn extends ResponseType {
  photos: number[];
}

export interface PostGetEventForEditingReturn {
  address1: string;
  address2: string;
  capacity: number | null;
  date: string;
  details: string;
  lat: number | null;
  lon: number;
  region: number;
  time_from: string;
  time_to: string;
  title: string;
  photo_id: number | null;
  result: string;
}

export interface PostJoinEvent {
  token: string;
  id: number;
}

export interface PostUploadTemp {
  token: string;
  file: {
    type: string;
    uri: string;
    name: string;
  };
}

export interface PostUploadPhoto {
  token: string;
  event_id: number;
  file: {
    type: string;
    uri: string;
    name: string;
  };
}

export interface PostEventAddFile {
  token: string;
  type: 1 | 2 | 3;
  description: string;
  event_id: number;
  filetype: string;
  filename: string;
  temp_filename: string;
}

export interface PostDeleteFile {
  token: string;
  id: number;
  event_id: number;
}

export interface PostAddEvent {
  token: string;
  event: any;
}

export interface PostAddSharedTrip {
  token: string;
  trip: any;
}

export interface PostUpdateEvent {
  token: string;
  event_id: number;
  event: any;
}

export interface PostUpdateSharedTrip {
  token: string;
  trip_id: number;
  trip: any;
}

export const eventsApi = {
  getEventsList: (token: string, past: number) =>
    request.postForm<PostGetEventsListReturn>(API.GET_EVENTS_LIST, { token, past }),
  getEvent: (token: string, url: string) =>
    request.postForm<PostGetEventReturn>(API.GET_EVENT, { token, url }),
  joinEvent: (data: PostJoinEvent) => request.postForm<ResponseType>(API.JOIN_EVENT, data),
  unjoinEvent: (data: PostJoinEvent) => request.postForm<ResponseType>(API.UNJOIN_EVENT, data),
  uploadTempFile: (data: PostUploadTemp, config?: AxiosRequestConfig) => {
    const formData = new FormData();

    formData.append('token', data.token);
    formData.append('file', {
      ...data.file
    } as unknown as Blob);

    return request.postForm<PostUploadTempFileReturn>(API.UPLOAD_TEMP_FILE, formData, {
      ...config
    });
  },
  eventAddFile: (data: PostEventAddFile) =>
    request.postForm<ResponseType>(API.EVENT_ADD_FILE, data),
  deleteFile: (data: PostDeleteFile) => request.postForm<ResponseType>(API.DELETE_FILE, data),
  uploadPhoto: (data: PostUploadPhoto, config?: AxiosRequestConfig) => {
    const formData = new FormData();

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

    return request.postForm<PostUploadTempFileReturn>(API.UPLOAD_PHOTO_EVENT, formData, {
      ...config
    });
  },
  getMorePhotos: (token: string, event_id: number, last_id: number) =>
    request.postForm<PostGetMorePhotosReturn>(API.GET_MORE_PHOTOS, { token, event_id, last_id }),
  canAddEvent: (token: string) =>
    request.postForm<PostGetCanAddEventReturn>(API.CAN_ADD_EVENT, { token }),
  getPhotosForRegion: (region_id: number) =>
    request.postForm<PostGetPhotosForRegionReturn>(API.GET_PHOTOS_FOR_REGION, { region_id }),
  addEvent: (data: PostAddEvent) => request.postForm<ResponseType>(API.ADD_EVENT, data),
  getEventForEditing: (token: string, event_id: number) =>
    request.postForm<PostGetEventForEditingReturn>(API.GET_EVENT_FOR_EDITING, { token, event_id }),
  updateEvent: (data: PostUpdateEvent) => request.postForm<ResponseType>(API.UPDATE_EVENT, data),
  cancelEvent: (token: string, event_id: number) =>
    request.postForm<ResponseType>(API.CANCEL_EVENT, { token, event_id }),
  removeParticipant: (token: string, event_id: number, user_id: number) =>
    request.postForm<ResponseType>(API.REMOVE_PARTICIPANT, { token, event_id, user_id }),
  addSharedTrip: (data: PostAddSharedTrip) =>
    request.postForm<ResponseType>(API.ADD_SHARED_TRIP, data),
  updateSharedTrip: (data: PostUpdateSharedTrip) =>
    request.postForm<ResponseType>(API.UPDATE_SHARED_TRIP, data),
  sharedTripAddParticipant: (token: string, event_id: number, user_id: number) =>
    request.postForm<ResponseType>(API.SHARED_TRIP_ADD_PARTICIPANT, {
      token,
      event_id,
      user_id
    }),
  setFull: (token: string, id: number, full: 0 | 1) =>
    request.postForm<ResponseType>(API.SET_FULL, { token, id, full })
};
