import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { environment } from 'src/environments/environment';

import { User } from '../models/user.model';
import { TokenService } from './token.service';
import { Company } from '../models/company.model';
import { Box } from '../models/box.model';
import { Pricing } from '../models/pricing.model';
import { Discount } from '../models/discount.model';
import { BoxOrder } from '../models/box-order.model';
import { PaymentType } from '../models/payment.model';
import { CompanySubscription } from '../models/company-subscription.model';
import { CompanyOrder } from '../models/company-order.model';
import { New } from '../models/new.model';
import { Plan } from '../models/plan.model';
import { RegisterRequest } from '../models/register-request.model';
import { RegisterResponse } from '../models/register-response.model';
import { Subscription } from '../models/subscription.model';
import { UserSubscription } from '../models/user-subscription.model';
import { TrainingSession } from '../models/training-session.model';
import { map, filter } from 'rxjs/operators';
import * as moment from 'moment';
import { TrainingRoom } from '../models/training-room.model';
import { TrainingType, TrainingWorkout } from '../models/training-workout.model';
import { CalendarTemplate } from '../models/calendar-template.model';
import { Invoice } from '../models/invoice.model';
import { Role } from '../models/role.model';
import { StripeUser } from '../models/stripe-user.model';
import { stringify } from 'querystring';
import { BoxSetting } from '../models/box-setting.model';
import { Question } from '../models/question.model';

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

  private host:string = environment.baseApiUrl;

  constructor(private http:HttpClient,
              private token:TokenService) { }

  public getHost() {
    return this.host;
  }

  private getAuthenticationHeaders(): HttpHeaders {
    return new HttpHeaders({
      "Content-Type": "application/json",
      "Authorization": "Bearer " + this.token.get()
    });
  }

  private getAuthenticationMultipartHeaders(): HttpHeaders {
    return new HttpHeaders({
      "Authorization": "Bearer " + this.token.get()
      //Implicit content type form multipart + automatic boundaries
    });
  }

  private toStringDate(d:Date):string {
    return `${d.getFullYear()}-${d.getMonth()+1}-${d.getDate()} ${('00' + d.getHours()).slice(-2)}:${('00' + d.getMinutes()).slice(-2)}:${('00' + d.getSeconds()).slice(-2)}`;
  }

  private setParams(filters:any, params:HttpParams = new HttpParams()) {
    Object.entries(filters).forEach((val:[string,string]) => {
      if(val[1]!=undefined) params = params.set(val[0], val[1]);
    });

    return params;
  }
  
  /**
   * -------------------------------------------------- AUTHENTICATION --------------------------------------------------
   */
  me() {
    return this.http.get<User>(`${this.host}/auth/me`); // TODO
  }

  refreshToken() {
    return this.http.post<any>(`${this.host}/auth/refresh`, null);
  }

  login(data:any, custom_user_id:string = null) {
    let params:HttpParams = new HttpParams();
    if(custom_user_id!=null) params = params.set("custom_user_id", custom_user_id);
    return this.http.post<any>(`${this.host}/auth/login`, data, { params: params });
  }

  sendResetPasswordRequest(email:string) {
    return this.http.post<any>(`${this.host}/auth/sendPasswordResetLink`, { email: email });
  }

  changePassword(token:string, password:string) {
    return this.http.post<any>(`${this.host}/auth/changePassword`, { token: token, password: password });
  }

  registerUser(user:User) {
    return this.http.post<User>(`${this.host}/auth/register`, user);
  }

  /**
   * -------------------------------------------------- COMPANY --------------------------------------------------
   */

  //GET /companies
  companies() {
    return this.http.get<any>(`${this.host}/companies`);
  }

  //GET /companies/:id
  company(id:number) {
    return this.http.get<Company>(`${this.host}/companies/${id}`);
  }

  //POST /companies
  createCompany(body:RegisterRequest) {
    return this.http.post<RegisterResponse>(`${this.host}/companies`, body);
  }

  //PUT /companies
  updateCompany(id:number, company: Company) {
    return this.http.put<Company>(`${this.host}/companies/${id}`, company);
  }

  //DELETE /companies
  deleteCompany(id:number) {
    return this.http.delete<string>(`${this.host}/companies/${id}`);
  }

  //GET /companies/:id/boxes
  companyBoxes(id:number,page_number?:number, page_size?:number) {
    let params = new HttpParams();
    if(page_number != null) params = params.set("page_number", page_number.toString());
    if(page_size != null) params = params.set("page_size", page_size.toString());
    return this.http.get<any>(`${this.host}/companies/${id}/boxes`, { params });
  }

  //GET /companies/:id/pricing-plans
  companyPricings(id:number,page_number?:number, page_size?:number) {
    let params = new HttpParams();
    if(page_number != null) params = params.set("page_number", page_number.toString());
    if(page_size != null) params = params.set("page_size", page_size.toString());
    return this.http.get<any>(`${this.host}/companies/${id}/pricing-plans`, { params }).pipe(
      map(data => {
        data.data = data.data.map((p:Pricing) => {
          if(p.box_ids == null) p.box_ids = [];
          p.boxes.forEach(b => { p.box_ids.push(b.id); })
          return p;
        }); 
        return data;
      })
    );
  }

  //GET /companies/:id/users
  companyUsers(id:number,page_number?:number, page_size?:number) {
    let params = new HttpParams();
    if(page_number != null) params = params.set("page_number", page_number.toString());
    if(page_size != null) params = params.set("page_size", page_size.toString());
    return this.http.get<any>(`${this.host}/companies/${id}/users`, { params });
  }

  //GET /companies/:id/discounts
  companyDiscounts(id:number, active:boolean = null) {
    let params = new HttpParams();
    if(active && active==true) params = params.set("active", "1");
    return this.http.get<any>(`${this.host}/companies/${id}/discounts`, {params: params});
  }

  //GET /companies/:id/box-orders
  companyBoxOrders(id:number,page_number?:number, page_size?:number) {
    let params = new HttpParams();
    if(page_number != null) params = params.set("page_number", page_number.toString());
    if(page_size != null) params = params.set("page_size", page_size.toString());
    return this.http.get<any>(`${this.host}/companies/${id}/box-orders`, { params });
  }

  //GET /companies/:id/payment-types
  companyPaymentTypes(id:number,page_number?:number, page_size?:number) {
    let params = new HttpParams();
    if(page_number != null) params = params.set("page_number", page_number.toString());
    if(page_size != null) params = params.set("page_size", page_size.toString());
    return this.http.get<any>(`${this.host}/companies/${id}/payment-types`, { params });
  }

  //GET /companies/:id/subscriptions
  companySubscriptions(id:number,page_number?:number, page_size?:number) {
    let params = new HttpParams();
    if(page_number != null) params = params.set("page_number", page_number.toString());
    if(page_size != null) params = params.set("page_size", page_size.toString());
    return this.http.get<any>(`${this.host}/companies/${id}/subscriptions`, { params });
  }

  //GET /companies/:id/company-orders
  companyCompanyOrders(id:number,page_number?:number, page_size?:number) {
    let params = new HttpParams();
    if(page_number != null) params = params.set("page_number", page_number.toString());
    if(page_size != null) params = params.set("page_size", page_size.toString());
    return this.http.get<any>(`${this.host}/companies/${id}/company-orders`, { params });
  }

  //GET /companies/:id/news
  companyNews(id:number, filters:any) {
    const params:HttpParams = this.setParams(filters);
    return this.http.get<any>(`${this.host}/companies/${id}/news`, { params: params });
  }

  //GET /companies/:id/user-subscriptions
  getCompanyUserSubscriptions(id:string, page_size:number = null, page_number:number = null, options:any = null) {
    let params = new HttpParams();
    if(page_size!=null) params = params.set("page_size", page_size.toString());
    if(page_number!=null) params = params.set("page_number", page_number.toString());
    params = this.setParams(options, params);
    return this.http.get<any>(`${this.host}/companies/${id}/user-subscriptions`, { params: params });
  }

  getCompanyStripeUserByUserId(company_id:string, user_id:string) {
    return this.http.get<StripeUser>(`${this.host}/companies/${company_id}/users/${user_id}/stripe-user`).pipe(
      map(u => u.id ? u : null)
    );
  }

  getCompanyUserUserSubscriptions(id:string, user_id:string) {
    return this.http.get<any>(`${this.host}/companies/${id}/users/${user_id}/user-subscriptions`)
  }

  uploadCompanyProfileImage(id:string, file:File) {
    const formData = new FormData();
    formData.append("photo", file);

    return this.http.post<Company>(`${this.host}/companies/${id}/update-profile-image`, formData);
  }

  getCompanySubscriptionsAndOrders(id:string) {
    return this.http.get<any>(`${this.host}/companies/${id}/subscriptions/orders`);
  }

  /**
   * -------------------------------------------------- BUSINESS INTELLIGENCE --------------------------------------------------
   */
  dashboard(id_company:number, id_box?:number, data_ini?:string, data_fi?:string) {
    let params = new HttpParams();
    if (id_company != null) params = params.set("company_id",id_company.toString());
    if (id_box != null) params = params.set("box_id",id_box.toString());
    if (data_ini != null) params = params.set("data_ini",data_ini);
    if (data_fi != null) params = params.set("data_fi",data_fi);

    return this.http.get<any>(`${this.host}/business-intelligence`, {  params });
  }

  getBilling(id_company:number, id_box?:number, data_ini?:string, data_fi?:string) {
    let params = new HttpParams();
    if (id_company != null) params = params.set("company_id",id_company.toString());
    if (id_box != null) params = params.set("box_id",id_box.toString());
    if (data_ini != null) params = params.set("data_ini",data_ini);
    if (data_fi != null) params = params.set("data_fi",data_fi);

    return this.http.get<any>(`${this.host}/business-intelligence/billing`, {  params });
  }

  getOrdersBusinessIntelligence(id_company:number, id_box?:number, data_ini?:string, data_fi?:string, page_size?:number, page_number?:number, options?:any) {
    let params = new HttpParams();
    if (id_company != null) params = params.set("company_id",id_company.toString());
    if (id_box != null) params = params.set("box_id",id_box.toString());
    if (data_ini != null) params = params.set("data_ini",data_ini);
    if (data_fi != null) params = params.set("data_fi",data_fi);
    if(page_size!=null) params = params.set("page_size", page_size.toString());
    if(page_number!=null) params = params.set("page_number", page_number.toString());
    params = this.setParams(options, params);

    return this.http.get<any>(`${this.host}/business-intelligence/orders`, {  params });
  }

  getAssists(id_company:number, id_user:number, id_box?:number, data_ini?:string, data_fi?:string) {
    let params = new HttpParams();
    if (id_company != null) params = params.set("company_id",id_company.toString());
    if (id_user != null) params = params.set("user_id",id_user.toString());
    if (id_box != null) params = params.set("box_id",id_box.toString());
    if (data_ini != null) params = params.set("data_ini",data_ini);
    if (data_fi != null) params = params.set("data_fi",data_fi);

    return this.http.get<any>(`${this.host}/business-intelligence/assists`, {  params });
  }

  getWorkoutsBusinessIntelligence(id_company:number, id_box?:number, page_size?:number, page_number?:number, options?:any) {
    let params = new HttpParams();
    if (id_company != null) params = params.set("company_id",id_company.toString());
    if (id_box != null) params = params.set("box_id",id_box.toString());
    if (page_size!=null) params = params.set("page_size", page_size.toString());
    if (page_number!=null) params = params.set("page_number", page_number.toString());
    params = this.setParams(options, params);

    return this.http.get<any>(`${this.host}/business-intelligence/workouts`, {  params });
  }

  getWorkoutBusinessIntelligence(id_company:number, id_workout:string, id_box?:number) {
    let params = new HttpParams();
    if (id_company != null) params = params.set("company_id",id_company.toString());
    if (id_workout != null) params = params.set("workout_id",id_workout);
    if (id_box != null) params = params.set("box_id",id_box.toString());

    return this.http.get<any>(`${this.host}/business-intelligence/workout`, {  params });
  }
  
  /**
   * -------------------------------------------------- BOX ORDERS --------------------------------------------------
   */
  
  getOrder(id:number) {
      return this.http.get<BoxOrder>(`${this.host}/box-orders/${id}`);
  }
  
  /* -------------------------------------------------- BOX --------------------------------------------------
  * 
  */
  postBox(box:Box) {
    return this.http.post<Box>(`${this.host}/boxes/`, box);
  }
  
  putBox(box:Box) {
    return this.http.put<Box>(`${this.host}/boxes/${box.id}`, box);
  }

  deleteBox(id:string) {
    return this.http.delete<Box>(`${this.host}/boxes/${id}`);
  }

  getBoxUserSubscriptions(id:string, page_size:number = null, page_number:number = null, options:any = null) {
    let params = new HttpParams();
    if(page_size!=null) params = params.set("page_size", page_size.toString());
    if(page_number!=null) params = params.set("page_number", page_number.toString());
    params = this.setParams(options, params);
    return this.http.get<any>(`${this.host}/boxes/${id}/user-subscriptions`, { params: params });
  }

  getBoxTrainingSessions(id:string, training_room_id:number = null, date_start:Date = null, date_end:Date = null) {
    let params:HttpParams = new HttpParams();
    if(date_start != null) params = params.set("date_start", this.toStringDate(date_start));
    if(date_end != null) params = params.set("date_end", this.toStringDate(date_end));
    if(training_room_id != null) params = params.set("training_room_id", training_room_id.toString());
    return this.http.get<any>(`${this.host}/boxes/${id}/training-sessions`, { params: params }).pipe(
      map(tss => {
        tss.data.forEach(ts => {
          ts.date_start = moment(ts.date_start).toDate();
          ts.date_end = moment(ts.date_end).toDate();
        });
        return tss;
      })
    );
  }

  getBoxCoaches(id:string) {
    return this.http.get<any>(`${this.host}/boxes/${id}/coaches`);
  }

  getUsersThatCanBeCoaches(id:string) {
    return this.http.get<any>(`${this.host}/boxes/${id}/users-can-be-coaches`);
  }

  getBoxTrainingWorkouts(id:string, filters:Object = []) {
    const params:HttpParams = this.setParams(filters);
    return this.http.get<any>(`${this.host}/boxes/${id}/workouts`, { params: params });
  }

  getBoxTrainingWorkoutCategories(id:string) {
    return this.http.get<any>(`${this.host}/boxes/${id}/workouts/categories`);
  }

  setBulkWorkoutByBoxId(id:string, body:any) {
    return this.http.put<TrainingSession[]>(`${this.host}/boxes/${id}/training-sessions/bulk-edit-workouts`, body);
  }

  getBoxCalendarTemplates(id:string) {
    return this.http.get<any>(`${this.host}/boxes/${id}/calendar-templates`).pipe(
      map(cts => {
        cts.data.forEach(ct => {
          ct.start_day_time = moment(ct.start_day_time, "HH:mm:ss").add(2, 'hours').format("HH:mm:ss");
          ct.end_day_time = moment(ct.end_day_time, "HH:mm:ss").add(2, 'hours').format("HH:mm:ss");
          if(ct.calendar_template_exclusions) {
            ct.calendar_template_exclusions.forEach(excl => {
              excl.start_time_exclusion = moment(excl.start_time_exclusion, "HH:mm:ss").add(2, 'hours').format("HH:mm:ss");
              excl.end_time_exclusion = moment(excl.end_time_exclusion, "HH:mm:ss").add(2, 'hours').format("HH:mm:ss");
            });
          }
        });
        return cts;
      })
    );
  }

  postBoxGenerateCalendar(id:string, body:any) {
    return this.http.post<TrainingSession[]>(`${this.host}/boxes/${id}/generate-calendar`, body);
  }

  getBoxPricings(id:string) {
    return this.http.get<any>(`${this.host}/boxes/${id}/pricings`);
  }

  getBoxNews(id:string, filters:any) {
    const params:HttpParams = this.setParams(filters);
    return this.http.get<any>(`${this.host}/boxes/${id}/news`, { params: params });
  }

  getBoxSettings(id:string) {
    return this.http.get<any>(`${this.host}/boxes/${id}/settings`);
  }

  updateBoxSettings(id:string, settings:BoxSetting[]) {
    return this.http.put<any>(`${this.host}/boxes/${id}/settings`, settings);
  }
  
  getBoxTrainingRooms(id:string, filters:any = {}) {
    const params:HttpParams = this.setParams(filters);
    return this.http.get<any>(`${this.host}/boxes/${id}/training-rooms`, { params: params });
  }

  /**
   * -------------------------------------------------- USER --------------------------------------------------
   */
  getUsers(filters:any) {
    const params:HttpParams = this.setParams(filters);
    return this.http.get<any>(`${this.host}/users/`, { params: params });
  }

  getUser(id:string) {
    return this.http.get<User>(`${this.host}/users/${id}`);
  }

  getUserUserSubscriptions(id:string, active:boolean = null) {
    let params = null;
    if(active!=null) {
      params = new HttpParams();
      params = params.set("active", active ? 1 : 0);
    }
    return this.http.get<any>(`${this.host}/users/${id}/user-subscriptions`, { params: params });
  }

  /**
   * -------------------------------------------------- PLANS --------------------------------------------------
   */
  getAllPlans() {
    return this.http.get<any>(`${this.host}/plans`);
  }

  /**
   * -------------------------------------------------- LEGAL --------------------------------------------------
   */
  getTerminosCondiciones() {
    return this.http.get<any>(`${this.host}/legal/terminos-y-condiciones-de-uso`);
  }

  getPoliticaPrivacitat() {
    return this.http.get<any>(`${this.host}/legal/politica-de-privacidad`);
  }

  getAvisoLegalBasico() {
    return this.http.get<any>(`${this.host}/legal/aviso-legal/basico`);
  }

  getAvisoLegalCompleto() {
    return this.http.get<any>(`${this.host}/legal/aviso-legal/completo`);
  }

  getAvisoLegalCompletoAtleta(company_id:string) {
    let params = new HttpParams();
    params = params.set("company_id", company_id);
    return this.http.get<any>(`${this.host}/legal/aviso-legal/completo-atleta`, { params : params });
  }

  /**
   * -------------------------------------------------- SUBSCRIPTIONS --------------------------------------------------
   */
  postSubscriptionSubscribe(params:any) {
    const http_params:HttpParams = this.setParams(params);
    return this.http.post<Subscription>(`${this.host}/subscriptions/subscribe`, null, { params: http_params });
  }

  putSubscriptionPay(id:string, override:boolean = null) {
    let params = null;
    if(override!=null) {
      params = new HttpParams();
      params = params.set("override", override ? 1 : 0);
    }
    return this.http.put<Subscription>(`${this.host}/subscriptions/${id}/pay`, null, { params: params });
  }

  deleteCancelSubscription(id:string) {
    return this.http.delete<any>(`${this.host}/subscriptions/${id}/cancel`);
  }

  getUserSubscription(id:string) {
    return this.http.get<UserSubscription>(`${this.host}/user-subscriptions/${id}`);
  }

  /**
   * -------------------------------------------------- USER SUBSCRIPTIONS --------------------------------------------------
   */
  getUserSubscriptionById(id:string) {
    return this.http.get<UserSubscription>(`${this.host}/user-subscriptions/${id}`).pipe(
      map(us => {
        if(us.start_date) us.start_date = moment(us.start_date.toString()).toDate();
        if(us.end_date) us.end_date = moment(us.end_date.toString()).toDate();
        return us;
      })
    );
  }

  putUserSubscription(us:any, box_id:string) {
    let params = new HttpParams();
    params = params.set("box_id", box_id.toString());
    if(us.start_date) us.start_date = moment(us.start_date).format("YYYY-MM-DD");
    if(us.end_date) us.end_date = moment(us.end_date).format("YYYY-MM-DD");
    return this.http.put<UserSubscription>(`${this.host}/user-subscriptions/${us.id}`, us, { params: params }).pipe(
      map(us => {
        if(us.start_date) us.start_date = moment(us.start_date.toString()).toDate();
        if(us.end_date) us.end_date = moment(us.end_date.toString()).toDate();
        return us;
      })
    );
  }

  generateUserSubscription(params:any) {
    const paramsHttp:HttpParams = this.setParams(params);
    return this.http.post<UserSubscription>(`${this.host}/user-subscriptions/`, null, { params: paramsHttp});
  }

  renewUserSubscription(id:string, params:any) {
    const paramsHttp:HttpParams = this.setParams(params);
    return this.http.put<[string,UserSubscription,BoxOrder]>(`${this.host}/user-subscriptions/${id}/renew`, null, { params: paramsHttp});
  }

  payUserSubscription(id:string, params:any) {
    const paramsHttp:HttpParams = this.setParams(params);
    return this.http.put<UserSubscription>(`${this.host}/user-subscriptions/${id}/pay`, null, {params: paramsHttp});
  }

  /**
   * -------------------------------------------------- TRAINING SESSIONS --------------------------------------------------
   */

  postTrainingSession(ts:any) {
    ts.date_start = moment(ts.date_start).format();
    ts.date_end = moment(ts.date_end).format();
    return this.http.post<TrainingSession>(`${this.host}/training-sessions`, ts);
  }

  putTrainingSession(ts:any) {
    ts.date_start = moment(ts.date_start).format();
    ts.date_end = moment(ts.date_end).format();
    return this.http.put<TrainingSession>(`${this.host}/training-sessions/${ts.id}`, ts);
  }

  deleteTrainingSession(id:string) {
    return this.http.delete<any>(`${this.host}/training-sessions/${id}`);
  }

  getTrainingSessionUsers(id:string) {
    return this.http.get<any>(`${this.host}/training-sessions/${id}/users`);
  }

  /**
   * -------------------------------------------------- TRAINING TYPES --------------------------------------------------
   */
  getTrainingTypes() {
    return this.http.get<any>(`${this.host}/training-types`);
  }

  /**
   * -------------------------------------------------- TRAINING WORKOUTS --------------------------------------------------
   */
  getTrainingWorkouts() {
    return this.http.get<any>(`${this.host}/workouts`);
  }

  /**
   * -------------------------------------------------- INVOICES --------------------------------------------------
   */
  getHTMLInvoice(id:string) {
    const headers: HttpHeaders = new HttpHeaders({'Accept': 'text/html'});
    return this.http.get(`${this.host}/invoices/${id}/html`, { headers: headers, responseType: 'text' });
  }

  getCSVInvoice(id:string, invoices_id?:string) {
    let params = null;
    if (invoices_id) {
      params = new HttpParams();
      params = params.set("invoices_id", invoices_id);
    }
    return this.http.get(`${this.host}/companies/${id}/invoicesExcel`, {responseType: 'blob', params: params});
  }

  updateInvoice(invoice_id:number, box_id:string, body:any) {
    let params = new HttpParams();
    params = params.set("box_id", box_id);
    return this.http.put<Invoice>(`${this.host}/invoices/${invoice_id}`, body, { params: params });
  }
  /*-------------------------------------------------- CALENDAR TEMPLATES --------------------------------------------------
  */
  postCalendarTemplate(ct:CalendarTemplate) {
    ct.start_day_time = moment(ct.start_day_time, "HH:mm:ss").subtract(2, 'hours').format("HH:mm:ss"); //Passem a UTC
    ct.end_day_time = moment(ct.end_day_time, "HH:mm:ss").subtract(2, 'hours').format("HH:mm:ss");
    if(ct.calendar_template_exclusions) {
      ct.calendar_template_exclusions.forEach(excl => {
        excl.start_time_exclusion = moment(excl.start_time_exclusion, "HH:mm:ss").subtract(2, 'hours').format("HH:mm:ss"); //Passem a UTC
        excl.end_time_exclusion = moment(excl.end_time_exclusion, "HH:mm:ss").subtract(2, 'hours').format("HH:mm:ss"); //Passem a UTC
      });
    }
    return this.http.post<CalendarTemplate>(`${this.host}/calendar-templates`, ct);
  }

  putCalendarTemplate(ct:CalendarTemplate) {
    ct.start_day_time = moment(ct.start_day_time, "HH:mm:ss").subtract(2, 'hours').format("HH:mm:ss"); //Passem a UTC
    ct.end_day_time = moment(ct.end_day_time, "HH:mm:ss").subtract(2, 'hours').format("HH:mm:ss");
    if(ct.calendar_template_exclusions) {
      ct.calendar_template_exclusions.forEach(excl => {
        excl.start_time_exclusion = moment(excl.start_time_exclusion, "HH:mm:ss").subtract(2, 'hours').format("HH:mm:ss"); //Passem a UTC
        excl.end_time_exclusion = moment(excl.end_time_exclusion, "HH:mm:ss").subtract(2, 'hours').format("HH:mm:ss"); //Passem a UTC
      });
    }
    return this.http.put<CalendarTemplate>(`${this.host}/calendar-templates/${ct.id}`, ct);
  }

  deleteCalendarTemplate(id:string) {
    return this.http.delete<any>(`${this.host}/calendar-templates/${id}`);
  }


  /**
   * -------------------------------------------------- ROLES --------------------------------------------------
   */
  getAllRoles() {
    return this.http.get<any>(`${this.host}/roles`).pipe(
      map(o => o.data)
    );
  }

  /* -------------------------------------------------- EXERCISES --------------------------------------------------
  * 
  */
  getAllExercises(name?:string, page_size?:number, page_number?:number) {
    let params = new HttpParams();
    if(name!=null) params = params.set("name", name);
    if(page_size!=null) params = params.set("page_size", page_size.toString());
    if(page_number!=null) params = params.set("page_number", page_number.toString());
    return this.http.get<any>(`${this.host}/exercises`, { params: params });
  }

  
  /* -------------------------------------------------- WORKOUTS --------------------------------------------------
  * 
  */
  createWorkout(workout:TrainingWorkout) {
    return this.http.post<TrainingWorkout>(`${this.host}/workouts`, workout);
  }
  
  updateWorkout(workout:TrainingWorkout) {
    return this.http.put<TrainingWorkout>(`${this.host}/workouts/${workout.id}`, workout);
  }

  deleteWorkout(id:string) {
    return this.http.delete<TrainingWorkout>(`${this.host}/workouts/${id}`);
  }
  /**
   * -------------------------------------------------- PRICINGS --------------------------------------------------
   */
  postPricing(p:Pricing) {
    return this.http.post<Pricing>(`${this.host}/pricings`,p);
  }

  putPricing(p:Pricing) {
    return this.http.put<Pricing>(`${this.host}/pricings/${p.id}`, p);
  }

  deletePricing(id:string) {
    return this.http.delete<Pricing>(`${this.host}/pricings/${id}`);
  }

  /*
  * -------------------------------------------------- DISCOUNTS --------------------------------------------------
  */
  postDiscount(d:Discount) {
    return this.http.post<Box>(`${this.host}/discounts/`, d);
  }

  putDiscount(d:Discount) {
    return this.http.put<Box>(`${this.host}/discounts/${d.id}`, d);
  }

  deleteDiscount(id:string) {
    return this.http.delete<Box>(`${this.host}/discounts/${id}`);
  }

  /* 
  * -------------------------------------------------- NEWS --------------------------------------------------
  */
  postNew(n:New) {
    return this.http.post<New>(`${this.host}/news`, n);
  }

  putNew(n:New) {
    return this.http.put<New>(`${this.host}/news/${n.id}`, n);
  }

  deleteNew(id:string) {
    return this.http.delete<New>(`${this.host}/news/${id}`);
  }

  /* 
  * -------------------------------------------------- TRAINING ROOMS --------------------------------------------------
  */
  postTrainingRoom(r:TrainingRoom) {
    return this.http.post<TrainingRoom>(`${this.host}/training-rooms`, r);
  }

  putTrainingRoom(r:TrainingRoom) {
    return this.http.put<TrainingRoom>(`${this.host}/training-rooms/${r.id}`, r);
  }

  deleteTrainingRoom(id:string) {
    return this.http.delete<TrainingRoom>(`${this.host}/training-rooms/${id}`);
}

  /* -------------------------------------------------- UTILS --------------------------------------------------
  * 
  */
  sendQuestion(question:Question) {
    return this.http.post<any>(`${this.host}/utils/question/`,question);
  }
}
