import { Card, Divider, Form, Input, InputNumber, Select, Space, Switch, Tooltip } from "antd";
import { useTranslation } from "react-i18next";
import { connect } from "react-redux";
import { createStructuredSelector } from "reselect";
import { selectJobReadOnly } from "../../redux/application/application.selectors";
import { selectBodyshop } from "../../redux/user/user.selectors";
import InstanceRenderManager from "../../utils/instanceRenderMgr";
import CABCpvrtCalculator from "../ca-bc-pvrt-calculator/ca-bc-pvrt-calculator.component";
import CurrencyInput from "../form-items-formatted/currency-form-item.component";
import JobsDetailRatesChangeButton from "../jobs-detail-rates-change-button/jobs-detail-rates-change-button.component";
import JobsMarkPstExempt from "../jobs-mark-pst-exempt/jobs-mark-pst-exempt.component";
import FormRow from "../layout-form-row/layout-form-row.component";
import JobsDetailRatesLabor from "./jobs-detail-rates.labor.component";
import JobsDetailRatesMaterials from "./jobs-detail-rates.materials.component";
import JobsDetailRatesOther from "./jobs-detail-rates.other.component";
import JobsDetailRatesParts from "./jobs-detail-rates.parts.component";
import JobsDetailRatesProfileOVerride from "./jobs-detail-rates.profile-override.component";
import JobsDetailRatesTaxes from "./jobs-detail-rates.taxes.component";

const mapStateToProps = createStructuredSelector({
  jobRO: selectJobReadOnly,
  bodyshop: selectBodyshop
});

