import { useQuery } from "@apollo/client";
import { Card, Col, Result, Row, Space, Typography } from "antd";
import React, { useEffect } from "react";
import { useTranslation } from "react-i18next";
import { connect } from "react-redux";
import { useParams } from "react-router-dom";
import AlertComponent from "../../components/alert/alert.component";
import JobCalculateTotals from "../../components/job-calculate-totals/job-calculate-totals.component";
import ScoreboardAddButton from "../../components/job-scoreboard-add-button/job-scoreboard-add-button.component";
import JobsAdminStatus from "../../components/jobs-admin-change-status/jobs-admin-change.status.component";
import JobsAdminClass from "../../components/jobs-admin-class/jobs-admin-class.component";
import JobsAdminDatesChange from "../../components/jobs-admin-dates/jobs-admin-dates.component";
import JobsAdminDeleteIntake from "../../components/jobs-admin-delete-intake/jobs-admin-delete-intake.component";
import JobsAdminMarkReexport from "../../components/jobs-admin-mark-reexport/jobs-admin-mark-reexport.component";
import JobAdminOwnerReassociate from "../../components/jobs-admin-owner-reassociate/jobs-admin-owner-reassociate.component";
import JobsAdminUnvoid from "../../components/jobs-admin-unvoid/jobs-admin-unvoid.component";
import JobAdminVehicleReassociate from "../../components/jobs-admin-vehicle-reassociate/jobs-admin-vehicle-reassociate.component";
import JobsAdminRemoveAR from "../../components/jobs-admin-remove-ar/jobs-admin-remove-ar.component";
import LoadingSpinner from "../../components/loading-spinner/loading-spinner.component";
import NotFound from "../../components/not-found/not-found.component";
import RbacWrapper from "../../components/rbac-wrapper/rbac-wrapper.component";
import { GET_JOB_BY_PK } from "../../graphql/jobs.queries";
import { setBreadcrumbs, setSelectedHeader } from "../../redux/application/application.actions";
import InstanceRenderManager from "../../utils/instanceRenderMgr";

const mapDispatchToProps = (dispatch) => ({
  setBreadcrumbs: (breadcrumbs) => dispatch(setBreadcrumbs(breadcrumbs)),
  setSelectedHeader: (key) => dispatch(setSelectedHeader(key))
});

const colSpan = {
  sm: { span: 24 },
  md: { span: 12 },
  lg: { span: 8 },
  xl: { span: 6 }
};

const cardStyle = {
  height: "100%"
};

export function JobsCloseContainer({ setBreadcrumbs, setSelectedHeader }) {
  const { jobId } = useParams();
  const { loading, error, data } = useQuery(GET_JOB_BY_PK, {
    variables: { id: jobId },
    fetchPolicy: "network-only",
    nextFetchPolicy: "network-only"
  });
  const { t } = useTranslation();
  useEffect(() => {
    setSelectedHeader("activejobs");
    document.title = t("titles.jobs-admin", {
      app: InstanceRenderManager({
        imex: "$t(titles.imexonline)",
        rome: "$t(titles.romeonline)",
        promanager: "$t(titles.promanager)"
      }),
      ro_number: data ? data.jobs_by_pk && data.jobs_by_pk.ro_number : null
    });

    setBreadcrumbs([
      {
        link: `/manage/jobs/`,
        label: t("titles.bc.jobs")
      },
      {
        link: `/manage/jobs/${jobId}/`,
        label: t("titles.bc.jobs-detail", {
          number: data ? data.jobs_by_pk && data.jobs_by_pk.ro_number : null
        })
      },
      {
        link: `/manage/jobs/${jobId}/admin`,
        label: t("titles.bc.jobs-admin")
      }
    ]);
  }, [setBreadcrumbs, t, jobId, data, setSelectedHeader]);

  if (loading) return <LoadingSpinner />;
  if (error) return <AlertComponent message={error.message} type="error" />;
  if (!!!data.jobs_by_pk) return <NotFound />;
  if (!data.jobs_by_pk.job_totals)
    return <Result title={t("jobs.errors.nofinancial")} extra={<JobCalculateTotals job={data.jobs_by_pk} />} />;

  return (
    <RbacWrapper action="jobs:admin">
      <Typography.Title level={4} style={{ color: "tomato" }}>
        {t("jobs.labels.adminwarning")}
      </Typography.Title>
      <Row gutter={[16, 16]}>
        <Col {...colSpan}>
          <Card style={cardStyle}>
            <Space wrap>
              <ScoreboardAddButton
                job={data ? data.jobs_by_pk : {}}
                disabled={(data && data.jobs_by_pk.voided) || (data.jobs_by_pk && !data.jobs_by_pk.converted)}
              />
              <JobsAdminDeleteIntake job={data ? data.jobs_by_pk : {}} />
              <JobsAdminMarkReexport job={data ? data.jobs_by_pk : {}} />
              <JobsAdminUnvoid job={data ? data.jobs_by_pk : {}} />
              <JobsAdminStatus job={data ? data.jobs_by_pk : {}} />
              <JobsAdminRemoveAR job={data ? data.jobs_by_pk : {}} />
            </Space>
          </Card>
        </Col>
        <Col {...colSpan}>
          <Card style={cardStyle}>
            <JobsAdminClass job={data ? data.jobs_by_pk : {}} />
          </Card>
        </Col>
        <Col {...colSpan}>
          <Card style={cardStyle}>
            <JobAdminOwnerReassociate job={data ? data.jobs_by_pk : {}} />
          </Card>
        </Col>
        <Col {...colSpan}>
          <Card style={cardStyle}>
            <JobAdminVehicleReassociate job={data ? data.jobs_by_pk : {}} />
          </Card>
        </Col>

        <Col span={24}>
          <Card style={cardStyle}>
            <JobsAdminDatesChange job={data ? data.jobs_by_pk : {}} />
          </Card>
        </Col>
      </Row>
    </RbacWrapper>
  );
}

export default connect(null, mapDispatchToProps)(JobsCloseContainer);
