import { useMutation } from "@apollo/client";
import { Button, notification } from "antd";
import axios from "axios";
import React, { useState } from "react";
import { useTranslation } from "react-i18next";
import { connect } from "react-redux";
import { createStructuredSelector } from "reselect";
import { auth, logImEXEvent } from "../../firebase/firebase.utils";
import { INSERT_EXPORT_LOG } from "../../graphql/accounting.queries";
import { UPDATE_PAYMENTS } from "../../graphql/payments.queries";

import { selectBodyshop, selectCurrentUser } from "../../redux/user/user.selectors";
import client from "../../utils/GraphQLClient";

const mapStateToProps = createStructuredSelector({
  bodyshop: selectBodyshop,
  currentUser: selectCurrentUser
});

function updatePaymentCache(items) {
  client.cache.modify({
    id: "ROOT_QUERY",
    fields: {
      payments(existingJobs = []) {
        return existingJobs.filter((paymentRef) => paymentRef.__ref.includes(items) === false);
      }
    }
  });
}

export function PaymentExportButton({
  bodyshop,
  currentUser,
  paymentId,
  disabled,
  loadingCallback,
  setSelectedPayments,
  refetch
}) {
  const { t } = useTranslation();
  const [updatePayment] = useMutation(UPDATE_PAYMENTS);
  const [loading, setLoading] = useState(false);
  const [insertExportLog] = useMutation(INSERT_EXPORT_LOG);

  const handleQbxml = async () => {
    logImEXEvent("accounting_payment_export");
    setLoading(true);
    //Check if it's a QBO Setup.
    let PartnerResponse;
    if (bodyshop.accountingconfig && bodyshop.accountingconfig.qbo) {
      PartnerResponse = await axios.post(`/qbo/payments`, {
        payments: [paymentId],
        elgen: true
      });
    } else {
      //Default is QBD

      if (loadingCallback) loadingCallback(true);

      let QbXmlResponse;
      try {
        QbXmlResponse = await axios.post(
          "/accounting/qbxml/payments",
          { payments: [paymentId] },
          {
            headers: {
              Authorization: `Bearer ${await auth.currentUser.getIdToken()}`
            }
          }
        );
        console.log("handle -> XML", QbXmlResponse);
      } catch (error) {
        console.log("Error getting QBXML from Server.", error);
        notification["error"]({
          message: t("payments.errors.exporting", {
            error: "Unable to retrieve QBXML. " + JSON.stringify(error.message)
          })
        });
        if (loadingCallback) loadingCallback(false);
        setLoading(false);
        return;
      }

      try {
        PartnerResponse = await axios.post("http://localhost:1337/qb/", QbXmlResponse.data);
      } catch (error) {
        console.log("Error connecting to quickbooks or partner.", error);
        notification["error"]({
          message: t("payments.errors.exporting-partner")
        });
        if (loadingCallback) loadingCallback(false);
        setLoading(false);
        return;
      }
    }

    console.log("handleQbxml -> PartnerResponse", PartnerResponse);
    const failedTransactions = PartnerResponse.data.filter((r) => !r.success);
    const successfulTransactions = PartnerResponse.data.filter((r) => r.success);
    if (failedTransactions.length > 0) {
      //Uh oh. At least one was no good.
      failedTransactions.map((ft) =>
        notification["error"]({
          message: t("payments.errors.exporting", {
            error: ft.errorMessage || ""
          })
        })
      );
      if (!(bodyshop.accountingconfig && bodyshop.accountingconfig.qbo)) {
        //QBO Logs are handled server side.
        await insertExportLog({
          variables: {
            logs: [
              {
                bodyshopid: bodyshop.id,
                paymentid: paymentId,
                successful: false,
                message: JSON.stringify(failedTransactions.map((ft) => ft.errorMessage)),
                useremail: currentUser.email
              }
            ]
          }
        });
      }
    } else {
      if (!(bodyshop.accountingconfig && bodyshop.accountingconfig.qbo)) {
        //QBO Logs are handled server side.
        await insertExportLog({
          variables: {
            logs: [
              {
                bodyshopid: bodyshop.id,
                paymentid: paymentId,
                successful: true,
                useremail: currentUser.email
              }
            ]
          }
        });

        const paymentUpdateResponse = await updatePayment({
          variables: {
            paymentIdList: successfulTransactions.map(
              (st) => st[bodyshop.accountingconfig && bodyshop.accountingconfig.qbo ? "paymentid" : "id"]
            ),
            payment: {
              exportedat: new Date()
            }
          }
        });
        if (!paymentUpdateResponse.errors) {
          notification.open({
            type: "success",
            key: "paymentsuccessexport",
            message: t("payments.successes.exported")
          });
          updatePaymentCache(paymentUpdateResponse.data.update_payments.returning.map((payment) => payment.id));
        } else {
          notification["error"]({
            message: t("payments.errors.exporting", {
              error: JSON.stringify(paymentUpdateResponse.error)
            })
          });
        }
      }

      if (setSelectedPayments) {
        setSelectedPayments((selectedBills) => {
          return selectedBills.filter((i) => i !== paymentId);
        });
      }
    }
    if (bodyshop.accountingconfig && bodyshop.accountingconfig.qbo && successfulTransactions.length > 0) {
      notification.open({
        type: "success",
        key: "paymentsuccessexport",
        message: t("payments.successes.exported")
      });
      updatePaymentCache([
        ...new Set(
          successfulTransactions.map(
            (st) => st[bodyshop.accountingconfig && bodyshop.accountingconfig.qbo ? "paymentid" : "id"]
          )
        )
      ]);
    }
    if (loadingCallback) loadingCallback(false);
    setLoading(false);
  };

  return (
    <Button onClick={handleQbxml} loading={loading} disabled={disabled} type="primary">
      {t("jobs.actions.export")}
    </Button>
  );
}

export default connect(mapStateToProps, null)(PaymentExportButton);
