import { Injectable, EventEmitter } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Storage } from '@ionic/storage';

import { BaseService } from "./base.service"

import 'rxjs/add/operator/mergeMap';
import 'rxjs/add/observable/fromPromise';
import { Observable } from 'rxjs';

@Injectable()
export class DoctorService extends BaseService{

  public cartFull: any;
  public finishCompra = new EventEmitter<any>();

  constructor(public http: HttpClient, public storage: Storage) {
    super(http, storage);
  }
  getToken() {
    return Observable.fromPromise(this.storage.get('token'));
  }
  addCartItem(o:any) {
    let found = 0;
    let error = 0;
    for (let x = 0; x < this.cartFull.items.length; x++) {
      if (this.cartFull.items[x].id == o.id) {
        let newsum = parseFloat(this.cartFull.items[x].qty) + parseFloat(o.qty);
        if (o.stock < newsum) {
          return false;
        }
        else {
          this.cartFull.items[x].qty = newsum;
          this.cartFull.items[x].total = newsum * this.cartFull.items[x].price;
          found = 1;
        }
      }
    }
    if (found == 0) { 
      if (o.stock < o.qty) {
        return false;
      }
      else {
        this.cartFull.items.push(o);
        return true;
      }
    }
    else { // encontrado sin problemas de stock
      return true;
    }
  }
  syncCart() {
    let c = this.post('customers/cart', {
      cart: this.cartFull.items
    }).share();
    c.subscribe((data:any)=>{});
    return c;
  }
  getCart() {
    let c = this.get('customers/cart').share();
    c.subscribe((data:any)=>{
      this.cartFull = data.data;
    });
    return c;
  }
  search(search:string) {
    return this.post('search', {search:search});
  }
  getTerms() {
    return this.get('terms');
  }
  acceptTerms() {
    return this.get('terms/ok');
  }
  /* Another */
  getRecetas() {
    return this.get('prescriptions');
  }
  deleteReceta(id:string) {
    return this.post('prescriptions/delete', {id: id});
  }
  savePrescription(data:any) {
    return this.post('prescriptions/save',data);
  }
  getCards() {
    return this.get('customers/cards');
  }
  getBalance() {
    return this.get('customers/balance');
  }
  deleteTarjeta(id:string) {
    return this.post('customers/cards/delete', {id: id});
  }
  getBalanceGrow() {
    return this.get('prescriptions/balance');
  }
  getHistory() {
    return this.get('customers/history');
  }
  featured() {
    return this.get('products/featured');
  }
  getCategory(id?:string) {
    if (id) {
      return this.get('category/'+id);
    } else {
      return this.get('products/category');
    }
  }
  getProduct(id?:string) {
    return this.get('products/detail/'+id);
  }
  getAddresses() {
    return this.get('customers/addresses');
  }
  putPedido(order:any) {
    return this.post('orders/save', order);
  }
  shippingPrice(idCity:any) {
    return this.get('orders/shipping?city='+idCity);
  }
  getOrders(id?:string) {
    if (id) {
      return this.get('customers/orders/'+id);
    }
    else {
      return this.get('customers/orders'); 
    }
  }
  deleteOrder(id:string) {
    return this.post('customers/orders/delete', {id: id});
  }

  getTbkOrder(id:string) {
    return this.get('tbk/'+id);
  }
  address() {
    return this.get('address');
  }

  masterdata() {
    return this.get('masterdata');
  }
  trucks() {
    return this.get('vehicle/truck');
  }
  trailers() {
    return this.get('vehicle/trailer');
  }

  getTruck(){
    return this.get('vehicle/truck');
  }

  getTrailer(){
    return this.get('vehicle/trailer');
  }
  getOfferForReq(param: any){
    return this.get('requeriments/offers/'+param);
  }
  getDrivers(){
    return this.get('drivers');
  }
  getDriverService(){
    return this.get('services/open');
  }
  getDriverServiceInProgress(){
    return this.get('services/onservice');
  }

  getTrucking(param:any){
    return this.get('services/tracking/'+param);
  }
  getRating(param:any){
    return this.get('users/raiting/'+param);
  }
  getService(){
    return this.get('services');
  }
  getRates(){
    return this.get('services/rates');
  }
  getRatesId(param:any){
    return this.get('services/rates/'+param);
  }
  getWaitings(lat:number, lng: number) {
    return this.get('deals/waitings?lat='+lat+'&lng='+lng);
  }
  getOfferConditions() {
    return this.get('offers/conditions');
  }


  //POST

  addAdress(params: any){
    return this.post('address', params);
  }

  saveDisponibilidad(params: any) {
    return this.post('users/availability', params);
  }

  addRequeriment(params: any){
    return this.post('requirements/add', params);
  }
  addVehicle(params: any){
    return this.post('vehicle/add', params);
  }
  editVehicle(params: any){
    return this.post('vehicle/update', params);
  }
  evaluateOffer(params:any){
    return this.post('offers/evaluate', params);
  }
  setDriverStatus(params:any){
    return this.post('drivers/set', params);
  }
  sendPositionTraking(params:any){
    return this.post('services/tracking', params);
  }
  setStartService(params:any){
    return this.post('services/start', params);
  }
  setStopService(params:any){
    return this.post('services/finish', params);
  }
  matchOferta(params: any){
    return this.post('requirements/match',params);
  }
  addOffer(params: any){
    return this.post('offers/add',params);
  }
  updateTruchAndTrailer(params: any){
    return this.post('drivers/update',params);
  }
  sendRate(params: any){
    return this.post('users/rate', params);
  }
  declineOffer(params: any){
    return this.post('offers/decline', params);
  }
  sendLatLong(params: any){
    return this.post('address/latlng', params);
  }
  
  //Delete
  deleteRequeriment(params:any){
    return this.post('requirements/delete',params);
  }
  deleteOffer(params:any){
    return this.post('offers/delete',params);
  }
  serviceQuery(id:string) {
    return this.get("services/driverdata/"+id);
  }
  serviceUpdateDriverData(id: number, distance: string, actual: string, timer: string, lat: number, lng: number) {
    return this.post('services/driverdata/'+id, {
      distance: distance,
      actual: actual,
      timer: timer,
      lat: lat,
      lng: lng
    });
  }
  setServiceGuia(service_id: string, guia: string) {
    return this.post('services/setdocument/'+service_id, {document: guia});
  }

  getAlertaTransportista() {
    return this.get('requirements/new_requirement_alert');
  }
  getAlertaGenerador() {
    return this.get('requirements/new_offer_alert');
  }
}
