import { DeleteFilled } from "@ant-design/icons";
import { Button, Form, Input, InputNumber, Select, Space, Switch } from "antd";
import { useTranslation } from "react-i18next";
import styled from "styled-components";
import { TemplateList } from "../../utils/TemplateConstants";
import ConfigFormTypes from "../config-form-components/config-form-types";
import FormListMoveArrows from "../form-list-move-arrows/form-list-move-arrows.component";
import ConfigListEmptyState from "../layout-form-row/config-list-empty-state.component.jsx";
import InlineValidatedFormRow from "../layout-form-row/inline-validated-form-row.component.jsx";
import LayoutFormRow from "../layout-form-row/layout-form-row.component";
import {
  INLINE_TITLE_GROUP_STYLE,
  INLINE_TITLE_HANDLE_STYLE,
  INLINE_TITLE_INPUT_STYLE,
  INLINE_TITLE_LABEL_STYLE,
  INLINE_TITLE_ROW_STYLE,
  INLINE_TITLE_SEPARATOR_STYLE,
  INLINE_TITLE_SWITCH_GROUP_STYLE,
  InlineTitleListIcon
} from "../layout-form-row/inline-form-row-title.utils.js";

const SelectorDiv = styled.div`
  .ant-form-item .ant-select {
    width: 200px;
  }
`;

