import { Table } from "antd";
import Dinero from "dinero.js";
import { useMemo } from "react";
import { useTranslation } from "react-i18next";

import { connect } from "react-redux";
import { createStructuredSelector } from "reselect";
import { selectBodyshop } from "../../redux/user/user.selectors";
import InstanceRenderManager from "../../utils/instanceRenderMgr";
import JobTotalsCashDiscount from "./jobs-totals.cash-discount-display.component";

const mapStateToProps = createStructuredSelector({
  //currentUser: selectCurrentUser
  bodyshop: selectBodyshop
});
const mapDispatchToProps = () => ({
  //setUserLanguage: language => dispatch(setUserLanguage(language))
});
export default connect(mapStateToProps, mapDispatchToProps)(JobTotalsTableTotals);

export function JobTotalsTableTotals({ bodyshop, job }) {
  const { t } = useTranslation();

  const data = useMemo(() => {
    return [
      ...(job.job_totals?.totals?.ttl_adjustment
        ? [
            {
              key: `Subtotal Adj.`,
              total: job.job_totals?.totals?.ttl_adjustment
            }
          ]
        : []),
      {
        key: t("jobs.labels.subtotal"),
        total: job.job_totals.totals.subtotal,
        bold: true
      },
      ...InstanceRenderManager({
        imex: [
          {
            key: t("jobs.labels.local_tax_amt"),
            total: job.job_totals.totals.local_tax
          },
          {
            key: t("jobs.labels.state_tax_amt"),
            total: job.job_totals.totals.state_tax
          },
          ...(bodyshop.region_config === "CA_BC"
            ? [
                {
                  key: t("jobs.fields.ca_bc_pvrt"),
                  total: job.job_totals.additional.pvrt
                }
              ]
            : []),
          {
            key: t("jobs.labels.federal_tax_amt"),
            total: job.job_totals.totals.federal_tax
          }
        ],
        rome: job.job_totals.totals.us_sales_tax_breakdown
          ? [
              {
                key: `${bodyshop.md_responsibility_centers.taxes.tax_ty1?.tax_type1 || "T1"} - ${[
                  job.cieca_pft.ty1_rate1,
                  job.cieca_pft.ty1_rate2,
                  job.cieca_pft.ty1_rate3,
                  job.cieca_pft.ty1_rate4,
                  job.cieca_pft.ty1_rate5
                ]
                  .filter((i) => i > 0)
                  .join(", ")}%`,
                total: job.job_totals.totals.us_sales_tax_breakdown.ty1Tax
              },
              {
                key: `${bodyshop.md_responsibility_centers.taxes.tax_ty2?.tax_type2 || "T2"} - ${[
                  job.cieca_pft.ty2_rate1,
                  job.cieca_pft.ty2_rate2,
                  job.cieca_pft.ty2_rate3,
                  job.cieca_pft.ty2_rate4,
                  job.cieca_pft.ty2_rate5
                ]
                  .filter((i) => i > 0)
                  .join(", ")}%`,
                total: job.job_totals.totals.us_sales_tax_breakdown.ty2Tax
              },
              {
                key: `${bodyshop.md_responsibility_centers.taxes.tax_ty3?.tax_type3 || "T3"} - ${[
                  job.cieca_pft.ty3_rate1,
                  job.cieca_pft.ty3_rate2,
                  job.cieca_pft.ty3_rate3,
                  job.cieca_pft.ty3_rate4,
                  job.cieca_pft.ty3_rate5
                ]
                  .filter((i) => i > 0)
                  .join(", ")}%`,
                total: job.job_totals.totals.us_sales_tax_breakdown.ty3Tax
              },
              {
                key: `${bodyshop.md_responsibility_centers.taxes.tax_ty4?.tax_type4 || "T4"} - ${[
                  job.cieca_pft.ty4_rate1,
                  job.cieca_pft.ty4_rate2,
                  job.cieca_pft.ty4_rate3,
                  job.cieca_pft.ty4_rate4,
                  job.cieca_pft.ty4_rate5
                ]
                  .filter((i) => i > 0)
                  .join(", ")}%`,
                total: job.job_totals.totals.us_sales_tax_breakdown.ty4Tax
              },
              {
                key: `${bodyshop.md_responsibility_centers.taxes.tax_ty5?.tax_type5 || "Adj."} - ${[
                  job.cieca_pft.ty5_rate1,
                  job.cieca_pft.ty5_rate2,
                  job.cieca_pft.ty5_rate3,
                  job.cieca_pft.ty5_rate4,
                  job.cieca_pft.ty5_rate5
                ]
                  .filter((i) => i > 0)
                  .join(", ")}%`,
                total: job.job_totals.totals.us_sales_tax_breakdown.ty5Tax
              },
              ...(job.job_totals?.totals?.ttl_tax_adjustment
                ? [
                    {
                      key: `Tax Adj.`,
                      total: job.job_totals?.totals?.ttl_tax_adjustment
                    }
                  ]
                : []),
              {
                key: t("jobs.labels.total_sales_tax"),
                bold: true,
                total: Dinero(job.job_totals.totals.us_sales_tax_breakdown.ty1Tax)
                  .add(Dinero(job.job_totals.totals.us_sales_tax_breakdown.ty2Tax))
                  .add(Dinero(job.job_totals.totals.us_sales_tax_breakdown.ty3Tax))
                  .add(Dinero(job.job_totals.totals.us_sales_tax_breakdown.ty4Tax))
                  .add(Dinero(job.job_totals.totals.us_sales_tax_breakdown.ty5Tax))
                  .add(Dinero(job.job_totals.totals.ttl_tax_adjustment))
                  .toJSON()
              }
            ].filter((item) => item.total.amount !== 0)
          : [
              {
                key: t("jobs.labels.state_tax_amt"),
                total: job.job_totals.totals.state_tax
              }
            ]
      }),

      ...(bodyshop.intellipay_config?.enable_cash_discount
        ? [
            {
              key: t("jobs.labels.total_repairs_cash_discount"),
              total: job.job_totals.totals.total_repairs,
              bold: true
            },
            {
              key: t("jobs.labels.total_repairs"),
              render: <JobTotalsCashDiscount amountDinero={job.job_totals.totals.total_repairs} />,
              bold: true
            }
          ]
        : [
            {
              key: t("jobs.labels.total_repairs"),
              total: job.job_totals.totals.total_repairs,
              bold: true
            }
          ]),

      {
        key: t("jobs.fields.ded_amt"),
        total: job.job_totals.totals.custPayable.deductible
      },
      ...InstanceRenderManager({
        imex: [
          {
            key: t("jobs.fields.federal_tax_payable"),
            total: job.job_totals.totals.custPayable.federal_tax
          }
        ],
        rome: []
      }),
      {
        key: t("jobs.fields.other_amount_payable"),
        total: job.job_totals.totals.custPayable.other_customer_amount
      },
      {
        key: t("jobs.fields.depreciation_taxes"),
        total: job.job_totals.totals.custPayable.dep_taxes
      },

      ...(bodyshop.intellipay_config?.enable_cash_discount
        ? [
            {
              key: t("jobs.labels.total_cust_payable_cash_discount"),
              total: job.job_totals.totals.custPayable.total,
              bold: true
            },
            {
              key: t("jobs.labels.total_cust_payable"),
              render: <JobTotalsCashDiscount amountDinero={job.job_totals.totals.custPayable.total} />,
              bold: true
            }
          ]
        : [
            {
              key: t("jobs.labels.total_cust_payable"),
              total: job.job_totals.totals.custPayable.total,
              bold: true
            }
          ]),
      {
        key: t("jobs.labels.net_repairs"),
        total: job.job_totals.totals.net_repairs,
        bold: true
      }
    ];
    // TODO: was removed by Patrick during a CI bug fix.
  }, [job.job_totals, job.cieca_pft, t, bodyshop.md_responsibility_centers]);

  const columns = [
    {
      //title: t("joblines.fields.part_type"),
      dataIndex: "key",
      key: "key",
      width: "80%",
      onCell: (record) => {
        return { style: { fontWeight: record.bold && "bold" } };
      }
    },
    {
      title: t("joblines.fields.total"),
      dataIndex: "total",
      key: "total",
      align: "right",
      render: (text, record) => (record.render ? record.render : Dinero(record.total).toFormat()),
      width: "20%",
      onCell: (record) => {
        return { style: { fontWeight: record.bold && "bold" } };
      }
    }
  ];

  return (
    <Table
      columns={columns}
      rowKey="key"
      showHeader={false}
      pagination={false}
      dataSource={data}
      scroll={{
        x: true
      }}
    />
  );
}
