import { useLazyQuery, useMutation } from "@apollo/client/react";
import { Button, Form, Popover, Space } from "antd";
import dayjs from "dayjs";
import { useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
import { connect } from "react-redux";
import { createStructuredSelector } from "reselect";
import { useNotification } from "../../contexts/Notifications/notificationContext.jsx";
import { GET_JOB_BY_PK_QUICK_INTAKE, JOB_PRODUCTION_TOGGLE } from "../../graphql/jobs.queries";
import { insertAuditTrail } from "../../redux/application/application.actions";
import { selectJobReadOnly } from "../../redux/application/application.selectors";
import { selectBodyshop } from "../../redux/user/user.selectors";
import AuditTrailMapping from "../../utils/AuditTrailMappings";
import { DateTimeFormatterFunction } from "../../utils/DateFormatter";
import FormDateTimePickerComponent from "../form-date-time-picker/form-date-time-picker.component";
import LoadingSpinner from "../loading-spinner/loading-spinner.component.jsx";
import { logImEXEvent } from "../../firebase/firebase.utils.js";

const mapStateToProps = createStructuredSelector({
  //currentUser: selectCurrentUser,
  bodyshop: selectBodyshop,
  jobRO: selectJobReadOnly
});
const mapDispatchToProps = (dispatch) => ({
  insertAuditTrail: ({ jobid, operation }) => dispatch(insertAuditTrail({ jobid, operation }))
});

export function JobsDetailHeaderActionsToggleProduction({
  bodyshop,
  job,
  jobRO,
  refetch,
  closeParentMenu,
  insertAuditTrail
}) {
  const [scenario, setScenario] = useState(false);
  const [loading, setLoading] = useState(false);
  const [popOverVisible, setPopOverVisible] = useState(false);
  const [mutationUpdateJob] = useMutation(JOB_PRODUCTION_TOGGLE);
  const { t } = useTranslation();
  const [form] = Form.useForm();
  const notification = useNotification();

  const [getJobDetails, { loading: jobDetailsLoading }] = useLazyQuery(GET_JOB_BY_PK_QUICK_INTAKE, {
    onCompleted: (data) => {
      if (data?.jobs_by_pk) {
        const totalHours =
          (data.jobs_by_pk.labhrs?.aggregate?.sum?.mod_lb_hrs || 0) +
          (data.jobs_by_pk.larhrs?.aggregate?.sum?.mod_lb_hrs || 0);
        form.setFieldsValue({
          actual_in: data.jobs_by_pk.actual_in ? data.jobs_by_pk.actual_in : dayjs(),
          scheduled_completion: data.jobs_by_pk.scheduled_completion
            ? data.jobs_by_pk.scheduled_completion
            : totalHours && bodyshop.ss_configuration.nobusinessdays
              ? dayjs().businessDaysAdd(totalHours / (bodyshop.target_touchtime || 1), "day")
              : dayjs().add(totalHours / (bodyshop.target_touchtime || 1), "day"),
          actual_completion: data.jobs_by_pk.actual_completion,
          scheduled_delivery: data.jobs_by_pk.scheduled_delivery,
          actual_delivery: data.jobs_by_pk.actual_delivery
        });
      }
    },
    fetchPolicy: "network-only"
  });

  useEffect(() => {
    //Figure out what scenario were in, populate accordingly
    if (job && bodyshop) {
      if (bodyshop.md_ro_statuses.pre_production_statuses.includes(job.status)) {
        setScenario("pre");
      } else if (bodyshop.md_ro_statuses.production_statuses.includes(job.status)) {
        setScenario("prod");
      } else {
        setScenario("post");
      }
    }
  }, [job, setScenario, bodyshop]);

  const handleConvert = async (values) => {
    setLoading(true);
    const res = await mutationUpdateJob({
      variables: {
        jobId: job.id,
        job: {
          ...values,
          status:
            scenario === "pre" ? bodyshop.md_ro_statuses.default_arrived : bodyshop.md_ro_statuses.default_delivered,
          inproduction: scenario === "pre" ? true : false
        }
      }
    });

    if (!res.errors) {
      notification.success({
        title: t("jobs.successes.converted")
      });

      insertAuditTrail({
        jobid: job.id,
        operation:
          scenario === "pre"
            ? AuditTrailMapping.jobintake(
                res.data.update_jobs.returning[0].status,
                DateTimeFormatterFunction(values.scheduled_completion)
              )
            : AuditTrailMapping.jobdelivery(
                res.data.update_jobs.returning[0].status,
                DateTimeFormatterFunction(values.actual_completion)
              )
      });

      logImEXEvent(scenario === "pre" ? "job_intake_quick" : "job-deliver-quick", {});

      setPopOverVisible(false);
      closeParentMenu();
      refetch();
    }
    setLoading(false);
  };

  const popMenu = (
    <div onClick={(e) => e.stopPropagation()}>
      {jobDetailsLoading ? (
        <LoadingSpinner />
      ) : (
        <Form layout="vertical" form={form} onFinish={handleConvert}>
          {scenario === "pre" && (
            <>
              <Form.Item
                name={["actual_in"]}
                label={t("jobs.fields.actual_in")}
                rules={[
                  {
                    required: true
                    //message: t("general.validation.required"),
                  }
                ]}
              >
                <FormDateTimePickerComponent disabled={jobRO} />
              </Form.Item>
              <Form.Item
                name={["scheduled_completion"]}
                label={t("jobs.fields.scheduled_completion")}
                rules={[
                  {
                    required: true
                    //message: t("general.validation.required"),
                  }
                ]}
              >
                <FormDateTimePickerComponent disabled={jobRO} />
              </Form.Item>
              <Form.Item name={["scheduled_delivery"]} label={t("jobs.fields.scheduled_delivery")}>
                <FormDateTimePickerComponent disabled={jobRO} />
              </Form.Item>
            </>
          )}
          {scenario === "prod" && (
            <>
              <Form.Item
                name={["actual_completion"]}
                label={t("jobs.fields.actual_completion")}
                rules={[
                  {
                    required: true
                    //message: t("general.validation.required"),
                  }
                ]}
              >
                <FormDateTimePickerComponent disabled={jobRO} />
              </Form.Item>

              <Form.Item
                name={["actual_delivery"]}
                label={t("jobs.fields.actual_delivery")}
                rules={[
                  {
                    required: bodyshop.deliverchecklist.actual_delivery
                      ? bodyshop.deliverchecklist.actual_delivery
                      : false
                    //message: t("general.validation.required"),
                  }
                ]}
              >
                <FormDateTimePickerComponent disabled={jobRO} />
              </Form.Item>
            </>
          )}

          <Space wrap>
            <Button type="primary" onClick={() => form.submit()} loading={loading}>
              {t("general.actions.save")}
            </Button>
          </Space>
        </Form>
      )}
    </div>
  );

  return (
    <Popover //open={open}
      content={popMenu}
      open={popOverVisible}
      onOpenChange={setPopOverVisible}
      onClick={(e) => {
        getJobDetails({ variables: { id: job.id } });
        e.stopPropagation();
      }}
      getPopupContainer={(trigger) => trigger.parentNode}
      trigger="click"
    >
      {scenario === "pre" && t("jobs.actions.intake_quick")}
      {scenario === "prod" && t("jobs.actions.deliver_quick")}
    </Popover>
  );
}

export default connect(mapStateToProps, mapDispatchToProps)(JobsDetailHeaderActionsToggleProduction);
