import { defineStore } from "pinia";
import { http, newEventSource } from "@/serverCom";
import { useNewsletterStore } from "./newsletter";
import type { AxiosResponse } from "axios";
import type { EventSourcePolyfill } from "event-source-polyfill";

export const useNewsletterPrintoutStore = defineStore("newsletterPrintout", {
  state: () => {
    return {
      printout: [] as Array<string>,
      loading: "loading" as "loading" | "fetched" | "failed",
      printing: undefined as undefined | "loading" | "success" | "failed",
      sending: undefined as undefined | "loading" | "success" | "failed",
      sendingPreview: undefined as undefined | "loading" | "success" | "failed",
      pdfProgessSource: undefined as undefined | EventSourcePolyfill,
      mailProgessSource: undefined as undefined | EventSourcePolyfill,
      pdfSourceMessages: [] as Array<Object>,
      mailSourceMessages: [] as Array<Object>,
    };
  },
  actions: {
    fetchNewsletterPrintout() {
      const newsletterId = useNewsletterStore().activeNewsletter;
      this.loading = "loading";
      http
        .get(`/admin/newsletter/${newsletterId}/printouts`)
        .then((result) => {
          this.printout = result.data;
          this.loading = "fetched";
        })
        .catch((err) => {
          this.loading = "failed";
        });
    },
    fetchNewsletterPrintoutById(printout: string): Promise<AxiosResponse<any, any>> {
      const newsletterId = useNewsletterStore().activeNewsletter;
      return http.get(`/admin/newsletter/${newsletterId}/printout/${printout}`, {
        responseType: "blob",
      });
    },
    fetchNewsletterPrintoutPreview(): Promise<AxiosResponse<any, any>> {
      const newsletterId = useNewsletterStore().activeNewsletter;
      return http.get(`/admin/newsletter/${newsletterId}/printoutpreview`, {
        responseType: "blob",
      });
    },
    createNewsletterMailPreview() {
      this.sendingPreview = "loading";
      const newsletterId = useNewsletterStore().activeNewsletter;
      if (newsletterId == null) return;
      return http
        .post(`/admin/newsletter/${newsletterId}/mailpreview`)
        .then((res) => {
          this.sendingPreview = "success";
        })
        .catch((err) => {
          this.sendingPreview = "failed";
        })
        .finally(() => {
          setTimeout(() => {
            this.sendingPreview = undefined;
          }, 1500);
        });
    },
    createNewsletterPrintout() {
      this.printing = "loading";
      const newsletterId = useNewsletterStore().activeNewsletter;
      if (newsletterId == null) return;
      return http
        .post(`/admin/newsletter/${newsletterId}/printout`)
        .then((res) => {
          this.fetchNewsletterPrintout();
          this.printing = "success";
        })
        .catch((err) => {
          this.printing = "failed";
        })
        .finally(() => {
          setTimeout(() => {
            this.printing = undefined;
          }, 1500);
        });
    },
    createNewsletterSend() {
      this.sending = "loading";
      const newsletterId = useNewsletterStore().activeNewsletter;
      if (newsletterId == null) return;
      return http
        .post(`/admin/newsletter/${newsletterId}/send`)
        .then((res) => {
          this.sending = "success";
        })
        .catch((err) => {
          this.sending = "failed";
        })
        .finally(() => {
          setTimeout(() => {
            this.sending = undefined;
          }, 1500);
        });
    },
    subscribePdfPrintingProgress() {
      // const newsletterId = useNewsletterStore().activeNewsletter;
      // if (this.pdfProgessSource != undefined) return;
      // this.pdfProgessSource = newEventSource(`/admin/newsletter/${newsletterId}/printoutprogress`);
      // this.pdfProgessSource.onmessage = (event) => {
      //   console.log("pdf", event);
      // };
    },
    subscribeMailSendingProgress() {
      // const newsletterId = useNewsletterStore().activeNewsletter;
      // if (this.mailProgessSource != undefined) return;
      // this.mailProgessSource = newEventSource(`/admin/newsletter/${newsletterId}/sendprogress`);
      // this.mailProgessSource.onmessage = (event) => {
      //   console.log("mail", event);
      // };
    },
    unsubscribePdfPrintingProgress() {
      this.pdfProgessSource?.close();
      this.pdfProgessSource = undefined;
    },
    unsubscribeMailSendingProgress() {
      this.mailProgessSource?.close();
      this.mailProgessSource = undefined;
    },
  },
});
