import axios from 'axios';
import FileSaver from 'file-saver';
import get from 'lodash/get';

class MZ {
  private serviceUrl: string;

  constructor() {
    this.serviceUrl = '/service/mz-';
  }

  getHistoryById(id: string) {
    const requestUrl = `/mz/history/request/${id}`;
    return new Promise((resolve, reject) => {
      try {
        axios
          .get(requestUrl, {
            headers: {
              Accept: '*/*',
            },
          })
          .then((response) => {
            const data =
              get(
                {
                  response,
                },
                'response.data'
              ) || '';
            resolve(data);
          })
          .catch((e) => {
            reject(e);
          });
      } catch (e) {
        reject(e);
      }
    });
  }

  getCurrentXml(type: string, id: string) {
    const requestUrl = `/mz/history/request/${id}/${type}`;
    return new Promise((resolve, reject) => {
      try {
        axios
          .get(requestUrl, {
            headers: {
              Accept: '*/*',
            },
          })
          .then((response) => {
            const data =
              get(
                {
                  response,
                },
                'response.data'
              ) || '';
            const blob = new Blob([data], {
              type: 'application/xml',
            });
            FileSaver.saveAs(blob, `${type}.xml`);
            resolve(data);
          })
          .catch((e) => {
            reject(e);
          });
      } catch (e) {
        reject(e);
      }
    });
  }

  getDiscoveryByClass(className: string) {
    const requestUrl = `${this.serviceUrl}discovery/service?class=${className}`;
    return new Promise((resolve, reject) => {
      try {
        axios
          .get(requestUrl, {
            headers: {
              Accept: '*/*',
            },
          })
          .then((response) => {
            const data =
              get(
                {
                  response,
                },
                'response.data'
              ) || '';
            resolve(data);
          })
          .catch((e) => {
            reject(e);
          });
      } catch (e) {
        reject(e);
      }
    });
  }

  getDiscoveryServiceById(id: string) {
    const requestUrl = `${this.serviceUrl}discovery/service/${id}`;
    return new Promise((resolve, reject) => {
      try {
        axios
          .get(requestUrl)
          .then((response) => {
            const data =
              get(
                {
                  response,
                },
                'response.data'
              ) || '';
            resolve(data);
          })
          .catch((e) => {
            reject(e);
          });
      } catch (e) {
        reject(e);
      }
    });
  }

  postGatewayResponse(data: any) {
    const requestUrl = `/mz/gateway/response/`;
    return new Promise((resolve, reject) => {
      try {
        axios
          .post(requestUrl, data, {
            headers: {
              Accept: '*/*',
            },
          })
          .then((response) => {
            resolve(response);
          })
          .catch((e) => {
            reject(e);
          });
      } catch (e) {
        reject(e);
      }
    });
  }

  postGatewayRequest(data: any) {
    const requestUrl = `/mz/gateway/request/`;
    return new Promise((resolve, reject) => {
      try {
        axios
          .post(requestUrl, data, {
            headers: {
              Accept: '*/*',
            },
          })
          .then((response) => {
            resolve(response);
          })
          .catch((e) => {
            reject(e);
          });
      } catch (e) {
        reject(e);
      }
    });
  }
}

const mzService = new MZ();

export default mzService;
