import {Injectable} from '@angular/core';
import {HttpClient} from "@angular/common/http";
import {Endpoints} from "./endpoints";
import { Observable} from "rxjs";

@Injectable({
  providedIn: 'root'
})
export class EventService {

  constructor(private http: HttpClient) {  }

  findAllEventTypes(): Observable<EventType[]>{
    return this.http.get(Endpoints.GET_EVENT_TYPES()) as Observable<EventType[]>;
  }

  findEvents( ): Observable<Event[]> {
    return this.http.get(Endpoints.GET_EVENTS( )) as Observable<Event[]>;
  }

  findEvent(idEvent: number ): Observable<Event> {
    return this.http.get(Endpoints.GET_EVENT( idEvent  )) as Observable<Event>;
  }

  register(registerEventRequest: RegisterEventRequest ): Observable<Event> {
    return this.http.post(Endpoints.POST_EVENT_REGISTRATIONS_REGISTER( registerEventRequest.idEvent  ),registerEventRequest) as Observable<Event>;
  }

  findRegistrations(): Observable<Registration[]> {
    return this.http.get(Endpoints.GET_EVENT_REGISTRATIONS_FIND()) as Observable<Registration[]>;
  }

  findRegistration(idRegistration: number ): Observable<Registration> {
    return this.http.get(Endpoints.GET_REGISTRATION( idRegistration  )) as Observable<Registration>;
  }

  cancel(idEvent: number ): Observable<Event> {
    return this.http.post(Endpoints.POST_EVENT_CANCEL( idEvent  ),{}) as Observable<Event>;
  }

  cancelRegistration(idRegistration: number ): Observable<Event> {
    return this.http.post(Endpoints.POST_EVENT_REGISTRATION_CANCEL( idRegistration  ),{}) as Observable<Event>;
  }

  findEventsAvailable(): Observable<EventsAvailableResponse>{
    return this.http.get(Endpoints.GET_EVENTS_AVAILABLE()) as Observable<EventsAvailableResponse>;
  }

}

export interface Trainer {
  id: number;
  name: string;
}

export interface Event {
  id: number;
  name: string;
  start: number;
  end: number;
  deleted_at?: number;
  trainer?: Trainer;
  seat_count: number;
  reservationCount: number;
  eventType: EventType;
  room: Room;
  registrations: Registration[];
  hasFreeSeats?: boolean;
}

export interface EquipmentTypeAssignment {
  id: number;
  count: number;
  equipmentType: EquipmentType;

}

export interface EquipmentType {
  id: number;
  name: string;

}

export interface Room {
  id: number;
  name: string;
  seat_count: number;
}
export interface EventType {
  id: number;
  name: string;
  theme: string;
}

export interface DayToDisplay {
  date: number; //seconds
  active: true;
  events: Event[];
}

export interface EventsAvailableResponse {
  dates: DayToDisplay[];
  events: Event[];
  trainers: Trainer[];
  eventTypes: EventType[];
}

export interface Registration {
  id: number;
  created_at: number;
  deleted_at: number;
  canceled_at: number;
  event?: Event
}

export interface RegisterEventRequest{
  idEvent: number;
  equipment: number[];
}
