import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { map, Observable } from 'rxjs';
import { CoinsListResponse } from '../interfaces/coins-list.interface';
import { FormTaxInformation } from '../interfaces/tax-information.interface';
import { RequestFormValidateCatalog, ResponseDataGetValidateCatalog } from '../interfaces/validate-catalog.interface';
import { apiTemp } from '../../environments/environment.prod';

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

  constructor(private _http: HttpClient) { }

  getAdvices(id: string, line: number){
    return this.getQuery(`get-advices/${id}/${line}`).pipe(map((data: any) => data));
  }

  getTwoStepsAuth(id: string, line: number){
    return this.getQuery(`get-two-steps-auths/${id}/${line}`).pipe(map((data: any) => data));
  }

  getPasswordFormat(id: string, line: number){
    return this.getQuery(`get-password-format/${id}/${line}`).pipe(map((data: any) => data));
  }

  getSessionsDuration(id: string, line: number){
    return this.getQuery(`get-sessions-duration/${id}/${line}`).pipe(map((data: any) => data));
  }

  getActiveSessionsNumber(id: string, line: number){
    return this.getQuery(`get-active-sessions-number/${id}/${line}`).pipe(map((data: any) => data));
  }

  getMaintenanceHistory(id: string, line: number){
    return this.getQuery(`get-maintenance-history/${id}/${line}`).pipe(map((data: any) => data));
  }

  getMaintenanceStatus(id: string, line: number, token?: string){
    return this.getQuery(`get-maintenance-status/${id}/${line}`, token).pipe(map((data: any) => data));
  }

  getEmailServers(id: string, line: number){
    return this.getQuery(`get-email-servers/${id}/${line}`).pipe(map((data: any) => data));
  }

  getEmailServer(host: string, user: string, id: string, line: number){
    return this.getQuery(`get-email-server/${host}/${user}/${id}/${line}`).pipe(map((data: any) => data));
  }

  getEmailTemplates(id: string, line: number){
    return this.getQuery(`get-email-templates/${id}/${line}`).pipe(map((data: any) => data));
  }

  getEmailTemplate(id: string, idUser: string, line: number){
    return this.getQuery(`get-email-template/${id}/${idUser}/${line}`).pipe(map((data: any) => data));
  }

  getCatalogueHistory(type: string, user: string, line: number){
    return this.getQuery(`get-catalogue-history/${type}/${user}/${line}`).pipe(map((data: any) => data));
  }

  getMaterialIcons(idUser: string, line: number){
    return this.getQuery(`get-material-icons/${idUser}/${line}`).pipe(map((data: any) => data))
  }

  getLoginImages(idUser: string, line: number){
    return this.getQuery(`get-login-images/${idUser}/${line}`).pipe(map((data: any) => data))
  }

  getSCADA(idSCADA: string, idUser: string, line: number){
    return this.getQuery(`get-scada/${idSCADA}/${idUser}/${line}`).pipe(map((data: any) => data))
  }

  getSCADAList(idUser: string, line: number){
    return this.getQuery(`get-scada-list/${idUser}/${line}`).pipe(map((data: any) => data))
  }

  getGeneratedTokens(idUser: string, line: number){
    return this.getQuery(`get-generated-tokens/${idUser}/${line}`).pipe(map((data: any) => data))
  }

  getWebServices(idUser: string, line: number){
    return this.getQuery(`get-web-services/${idUser}/${line}`).pipe(map((data: any) => data))
  }

  getRoutedTokens(idUser: string, line: number){
    return this.getQuery(`get-routed-tokens/${idUser}/${line}`).pipe(map((data: any) => data))
  }

  getBackupList(idUser: string, line: number){
    return this.getQuery(`get-backups-list/${idUser}/${line}`).pipe(map((data: any) => data))
  }

  getOrderPriorities(idUser: string, line: number){
    return this.getQuery(`get-order-priorities/${idUser}/${line}`).pipe(map((data: any) => data))
  }

  getCountriesList(idUser: string, line: number){
    return this.getQuery(`get-countries-list/${idUser}/${line}`).pipe(map((data: any) => data))
  }

  getZipCodes(idUser: string, line: number){
    return this.getQuery(`get-zip-codes/${idUser}/${line}`).pipe(map((data: any) => data))
  }

  getStatesList(idUser: string, line: number){
    return this.getQuery(`get-states-list/${idUser}/${line}`).pipe(map((data: any) => data))
  }

  getCitiesList(idUser: string, line: number){
    return this.getQuery(`get-cities-list/${idUser}/${line}`).pipe(map((data: any) => data))
  }

  getTownsList(idUser: string, line: number){
    return this.getQuery(`get-towns-list/${idUser}/${line}`).pipe(map((data: any) => data))
  }
  
  getCoinsList (idUser: string, line: number) : Observable<CoinsListResponse>{
    return this.getQuery(`get-coins-list/${idUser}/${line}`).pipe(map((data: any) => data))
  }

  getSettingsList(idUser: string, line: number){
    return this.getQuery(`get-settings-list/${idUser}/${line}`).pipe(map((data: any) => data))
  }

  getTaxRegimeList(idUser: string, line: number){
    return this.getQuery(`get-tax-regime-list/${idUser}/${line}`).pipe(map((data: any) => data))
  }

  getAddressByZipCode(zipCode: string, idUser: string, line: number){
    return this.getQuery(`get-address-by-zip-code/${zipCode}/${idUser}/${line}`).pipe(map((data: any) => data))
  }

  getStatesByCountryCode(countryCode: string, idUser: string, line: number){
    return this.getQuery(`get-states-by-country-code/${countryCode}/${idUser}/${line}`).pipe(map((data: any) => data))
  }

  getCitiesByStateCode(countryCode: string, idUser: string, line: number){
    return this.getQuery(`get-cities-by-state-code/${countryCode}/${idUser}/${line}`).pipe(map((data: any) => data))
  }

  getTowns(countryCode: string, cityCode: string, idUser: string, line: number){
    return this.getQuery(`get-towns/${countryCode}/${cityCode}/${idUser}/${line}`).pipe(map((data: any) => data))
  }

  getCitySettings(stateCode: string, cityCode: string, townCode: string, idUser: string, line: number){
    return this.getQuery(`get-city-settings/${stateCode}/${cityCode}/${townCode}/${idUser}/${line}`).pipe(map((data: any) => data))
  }

  getTaxInformation(line: number){
    //return this.getQueryTemp(`get-tax-information/${line}`).pipe(map((data: any) => data));
    return this.getQuery(`get-tax-information/${line}`).pipe(map((data: any) => data));
  }

  getMagnitudes(idUser: string, line: number){
    return this.getQuery(`counters-and-triggers/get-magnitudes/${idUser}/${line}`).pipe(map((data: any) => data))
  }

  getUnitsByMagnitude(idMagnitude: string, idUser: string, line: number){
    return this.getQuery(`counters-and-triggers/get-units-by-magnitude/${idMagnitude}/${idUser}/${line}`).pipe(map((data: any) => data))
  }

  getMagnitude(idMagnitude: string, idUser: string, line: number){
    return this.getQuery(`counters-and-triggers/get-magnitude/${idMagnitude}/${idUser}/${line}`).pipe(map((data: any) => data))
  }

  getUnit(idUnit: string, idMagnitude: string, idUser: string, line: number){
    return this.getQuery(`counters-and-triggers/get-unit/${idUnit}/${idMagnitude}/${idUser}/${line}`).pipe(map((data: any) => data))
  }

  getPRTGReport(idReport: string, idUser: string, line: number){
    return this.getQuery(`PRTG/get-report/${idReport}/${idUser}/${line}`).pipe(map((data: any) => data))
  }

  getPRTGLastReports(idUser: string, line: number){
    return this.getQuery(`PRTG/get-last-reports/${idUser}/${line}`).pipe(map((data: any) => data))
  }

  deleteTempFile(body: any){
    return this.postQuery("delete-file", body).pipe(map((data: any) => data));
  }

  setTwoStepsAuth(body: any){
    return this.postQuery("set-two-steps-auth", body).pipe(map((data: any) => data));
  }

  savePasswordFormat(body: any){
    return this.postQuery("save-password-format", body).pipe(map((data: any) => data));
  }

  saveSessionsDuration(body: any){
    return this.postQuery("save-sessions-duration", body).pipe(map((data: any) => data));
  }

  saveActiveSessionsNumber(body: any){
    return this.postQuery("save-active-sessions-number", body).pipe(map((data: any) => data));
  }

  activateMaintenanceMode(body: any){
    return this.postQuery("activate-maintenance-mode", body).pipe(map((data: any) => data))
  }

  deactivateMaintenanceMode(body: any){
    return this.postQuery("deactivate-maintenance-mode", body).pipe(map((data: any) => data))
  }

  registerEmailServer(body: any){
    return this.postQuery("register-email-server", body).pipe(map((data: any) => data))
  }

  updateEmailServer(body: any){
    return this.postQuery("update-email-server", body).pipe(map((data: any) => data))
  }

  deleteEmailServer(body: any){
    return this.postQuery("delete-email-server", body).pipe(map((data: any) => data))
  }

  deactivateEmailServer(body: any){
    return this.postQuery("deactivate-email-server", body).pipe(map((data: any) => data))
  }

  registerEmailTemplate(body: any){
    return this.postQuery("register-email-template", body).pipe(map((data: any) => data))
  }

  updateEmailTemplate(body: any){
    return this.postQuery("update-email-template", body).pipe(map((data: any) => data))
  }

  deleteEmailTemplate(body: any){
    return this.postQuery("delete-email-template", body).pipe(map((data: any) => data))
  }

  saveCatalogue(body: any){
    return this.postQuery("save-catalogue", body).pipe(map((data: any) => data))
  }

  saveLoginImage(body: any){
    return this.postQuery("save-login-image", body).pipe(map((data: any) => data))
  }

  updateModuleIcons(body: any){
    return this.postQuery("update-module-icons", body).pipe(map((data: any) => data))
  }

  updateSubmoduleIcons(body: any){
    return this.postQuery("update-submodule-icons", body).pipe(map((data: any) => data))
  }

  updateFunctionIcons(body: any){
    return this.postQuery("update-function-icons", body).pipe(map((data: any) => data))
  }

  registerSCADA(body: any){
    return this.postQuery("register-scada", body).pipe(map((data: any) => data))
  }

  updateSCADAPassword(body: any){
    return this.postQuery("update-scada-password", body).pipe(map((data: any) => data))
  }

  updateSCADA(body: any){
    return this.postQuery("update-scada", body).pipe(map((data: any) => data))
  }

  deleteSCADA(body: any){
    return this.postQuery("delete-scada", body).pipe(map((data: any) => data))
  }

  generateSCADAtoken(body: any){
    return this.postQuery("generate-scada-token", body).pipe(map((data: any) => data))
  }

  revokeToken(body: any){
    return this.postQuery("revoke-token", body).pipe(map((data: any) => data))
  }

  registerWebService(body: any){
    return this.postQuery("register-web-service", body).pipe(map((data: any) => data))
  }

  changeTokenAccess(body: any){
    return this.postQuery("change-token-access", body).pipe(map((data: any) => data))
  }

  changeWebService(body: any){
    return this.postQuery("change-web-service", body).pipe(map((data: any) => data))
  }

  updateWebService(body: any){
    return this.postQuery("update-web-service", body).pipe(map((data: any) => data))
  }

  changeWSstatus(body: any){
    return this.postQuery("change-ws-status", body).pipe(map((data: any) => data))
  }

  updateOrderPriorities(body: any){
    return this.postQuery("update-order-priorities", body).pipe(map((data: any) => data))
  }

  saveTaxInformation(body: FormTaxInformation){
    //return this.postQueryTemp("update-tax-information", body).pipe(map((data: any) => data));
    return this.postQuery("update-tax-information", body).pipe(map((data: any) => data));
  }
  
  saveCatalogueConfig(body: any){
    return this.postQuery("save-catalogue-config", body).pipe(map((data: any) => data))
  }

  registerMagnitude(body: any){
    return this.postQuery("counters-and-triggers/register-magnitude", body).pipe(map((data: any) => data))
  }

  updateMagnitude(body: any){
    return this.postQuery("counters-and-triggers/update-magnitude", body).pipe(map((data: any) => data))
  }

  deleteMagnitude(body: any){
    return this.postQuery("counters-and-triggers/delete-magnitude", body).pipe(map((data: any) => data))
  }
  
  validateCatalog(body: RequestFormValidateCatalog):Observable<ResponseDataGetValidateCatalog> {
    return this.postQuery("validate-catalog", body).pipe(map((data: any) => data))
  }
  
  getCatalogCoins(user: string, line: number):Observable<CoinsListResponse> {
    return this.getQuery(`get-catalog-coins/${user}/${line}`).pipe(map((data: any) => data))
  }

  saveStationPlane(body: any){
    return this.postQuery("save-station-plane", body).pipe(map((data: any) => data))
  }
  
  getStationPlanes(idStation: string, idUser: string, line: number) {
    return this.getQuery(`get-station-planes/${idStation}/${idUser}/${line}`).pipe(map((data: any) => data))
  }

  getQuery(query: string, tk?: string){
    const URL = `${apiTemp}${query}`;
    let token = "";
    if(tk == undefined || tk == null || tk == ""){
      token = `Bearer ${localStorage.getItem('token')!}`;
    }else{
      token = `Bearer ${tk!}`;
    }
    return this._http.get(URL, {
      headers: new HttpHeaders({
        Authorization: token,
      })
    })
  }

  postQuery(query: string, body: any){
    const JWT = `Bearer ${localStorage.getItem('token')}`;
    const URL = `${apiTemp}${query}`;
    return this._http.post(URL, body, {
      headers: new HttpHeaders({
        Authorization: JWT!
      })
    });
  }
}
