import { SUCCESS } from "../constants/index.js";

export const fetchData = ({ query, url }) => {
  console.log("query, url", query, url);
  return $.ajax({
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    type: "POST",
    url,
    data: JSON.stringify(query),
    success: function (data, textStatus, jqXHR) {
      if (textStatus === SUCCESS) {
        return data;
      }
      return [];
    },
    error: function (jqXHR, textStatus, errorThrown) {
      console.log(jqXHR.statusText);
    },
  });
};

export const addBookToFavourites = ({ bookid }) => {
  return $.ajax({
    type: "POST",
    url: `../api/books/${bookid}/favorite`,
    success: function (data, textStatus, jqXHR) {
      if (textStatus === SUCCESS) {
        return data;
      }
    },
    error: function (jqXHR, textStatus, errorThrown) {
      console.log(jqXHR.statusText);
    },
  });
};

export const removeBookToFavourites = ({ bookid }) => {
  return $.ajax({
    type: "DELETE",
    url: `../api/books/${bookid}/favorite`,
    success: function (data, textStatus, jqXHR) {
      if (textStatus === SUCCESS) {
        return data;
      }
    },
    error: function (jqXHR, textStatus, errorThrown) {
      console.log(jqXHR.statusText);
    },
  });
};

export const addAuthorToFavourites = ({ bookid }) => {
  return $.ajax({
    type: "POST",
    url: `../api/books/${bookid}/authors/favorite`,
    success: function (data, textStatus, jqXHR) {
      if (textStatus === SUCCESS) {
        return data;
      }
    },
    error: function (jqXHR, textStatus, errorThrown) {
      console.log(jqXHR.statusText);
    },
  });
};

export const readLater = ({ bookid }) => {
  return $.ajax({
    type: "POST",
    url: `../api/books/${bookid}/read`,
    success: function (data, textStatus, jqXHR) {
      if (textStatus === SUCCESS) {
        return data;
      }
    },
    error: function (jqXHR, textStatus, errorThrown) {
      console.log(jqXHR.statusText);
    },
  });
};

export const removeFromReadLater = ({ bookid }) => {
  return $.ajax({
    type: "DELETE",
    url: `../api/books/${bookid}/read`,
    success: function (data, textStatus, jqXHR) {
      if (textStatus === SUCCESS) {
        return data;
      }
    },
    error: function (jqXHR, textStatus, errorThrown) {
      console.log(jqXHR.statusText);
    },
  });
};

export const ignoreBook = ({ bookid }) => {
  return $.ajax({
    type: "POST",
    url: `../api/books/${bookid}/block`,
    success: function (data, textStatus, jqXHR) {
      if (textStatus === SUCCESS) {
        return data;
      }
    },
    error: function (jqXHR, textStatus, errorThrown) {
      console.log(jqXHR.statusText);
    },
  });
};

export const stopIgnoreBook = ({ bookid }) => {
  return $.ajax({
    type: "DELETE",
    url: `../api/books/${bookid}/block`,
    success: function (data, textStatus, jqXHR) {
      if (textStatus === SUCCESS) {
        return data;
      }
    },
    error: function (jqXHR, textStatus, errorThrown) {
      console.log(jqXHR.statusText);
    },
  });
};

export const ignoreAuthors = ({ bookid }) => {
  return $.ajax({
    type: "POST",
    url: `../api/books/${bookid}/author/block`,
    success: function (data, textStatus, jqXHR) {
      if (textStatus === SUCCESS) {
        return data;
      }
    },
    error: function (jqXHR, textStatus, errorThrown) {
      console.log(jqXHR.statusText);
    },
  });
};

export const downloadBook = ({ id, title, format }) => {
  $.ajax({
    type: "GET",
    url: `../api/books/download/${id}`,
    responseType: "blob",
    success: function (data, textStatus, jqXHR) {
      if (textStatus === SUCCESS) {
        const name = title.replace(" ", "_");
        const url = window.URL.createObjectURL(new Blob([data]));

        const link = document.createElement("a");

        link.href = url;

        link.setAttribute("download", `${name}.${format}`);

        document.body.appendChild(link);

        link.click();
      }
    },
    error: function (jqXHR, textStatus, errorThrown) {
      console.log(jqXHR.statusText);
    },
  });
};

export const searchByAuthor = ({ id }) => {
  return $.ajax({
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    type: "POST",
    url: `../api/author/${id}/books`,
    success: function (data, textStatus, jqXHR) {
      if (textStatus === SUCCESS) {
        return data;
      }
      return [];
    },
    error: function (jqXHR, textStatus, errorThrown) {
      console.log(jqXHR.statusText);
    },
  });
};

export const waitForElement = (selector) => {
  return new Promise((resolve) => {
    if (document.querySelector(selector)) {
      return resolve(document.querySelector(selector));
    }

    const observer = new MutationObserver((mutations) => {
      if (document.querySelector(selector)) {
        observer.disconnect();
        resolve(document.querySelector(selector));
      }
    });

    observer.observe(document.body, {
      childList: true,
      subtree: true,
    });
  });
};

export const changeTags = ({ bookid, tags }) => {
  return $.ajax({
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    type: "POST",
    url: "/api/tags",
    data: JSON.stringify({ bookid, names: tags }),
    success: function (data, textStatus, jqXHR) {
      if (textStatus === SUCCESS) {
        return data;
      }
    },
    error: function (jqXHR, textStatus, errorThrown) {
      console.log(jqXHR.statusText);
    },
  });

  // return [
  //   {id: "6611598e1468849d1b00570d", name: "шиза"}
  // ];
};

export const searchByTag = ({ id }) => {
  return $.ajax({
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    type: "POST",
    url: `../api/tags/${id}/books`,
    success: function (data, textStatus, jqXHR) {
      if (textStatus === SUCCESS) {
        return data;
      }
      return [];
    },
    error: function (jqXHR, textStatus, errorThrown) {
      console.log(jqXHR.statusText);
    },
  });
};