export default function ShopInfoIntakeChecklistComponent({ form }) {
  const { t } = useTranslation();

  const TemplateListGenerated = TemplateList();
  return (
    <div>
      <SelectorDiv>
        <LayoutFormRow header={t("bodyshop.labels.intake_delivery")} id="intake-delivery">
          <Form.Item
            col={{ xs: 24, sm: 24, md: 24, lg: 24, xl: 24, xxl: 24 }}
            name={["intakechecklist", "templates"]}
            label={t("bodyshop.fields.intake.templates")}
            rules={[
              {
                required: true,
                //message: t("general.validation.required"),
                type: "array"
              }
            ]}
            >
              <Select
                mode="multiple"
                options={Object.keys(TemplateListGenerated).map((i) => ({
                  value: TemplateListGenerated[i].key,
                  label: TemplateListGenerated[i].title
                }))}
              />
            </Form.Item>
          <Form.Item
            col={{ xs: 24, sm: 24, md: 24, lg: 24, xl: 24, xxl: 24 }}
            name={["deliverchecklist", "templates"]}
            label={t("bodyshop.fields.deliver.templates")}
            rules={[
              {
                required: true,
                //message: t("general.validation.required"),
                type: "array"
              }
            ]}
          >
            <Select
              mode="multiple"
                options={Object.keys(TemplateListGenerated).map((i) => ({
                  value: TemplateListGenerated[i].key,
                  label: TemplateListGenerated[i].title
                }))}
              />
            </Form.Item>
          <Form.Item
            col={{ xs: 24, sm: 10, md: 8, lg: 8, xl: 8, xxl: 8 }}
            name={["intakechecklist", "next_contact_hours"]}
            label={t("bodyshop.fields.intake.next_contact_hours")}
          >
            <InputNumber min={0} precision={0} suffix="hrs" />
          </Form.Item>
          <Form.Item
            col={{ xs: 24, sm: 14, md: 16, lg: 16, xl: 16, xxl: 16 }}
            name={["deliverchecklist", "actual_delivery"]}
            label={t("bodyshop.fields.deliver.require_actual_delivery_date")}
            rules={[
              {
                required: true
                //message: t("general.validation.required"),
              }
            ]}
          >
            <Switch />
          </Form.Item>
        </LayoutFormRow>
      </SelectorDiv>
      <Form.List name={["intakechecklist", "form"]}>
        {(fields, { add, remove, move }) => {
          return (
            <LayoutFormRow
              header={t("bodyshop.labels.intakechecklist")}
              id="intakechecklist"
              actions={[
                <Button
                  key="add-intake-checklist-item"
                  type="primary"
                  block
                  onClick={() => {
                    add();
                  }}
                >
                  {t("bodyshop.actions.add_intake_checklist_item")}
                </Button>
              ]}
            >
              <div>
                {fields.length === 0 ? (
                  <ConfigListEmptyState actionLabel={t("bodyshop.actions.add_intake_checklist_item")} />
                ) : (
                  fields.map((field, index) => {
                    return (
                      <Form.Item noStyle key={field.key}>
                        <InlineValidatedFormRow
                          form={form}
                          errorNames={[["intakechecklist", "form", field.name, "name"]]}
                          noDivider
                          title={
                            <div style={INLINE_TITLE_ROW_STYLE}>
                              <InlineTitleListIcon style={INLINE_TITLE_HANDLE_STYLE} />
                              <div style={INLINE_TITLE_GROUP_STYLE}>
                                <div style={INLINE_TITLE_LABEL_STYLE}>{t("jobs.fields.intake.name")}</div>
                                <Form.Item
                                  noStyle
                                  name={[field.name, "name"]}
                                  rules={[
                                    {
                                      required: true
                                    }
                                  ]}
                                >
                                  <Input
                                    size="small"
                                    placeholder={t("jobs.fields.intake.name")}
                                    style={{
                                      ...INLINE_TITLE_INPUT_STYLE,
                                      width: "100%"
                                    }}
                                  />
                                </Form.Item>
                              </div>
                              <div aria-hidden style={INLINE_TITLE_SEPARATOR_STYLE} />
                              <div style={INLINE_TITLE_SWITCH_GROUP_STYLE}>
                                <div style={INLINE_TITLE_LABEL_STYLE}>{t("jobs.fields.intake.required")}</div>
                                <Form.Item noStyle name={[field.name, "required"]} valuePropName="checked">
                                  <Switch />
                                </Form.Item>
                              </div>
                            </div>
                          }
                          wrapTitle
                          extra={
                            <Space align="center" size="small">
                              <Button
                                type="text"
                                danger
                                icon={<DeleteFilled />}
                                onClick={() => {
                                  remove(field.name);
                                }}
                              />
                              <FormListMoveArrows
                                move={move}
                                index={index}
                                total={fields.length}
                                orientation="horizontal"
                              />
                            </Space>
                          }
                        >
                          <Form.Item
                            label={t("jobs.fields.intake.type")}
                            key={`${index}type`}
                            name={[field.name, "type"]}
                            rules={[
                              {
                                required: true
                                //message: t("general.validation.required"),
                              }
                            ]}
                          >
                            <Select options={Object.keys(ConfigFormTypes).map((i) => ({ value: i, label: i }))} />
                          </Form.Item>
                          <Form.Item
                            label={t("jobs.fields.intake.label")}
                            key={`${index}label`}
                            name={[field.name, "label"]}
                            rules={[
                              {
                                required: true
                                //message: t("general.validation.required"),
                              }
                            ]}
                          >
                            <Input />
                          </Form.Item>

                          <Form.Item shouldUpdate>
                            {() => {
                              if (form.getFieldValue(["intakechecklist", "form", index, "type"]) !== "slider")
                                return null;
                              return (
                                <>
                                  <Form.Item
                                    label={t("jobs.fields.intake.min")}
                                    key={`${index}min`}
                                    name={[field.name, "min"]}
                                    dependencies={[[field.name, "type"]]}
                                    rules={[
                                      {
                                        required: true
                                        //message: t("general.validation.required"),
                                      }
                                    ]}
                                  >
                                    <InputNumber />
                                  </Form.Item>
                                  <Form.Item
                                    label={t("jobs.fields.intake.max")}
                                    key={`${index}max`}
                                    name={[field.name, "max"]}
                                    rules={[
                                      {
                                        required: true
                                        //message: t("general.validation.required"),
                                      }
                                    ]}
                                  >
                                    <InputNumber />
                                  </Form.Item>
                                </>
                              );
                            }}
                          </Form.Item>
                        </InlineValidatedFormRow>
                      </Form.Item>
                    );
                  })
                )}
              </div>
            </LayoutFormRow>
          );
        }}
      </Form.List>
      <Form.List name={["deliverchecklist", "form"]}>
        {(fields, { add, remove, move }) => {
          return (
            <LayoutFormRow
              header={t("bodyshop.labels.deliverchecklist")}
              id="deliverchecklist"
              actions={[
                <Button
                  key="add-delivery-checklist-item"
                  type="primary"
                  block
                  onClick={() => {
                    add();
                  }}
                >
                  {t("bodyshop.actions.add_delivery_checklist_item")}
                </Button>
              ]}
            >
              <div>
                {fields.length === 0 ? (
                  <ConfigListEmptyState actionLabel={t("bodyshop.actions.add_delivery_checklist_item")} />
                ) : (
                  fields.map((field, index) => {
                    return (
                      <Form.Item noStyle key={field.key}>
                        <InlineValidatedFormRow
                          form={form}
                          errorNames={[["deliverchecklist", "form", field.name, "name"]]}
                          noDivider
                          title={
                            <div style={INLINE_TITLE_ROW_STYLE}>
                              <InlineTitleListIcon style={INLINE_TITLE_HANDLE_STYLE} />
                              <div style={INLINE_TITLE_GROUP_STYLE}>
                                <div style={INLINE_TITLE_LABEL_STYLE}>{t("jobs.fields.intake.name")}</div>
                                <Form.Item
                                  noStyle
                                  name={[field.name, "name"]}
                                  rules={[
                                    {
                                      required: true
                                    }
                                  ]}
                                >
                                  <Input
                                    size="small"
                                    placeholder={t("jobs.fields.intake.name")}
                                    style={{
                                      ...INLINE_TITLE_INPUT_STYLE,
                                      width: "100%"
                                    }}
                                  />
                                </Form.Item>
                              </div>
                              <div aria-hidden style={INLINE_TITLE_SEPARATOR_STYLE} />
                              <div style={INLINE_TITLE_SWITCH_GROUP_STYLE}>
                                <div style={INLINE_TITLE_LABEL_STYLE}>{t("jobs.fields.intake.required")}</div>
                                <Form.Item noStyle name={[field.name, "required"]} valuePropName="checked">
                                  <Switch />
                                </Form.Item>
                              </div>
                            </div>
                          }
                          wrapTitle
                          extra={
                            <Space align="center" size="small">
                              <Button
                                type="text"
                                danger
                                icon={<DeleteFilled />}
                                onClick={() => {
                                  remove(field.name);
                                }}
                              />
                              <FormListMoveArrows
                                move={move}
                                index={index}
                                total={fields.length}
                                orientation="horizontal"
                              />
                            </Space>
                          }
                        >
                          <Form.Item
                            label={t("jobs.fields.intake.type")}
                            key={`${index}typed`}
                            name={[field.name, "type"]}
                            rules={[
                              {
                                required: true
                                //message: t("general.validation.required"),
                              }
                            ]}
                          >
                            <Select options={Object.keys(ConfigFormTypes).map((i) => ({ value: i, label: i }))} />
                          </Form.Item>

                          <Form.Item
                            label={t("jobs.fields.intake.label")}
                            key={`${index}labeld`}
                            name={[field.name, "label"]}
                            rules={[
                              {
                                required: true
                                //message: t("general.validation.required"),
                              }
                            ]}
                          >
                            <Input />
                          </Form.Item>

                          <Form.Item shouldUpdate>
                            {() => {
                              if (form.getFieldValue(["deliverchecklist", "form", index, "type"]) !== "slider")
                                return null;
                              return (
                                <>
                                  <Form.Item
                                    label={t("jobs.fields.intake.min")}
                                    key={`${index}mind`}
                                    name={[field.name, "min"]}
                                    dependencies={[[field.name, "type"]]}
                                    rules={[
                                      {
                                        required: form.getFieldValue([field.name, "type"]) === "slider"
                                        //message: t("general.validation.required"),
                                      }
                                    ]}
                                  >
                                    <InputNumber />
                                  </Form.Item>
                                  <Form.Item
                                    label={t("jobs.fields.intake.max")}
                                    key={`${index}maxd`}
                                    name={[field.name, "max"]}
                                    dependencies={[[field.name, "type"]]}
                                    rules={[
                                      {
                                        required: form.getFieldValue([field.name, "type"]) === "slider"
                                        //message: t("general.validation.required"),
                                      }
                                    ]}
                                  >
                                    <InputNumber />
                                  </Form.Item>
                                </>
                              );
                            }}
                          </Form.Item>
                        </InlineValidatedFormRow>
                      </Form.Item>
                    );
                  })
                )}
              </div>
            </LayoutFormRow>
          );
        }}
      </Form.List>
    </div>
  );
}
