import { Inject, Injectable, PLATFORM_ID } from '@angular/core';
import { Cities } from '../../../entities/cities';
import { Leads } from '../../../entities/leads';
import { Services } from '../../../entities/services';
import { LeadService } from '../loanService/lead.service';
import { GlobalService } from './global.service';
import Swal from 'sweetalert2';
import { ToastrService } from 'ngx-toastr';
import { isPlatformBrowser } from '@angular/common';

@Injectable({
  providedIn: 'root'
})
export class CommonService {
  public cities: Cities[] = [];
  public services: Services[] = [];
  public serviceId: number = 0;
  constructor(
    private globalService: GlobalService,
    private leadService: LeadService,
    private toastrService: ToastrService,
    @Inject(PLATFORM_ID) public platformId: object
  ) { }

  // public async getCitites(): Promise<Cities[]> {
  //   let res = await this.globalService.getCities();
  //   if (res && res.status == 200) {
  //     this.cities = res.recordList;
  //     if (this.cities && this.cities.length > 0) {
  //       let cityName = this.cities.map(c => c.name.toLowerCase());
  //       if (isPlatformBrowser(this.platformId))
  //         sessionStorage.setItem("cityList", JSON.stringify(cityName))
  //       return this.cities;
  //     }
  //     else
  //       return [];
  //   }
  //   else {
  //     return [];
  //   }
  // }
  public async getCitites(): Promise<Cities[]> {
    let res = await this.globalService.getCities();
    if (res && res.status == 200) {
      this.cities = res.recordList;
      if (this.cities && this.cities.length > 0) {
        let cityName = this.cities.map(c => c.name.toLowerCase());
        if (isPlatformBrowser(this.platformId))
          sessionStorage.setItem("cityList", JSON.stringify(cityName))
        return this.cities;
      }
      else
        return [];
    }
    else {
      return [];
    }
  }

  public async getServices() {
    this.services = [];
    try {
      let res = await this.globalService.getService();
      if (res && res.status == 200)
        this.services = res.recordList;
      if (isPlatformBrowser(this.platformId))
        sessionStorage.setItem("Services", JSON.stringify(this.services));
    } catch (error: any) {
      let err = error;
      if (error && error.error && error.error.message) {
        err = error.error.message
      }
      this.toastrService.error(err, "Error");
    }
  }

  public getServiceId(serviceName: string) {
    if (isPlatformBrowser(this.platformId))
      this.services = JSON.parse(sessionStorage.getItem("Services") as any)
    let index = this.services.findIndex(c => c.name == serviceName);
    if (index >= 0) {
      this.serviceId = this.services[index].id;
      return this.serviceId;
    }
    else {
      return -1;
    }

  }



  public async insertLeads(leads: Leads) {
    try {
      if (leads.id) {
        let id = leads.id
        let res = await this.leadService.updateLeads(leads);
        if (res && res.status == 200) {
          return res.recordList
        }
        else {
          return [];
        }
      }
      else {
        let res = await this.leadService.insertLeads(leads);
        if (res && res.status == 200) {
          if (res.recordList && res.recordList.length > 0) {
            return res.recordList[0].id
          }
          else
            return -1;
        }
      }
    } catch (error: any) {

    }
  }

  public showSuccess() {
    Swal.fire({
      icon: 'success',
      text: 'You Successfully generate the lead',
      confirmButtonColor: '#0162e8'
    })
  }

  public showSucessSignUp() {
    Swal.fire({
      icon: 'success',
      text: 'Your Request Accepted!',
      confirmButtonColor: '#0162e8'
    })
  }

  public showWarning() {
    Swal.fire({
      icon: 'warning',
      title: 'Oops',
      text: 'Sorry!Invalid OTP Number',
      confirmButtonColor: '#0162e8',
      confirmButtonText: 'Retry'
    })
  }
}
