import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { environment } from 'src/environments/environment';
import { AuthService } from '../auth/services/auth.service';
import { Billing } from '../models/billing.model';
import { Checkout } from '../models/checkout.model';
import { Departamento } from '../models/departamento.model';
import { PartnerSales } from '../models/partners-sales.model';
import { PaymentPlan } from '../models/payment-plan.model';
import { Place } from '../models/places.model';
import { ResultApi } from '../models/result-api.model';
import { StoreERP } from '../models/store.model';
import { User } from '../models/user.model';
import { Seller } from '../models/seller.model';
import { CategoryPartner } from '../models/category-partner.model';
import { Ramo } from '../models/ramo.model';

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

  constructor(
    private http: HttpClient,
    private authService: AuthService,
  ) { }

  getStore(): Observable<StoreERP[]> {
    const url = environment.url + `lists/store/user/${this.authService.getUser()}`;
    try {
      const response = this.http.get<StoreERP[]>(url);
      return response;
    } catch (e) {
      console.log('Erro ao consultar filiais');
      console.log(e);
    }
  }

  getCheckout(store: string): Observable<Checkout[]> {
    const url = environment.url + `lists/checkout/${store}`;
    try {
      const response = this.http.get<Checkout[]>(url);
      return response;
    } catch (e) {
      console.log('Erro ao consultar caixas da filial');
      console.log(e);
    }
  }

  getUser(store: string): Observable<User[]> {
    const url = environment.url + `lists/user/${store}`;
    try {
      const response = this.http.get<User[]>(url);
      return response;
    } catch (e) {
      console.log('Erro ao consultar usuários da filial');
      console.log(e);
    }
  }

  getPaymentPlan(billingId: string = '9999', customerId: number = 1): Observable<PaymentPlan[]> {
    const url = environment.url + `lists/paymentplan/${billingId}/${customerId}`;
    try {
      const response = this.http.get<PaymentPlan[]>(url);
      return response;
    } catch (e) {
      console.log('Erro ao consultar planos de pagamentos');
      console.log(e);
    }
  }

  getBilling(customerId: number): Observable<Billing[]> {
    const url = environment.url + `lists/billing/${customerId}`;
    try {
      const response = this.http.get<Billing[]>(url);
      return response;
    } catch (e) {
      console.log('Erro ao consultar cobrancas');
      console.log(e);
    }
  }

  getDepartments(): Observable<Departamento[]> {
    const url = environment.url + `department/all`;
    try {
      const response = this.http.get<ResultApi>(url).pipe(map(result => result.data));
      return response;
    } catch (e) {
      console.log('Erro ao consultar departamentos');
      console.log(e);
    }
  }

  getPartners(): Observable<PartnerSales[]> {
    const url = environment.url + `lists/partners`;
    return this.http.get<PartnerSales[]>(url);
  }


  getSellers(): Observable<Seller[]> {
    console.log('pesquisando sellers');
    const url = environment.url + `sellers`;
    return this.http.get<Seller[]>(url);
  }

  getCategoryPartner(): Observable<CategoryPartner[]> {
    const url = environment.url + `partner/category`;
    return this.http.get<CategoryPartner[]>(url);
  }


  getPlaces(): Observable<Place[]> {
    const url = environment.url + `lists/places`;
    return this.http.get<Place[]>(url);
  }

  getStorePlaces(): Observable<Place[]> {
    const url = environment.url + `lists/store-places`;
    return this.http.get<Place[]>(url);
  }

  getRamos(): Observable<Ramo[]> {
    const url = environment.url + `lists/ramo`;
    return this.http.get<Ramo[]>(url);
  }

  getSupervisores(): Observable<any[]> {
    const url = environment.url + `lists/supervisores`;
    return this.http.get<any[]>(url);
  }

}
