import { useQuery } from "@apollo/client";
import { Card, Result } from "antd";
import React, { useEffect } from "react";
import { connect } from "react-redux";
import { createStructuredSelector } from "reselect";
import { QUERY_JOB_BY_PK } from "../../../graphql/jobs.queries";
import { setSelectedJobTargetPc } from "../../../redux/application/application.actions";
import { selectSelectedJobId } from "../../../redux/application/application.selectors";
import ErrorResultAtom from "../../atoms/error-result/error-result.atom";
import JobsPartsGraphAtom from "../../atoms/jobs-parts-graph/jobs-parts-graph.atom";
import JobsDetailDescriptionMolecule from "../../molecules/jobs-detail-description/jobs-detail-description.molecule";
import JobsLinesTableMolecule from "../../molecules/jobs-lines-table/jobs-lines-table.molecule";
import JobsTargetsStatsMolecule from "../../molecules/jobs-targets-stats/jobs-targets-stats.molecule";
import "./jobs-detail.organism.styles.scss";

const mapStateToProps = createStructuredSelector({
  //currentUser: selectCurrentUser
  selectedJobId: selectSelectedJobId,
});
const mapDispatchToProps = (dispatch) => ({
  //setUserLanguage: language => dispatch(setUserLanguage(language))
  setSelectedJobTargetPc: (job) => dispatch(setSelectedJobTargetPc(job)),
});

export function JobsDetailOrganism({ selectedJobId, setSelectedJobTargetPc }) {
  const { loading, error, data } = useQuery(QUERY_JOB_BY_PK, {
    variables: { jobId: selectedJobId },
    skip: !selectedJobId,
  });

  useEffect(() => {
    if (data && data.jobs_by_pk)
      setSelectedJobTargetPc({
        group: data.jobs_by_pk && data.jobs_by_pk.group,
        v_age: data.jobs_by_pk && data.jobs_by_pk.v_age,
        close_date: data.jobs_by_pk && data.jobs_by_pk.close_date,
      });
  }, [data, setSelectedJobTargetPc]);

  if (!selectedJobId)
    return (
      <div
        style={{
          display: "flex",
          height: "100%",
          justifyContent: "center",
          alignItems: "center",
        }}
      >
        <Result title="No job selected." />
      </div>
    );
  if (error)
    return (
      <ErrorResultAtom
        title="Error fetching Job details.."
        errorMessage={JSON.stringify(error)}
      />
    );

  return (
    <div className="jobs-detail-container">
      <Card>
        <JobsDetailDescriptionMolecule
          loading={loading}
          job={data ? data.jobs_by_pk : null}
        />
      </Card>
      <Card>
        <JobsLinesTableMolecule
          loading={loading}
          job={data ? data.jobs_by_pk : {}}
        />
      </Card>
      <Card>
        <JobsTargetsStatsMolecule
          loading={loading}
          job={data ? data.jobs_by_pk : null}
        />
        <div
          style={{
            display: "flex",
            justifyContent: "space-evenly",
            minHeight: "20rem",
            width: "100%",
          }}
        >
          <JobsPartsGraphAtom
            job={data ? data.jobs_by_pk : null}
            loading={loading}
            price="db_price"
          />
          <JobsPartsGraphAtom
            job={data ? data.jobs_by_pk : null}
            loading={loading}
          />
        </div>
      </Card>
    </div>
  );
}
export default connect(mapStateToProps, mapDispatchToProps)(JobsDetailOrganism);