export function JobsDetailRates({ jobRO, form, job, bodyshop }) {
  const { t } = useTranslation();
  return (
    <Card>
      <FormRow>
        <Form.Item label={t("jobs.fields.class")} name="class">
          <Select disabled={true} />
        </Form.Item>
        <Form.Item label={t("jobs.fields.depreciation_taxes")} name="depreciation_taxes">
          <CurrencyInput disabled={jobRO} min={0} />
        </Form.Item>
        {bodyshop.region_config.toLowerCase().startsWith("ca") && (
          <Tooltip title={t("jobs.labels.ca_gst_all_if_null")}>
            <Form.Item label={t("jobs.fields.ca_customer_gst")} name="ca_customer_gst">
              <CurrencyInput
                disabled={jobRO}
                min={0}
                max={Math.round((job.job_totals && job.job_totals.totals.federal_tax.amount) || 0) / 100}
              />
            </Form.Item>
          </Tooltip>
        )}
        <Form.Item label={t("jobs.fields.other_amount_payable")} name="other_amount_payable">
          <CurrencyInput disabled={jobRO} min={0} />
        </Form.Item>
        <Form.Item label={t("jobs.fields.towing_payable")} name="towing_payable">
          <CurrencyInput disabled={jobRO} min={0} />
        </Form.Item>
        <Form.Item label={t("jobs.fields.storage_payable")} name="storage_payable">
          <CurrencyInput disabled={jobRO} min={0} />
        </Form.Item>
        <Form.Item label={t("jobs.fields.adjustment_bottom_line")} name="adjustment_bottom_line">
          <CurrencyInput disabled={jobRO || bodyshop.cdk_dealerid} />
        </Form.Item>
        {bodyshop.region_config === "CA_BC" && (
          <Space align="center">
            <Form.Item label={t("jobs.fields.ca_bc_pvrt")} name="ca_bc_pvrt">
              <CurrencyInput disabled={jobRO} min={0} />
            </Form.Item>
            <CABCpvrtCalculator form={form} disabled={jobRO} />
          </Space>
        )}
        <Form.Item label={t("jobs.fields.auto_add_ats")} name="auto_add_ats" valuePropName="checked">
          <Switch
            disabled={jobRO}
            onChange={(checked) => {
              if (checked) {
                form.setFieldsValue({ flat_rate_ats: false });
                form.setFieldsValue({ rate_ats: form.getFieldValue("rate_ats") || bodyshop.shoprates.rate_ats });
              }
            }}
          />
        </Form.Item>
        <Form.Item noStyle shouldUpdate={(prev, cur) => prev.auto_add_ats !== cur.auto_add_ats}>
          {() => {
            if (form.getFieldValue("auto_add_ats"))
              return (
                <Form.Item label={t("jobs.fields.rate_ats")} name="rate_ats">
                  <CurrencyInput disabled={jobRO} />
                </Form.Item>
              );

            return null;
          }}
        </Form.Item>
        <Form.Item label={t("jobs.fields.flat_rate_ats")} name="flat_rate_ats" valuePropName="checked">
          <Switch
            disabled={jobRO}
            onChange={(checked) => {
              if (checked) {
                form.setFieldsValue({ auto_add_ats: false });
                form.setFieldsValue({
                  rate_ats_flat: form.getFieldValue("rate_ats_flat") || bodyshop.shoprates.rate_ats_flat
                });
              }
            }}
          />
        </Form.Item>
        <Form.Item noStyle shouldUpdate={(prev, cur) => prev.flat_rate_ats !== cur.flat_rate_ats}>
          {() => {
            if (form.getFieldValue("flat_rate_ats"))
              return (
                <Form.Item label={t("jobs.fields.rate_ats_flat")} name="rate_ats_flat">
                  <CurrencyInput disabled={jobRO} />
                </Form.Item>
              );

            return null;
          }}
        </Form.Item>
      </FormRow>
      {InstanceRenderManager({
        imex: (
          <FormRow>
            <Form.Item label={t("jobs.fields.federal_tax_rate")} name="federal_tax_rate">
              <InputNumber min={0} max={1} precision={2} disabled={jobRO} />
            </Form.Item>
            <Form.Item label={t("jobs.fields.state_tax_rate")} name="state_tax_rate">
              <InputNumber min={0} max={1} precision={2} disabled={jobRO} autoComplete="new-password" />
            </Form.Item>
            <Form.Item label={t("jobs.fields.local_tax_rate")} name="local_tax_rate">
              <InputNumber min={0} max={1} precision={2} disabled={jobRO} />
            </Form.Item>
            {bodyshop.region_config.toLowerCase().startsWith("ca") && (
              <Form.Item label={t("jobs.fields.ca_gst_registrant")} name="ca_gst_registrant" valuePropName="checked">
                <Switch disabled={jobRO} />
              </Form.Item>
            )}
          </FormRow>
        )
      })}

      <FormRow
        extra={
          <Space>
            <JobsDetailRatesChangeButton form={form} disabled={jobRO} />
            {InstanceRenderManager({ imex: <JobsMarkPstExempt form={form} /> })}
          </Space>
        }
        header={t("jobs.forms.laborrates")}
      >
        <Form.Item label={t("jobs.fields.labor_rate_desc")} name="labor_rate_desc">
          <Input disabled={jobRO} />
        </Form.Item>
        <Form.Item label={t("jobs.fields.rate_laa")} name="rate_laa">
          <CurrencyInput min={0} disabled={jobRO} />
        </Form.Item>
        <Form.Item label={t("jobs.fields.rate_lab")} name="rate_lab">
          <CurrencyInput min={0} disabled={jobRO} />
        </Form.Item>
        <Form.Item label={t("jobs.fields.rate_lad")} name="rate_lad">
          <CurrencyInput min={0} disabled={jobRO} />
        </Form.Item>
        <Form.Item label={t("jobs.fields.rate_lae")} name="rate_lae">
          <CurrencyInput min={0} disabled={jobRO} />
        </Form.Item>
        <Form.Item label={t("jobs.fields.rate_lar")} name="rate_lar">
          <CurrencyInput min={0} disabled={jobRO} />
        </Form.Item>
        <Form.Item label={t("jobs.fields.rate_las")} name="rate_las">
          <CurrencyInput min={0} disabled={jobRO} />
        </Form.Item>
        <Form.Item label={t("jobs.fields.rate_laf")} name="rate_laf">
          <CurrencyInput min={0} disabled={jobRO} />
        </Form.Item>
        <Form.Item label={t("jobs.fields.rate_lam")} name="rate_lam">
          <CurrencyInput min={0} disabled={jobRO} />
        </Form.Item>
        <Form.Item label={t("jobs.fields.rate_lag")} name="rate_lag">
          <CurrencyInput min={0} disabled={jobRO} />
        </Form.Item>
        <Form.Item label={t("jobs.fields.rate_la1")} name="rate_la1">
          <CurrencyInput min={0} disabled={jobRO} />
        </Form.Item>
        <Form.Item label={t("jobs.fields.rate_la2")} name="rate_la2">
          <CurrencyInput min={0} disabled={jobRO} />
        </Form.Item>
        <Form.Item label={t("jobs.fields.rate_la3")} name="rate_la3">
          <CurrencyInput min={0} disabled={jobRO} />
        </Form.Item>
        <Form.Item label={t("jobs.fields.rate_la4")} name="rate_la4">
          <CurrencyInput min={0} disabled={jobRO} />
        </Form.Item>
        <Form.Item label={t("jobs.fields.rate_lau")} name="rate_lau">
          <CurrencyInput min={0} disabled={jobRO} />
        </Form.Item>
        <Form.Item label={t("jobs.fields.rate_mapa")} name="rate_mapa">
          <CurrencyInput min={0} disabled={jobRO} />
        </Form.Item>
        <Form.Item label={t("jobs.fields.rate_mash")} name="rate_mash">
          <CurrencyInput min={0} disabled={jobRO} />
        </Form.Item>
        <Form.Item label={t("jobs.fields.rate_mahw")} name="rate_mahw">
          <CurrencyInput min={0} disabled={jobRO} />
        </Form.Item>
        <Form.Item label={t("jobs.fields.rate_ma2s")} name="rate_ma2s">
          <CurrencyInput min={0} disabled={jobRO} />
        </Form.Item>
        <Form.Item label={t("jobs.fields.rate_ma3s")} name="rate_ma3s">
          <CurrencyInput min={0} disabled={jobRO} />
        </Form.Item>
        {
          // <Form.Item label={t("jobs.fields.rate_mabl")} name="rate_mabl">
          //   <CurrencyInput  min={0}disabled={jobRO} />
          // </Form.Item>
          // <Form.Item label={t("jobs.fields.rate_macs")} name="rate_macs">
          //   <CurrencyInput  min={0}disabled={jobRO} />
          // </Form.Item>
        }
        <Form.Item label={t("jobs.fields.rate_matd")} name="rate_matd">
          <CurrencyInput min={0} disabled={jobRO} />
        </Form.Item>
      </FormRow>
      {InstanceRenderManager({
        imex: <JobsDetailRatesParts form={form} />,
        rome: (
          <>
            <Divider titlePlacement="left">Tax Profile</Divider>
            <JobsDetailRatesProfileOVerride form={form} />
            <JobsDetailRatesParts form={form} />
            <JobsDetailRatesLabor form={form} />
            <JobsDetailRatesMaterials form={form} />
            <JobsDetailRatesOther form={form} />
            <JobsDetailRatesTaxes form={form} />
          </>
        )
      })}
    </Card>
  );
}

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