/* eslint-disable quotes */
import { HttpClient } from "@angular/common/http";
import { Inject, Injectable } from "@angular/core";
import { Observable } from "rxjs";
import { DwSystemConfigService } from "@webdpt/framework/config";
import { DW_AUTH_TOKEN } from "@webdpt/framework/auth";

@Injectable({ providedIn: "root" })
export class CommonService {
  atdmUrl: string;
  bpmUrl: string;
  atmcUrl: any;
  frcUrl: any;
  eocUrl: any;
  iamUrl: any;
  apiUrl: any;
  semcUrl: any;
  themeMapUrl: any;
  pageData: any;
  // 记录机制的content参数
  content: any;
  uibotUrl: string;
  isDisabled: boolean;
  caLqsUrl: string;
  constructor(
    @Inject(DW_AUTH_TOKEN) protected authToken: any,
    private http: HttpClient,
    private configService: DwSystemConfigService
  ) {
    this.configService.get("atdmUrl").subscribe((url: string): void => {
      this.atdmUrl = url;
    });
    this.configService.get("bpmUrl").subscribe((url): void => {
      this.bpmUrl = url;
    });
    this.configService.get("atmcUrl").subscribe((url): void => {
      this.atmcUrl = url;
    });
    this.configService.get("frcUrl").subscribe((url): void => {
      this.frcUrl = url;
    });
    this.configService.get("eocUrl").subscribe((url): void => {
      this.eocUrl = url;
    });
    this.configService.get("uibotUrl").subscribe((url: string): void => {
      this.uibotUrl = url;
    });
    this.configService.get("caLqsUrl").subscribe((url: string): void => {
      this.caLqsUrl = url;
      //   console.log("caLqsUrl", this.caLqsUrl);
    });
    this.configService.get("iamUrl").subscribe((url: string): void => {
      this.iamUrl = url;
      console.log("iamUrl", this.iamUrl);
    });
    this.configService.get("apiUrl").subscribe((url: string): void => {
      this.apiUrl = url;
      console.log("api", this.apiUrl);
    });
    this.configService.get("semcUrl").subscribe((url: string): void => {
      this.semcUrl = url;
      console.log("semcUrl", this.semcUrl);
    });
  }

  getHeader(): any {
    if (this.authToken?.token) {
      return {
        "digi-middleware-auth-user": this.authToken.token,
        token: this.authToken.token,
      };
    }
    return {};
  }
  /**
   *
   * @param url
   * @param data
   * @param headers
   * @returns
   */
  postWithHeaders(url, data, headers) {
    headers = headers || {};
    headers["Content-Type"] = "application/json"; // 设置默认的Content-Type
    const options = {
      method: "POST",
      headers: headers,
      body: JSON.stringify(data),
    };

    return fetch(url, options)
      .then((response) => {
        if (response.ok) {
          return response.json();
        }
        throw new Error("请求出错！");
      })
      .catch((error) => {
        console.error("请求出错！", error);
        throw error;
      });
  }
  /**
   *
   * @param url
   * @param headers
   * @returns
   */
  getRequestWithHeaders(url, headers) {
    return fetch(url, {
      method: "GET",
      headers: headers,
    })
      .then((response) => {
        if (!response.ok) {
          throw new Error("Network response was not ok " + response.statusText);
        }
        return response.json();
      })
      .catch((error) => {
        console.error(
          "There has been a problem with your fetch operation:",
          error
        );
      });
  }
}
