import { DeleteFilled } from "@ant-design/icons";
import { Button, Form, Select, Space } from "antd";
import { useState } from "react";
import { ChromePicker } from "react-color";
import { useTranslation } from "react-i18next";
import styled from "styled-components";
import LayoutFormRow from "../layout-form-row/layout-form-row.component";

import { connect } from "react-redux";
import { createStructuredSelector } from "reselect";
import { selectBodyshop } from "../../redux/user/user.selectors";
import { useSplitTreatments } from "@splitsoftware/splitio-react";

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

const mapDispatchToProps = () => ({
  //setUserLanguage: language => dispatch(setUserLanguage(language))
});
export default connect(mapStateToProps, mapDispatchToProps)(ShopInfoROStatusComponent);

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

export function ShopInfoROStatusComponent({ bodyshop, form }) {
  const { t } = useTranslation();

  const {
    treatments: { Production_List_Status_Colors }
  } = useSplitTreatments({
    attributes: {},
    names: ["Production_List_Status_Colors"],
    splitKey: bodyshop.imexshopid
  });

  const [options, setOptions] = useState(form.getFieldValue(["md_ro_statuses", "statuses"]) || []);

  const [productionStatus, setProductionStatus] = useState(
    (form.getFieldValue(["md_ro_statuses", "production_statuses"]) || []).concat(
      form.getFieldValue(["md_ro_statuses", "additional_board_statuses"]) || []
    ) || []
  );

  const handleBlur = () => {
    setOptions(form.getFieldValue(["md_ro_statuses", "statuses"]));
    setProductionStatus(
      form
        .getFieldValue(["md_ro_statuses", "production_statuses"])
        .concat(form.getFieldValue(["md_ro_statuses", "additional_board_statuses"]))
    );
  };

  return (
    <SelectorDiv id="jobstatus">
      <Form.Item
        name={["md_ro_statuses", "statuses"]}
        label={t("bodyshop.labels.alljobstatuses")}
        rules={[
          {
            required: true,
            //message: t("general.validation.required"),
            type: "array"
          }
        ]}
      >
        <Select mode="tags" onBlur={handleBlur} />
      </Form.Item>
      <Form.Item
        name={["md_ro_statuses", "active_statuses"]}
        label={t("bodyshop.fields.statuses.active_statuses")}
        rules={[
          {
            required: true,
            //message: t("general.validation.required"),
            type: "array"
          }
        ]}
      >
        <Select mode="multiple">
          {options.map((item, idx) => (
            <Select.Option key={idx} value={item}>
              {item}
            </Select.Option>
          ))}
        </Select>
      </Form.Item>
      <Form.Item
        name={["md_ro_statuses", "pre_production_statuses"]}
        label={t("bodyshop.fields.statuses.pre_production_statuses")}
        rules={[
          {
            required: true,
            //message: t("general.validation.required"),
            type: "array"
          }
        ]}
      >
        <Select mode="multiple">
          {options.map((item, idx) => (
            <Select.Option key={idx} value={item}>
              {item}
            </Select.Option>
          ))}
        </Select>
      </Form.Item>
      <Form.Item
        name={["md_ro_statuses", "production_statuses"]}
        label={t("bodyshop.fields.statuses.production_statuses")}
        rules={[
          {
            required: true,
            //message: t("general.validation.required"),
            type: "array"
          }
        ]}
      >
        <Select mode="multiple">
          {options.map((item, idx) => (
            <Select.Option key={idx} value={item}>
              {item}
            </Select.Option>
          ))}
        </Select>
      </Form.Item>
      <Form.Item
        name={["md_ro_statuses", "post_production_statuses"]}
        label={t("bodyshop.fields.statuses.post_production_statuses")}
        rules={[
          {
            required: true,
            //message: t("general.validation.required"),
            type: "array"
          }
        ]}
      >
        <Select mode="multiple">
          {options.map((item, idx) => (
            <Select.Option key={idx} value={item}>
              {item}
            </Select.Option>
          ))}
        </Select>
      </Form.Item>
      <Form.Item
        name={["md_ro_statuses", "ready_statuses"]}
        label={t("bodyshop.fields.statuses.ready_statuses")}
        rules={[
          {
            //required: true,
            //message: t("general.validation.required"),
            type: "array"
          }
        ]}
      >
        <Select mode="multiple">
          {options.map((item, idx) => (
            <Select.Option key={idx} value={item}>
              {item}
            </Select.Option>
          ))}
        </Select>
      </Form.Item>
      <Form.Item
        name={["md_ro_statuses", "additional_board_statuses"]}
        label={t("bodyshop.fields.statuses.additional_board_statuses")}
        rules={[
          {
            //required: true,
            //message: t("general.validation.required"),
            type: "array"
          }
        ]}
      >
        <Select mode="multiple">
          {options.map((item, idx) => (
            <Select.Option key={idx} value={item}>
              {item}
            </Select.Option>
          ))}
        </Select>
      </Form.Item>
      <LayoutFormRow noDivider>
        <Form.Item
          label={t("bodyshop.fields.statuses.default_scheduled")}
          rules={[
            {
              required: true
              //message: t("general.validation.required"),
            }
          ]}
          name={["md_ro_statuses", "default_scheduled"]}
        >
          <Select>
            {options.map((item, idx) => (
              <Select.Option key={idx} value={item}>
                {item}
              </Select.Option>
            ))}
          </Select>
        </Form.Item>
        <Form.Item
          label={t("bodyshop.fields.statuses.default_arrived")}
          rules={[
            {
              required: true
              //message: t("general.validation.required"),
            }
          ]}
          name={["md_ro_statuses", "default_arrived"]}
        >
          <Select>
            {options.map((item, idx) => (
              <Select.Option key={idx} value={item}>
                {item}
              </Select.Option>
            ))}
          </Select>
        </Form.Item>
        <Form.Item
          label={t("bodyshop.fields.statuses.default_exported")}
          rules={[
            {
              required: true
              //message: t("general.validation.required"),
            }
          ]}
          name={["md_ro_statuses", "default_exported"]}
        >
          <Select>
            {options.map((item, idx) => (
              <Select.Option key={idx} value={item}>
                {item}
              </Select.Option>
            ))}
          </Select>
        </Form.Item>
        <Form.Item
          label={t("bodyshop.fields.statuses.default_imported")}
          rules={[
            {
              required: true
              //message: t("general.validation.required"),
            }
          ]}
          name={["md_ro_statuses", "default_imported"]}
        >
          <Select>
            {options.map((item, idx) => (
              <Select.Option key={idx} value={item}>
                {item}
              </Select.Option>
            ))}
          </Select>
        </Form.Item>
        <Form.Item
          label={t("bodyshop.fields.statuses.default_invoiced")}
          rules={[
            {
              required: true
              //message: t("general.validation.required"),
            }
          ]}
          name={["md_ro_statuses", "default_invoiced"]}
        >
          <Select>
            {options.map((item, idx) => (
              <Select.Option key={idx} value={item}>
                {item}
              </Select.Option>
            ))}
          </Select>
        </Form.Item>
        <Form.Item
          label={t("bodyshop.fields.statuses.default_completed")}
          rules={[
            {
              required: true
              //message: t("general.validation.required"),
            }
          ]}
          name={["md_ro_statuses", "default_completed"]}
        >
          <Select>
            {options.map((item, idx) => (
              <Select.Option key={idx} value={item}>
                {item}
              </Select.Option>
            ))}
          </Select>
        </Form.Item>
        <Form.Item
          label={t("bodyshop.fields.statuses.default_delivered")}
          rules={[
            {
              required: true
              //message: t("general.validation.required"),
            }
          ]}
          name={["md_ro_statuses", "default_delivered"]}
        >
          <Select>
            {options.map((item, idx) => (
              <Select.Option key={idx} value={item}>
                {item}
              </Select.Option>
            ))}
          </Select>
        </Form.Item>
        <Form.Item
          label={t("bodyshop.fields.statuses.default_void")}
          rules={[
            {
              required: true
              //message: t("general.validation.required"),
            }
          ]}
          name={["md_ro_statuses", "default_void"]}
        >
          <Select>
            {options.map((item, idx) => (
              <Select.Option key={idx} value={item}>
                {item}
              </Select.Option>
            ))}
          </Select>
        </Form.Item>
      </LayoutFormRow>
      {Production_List_Status_Colors.treatment === "on" && (
        <LayoutFormRow grow header={t("bodyshop.fields.statuses.production_colors")} id="production_colors">
          <Form.List name={["md_ro_statuses", "production_colors"]}>
            {(fields, { add, remove }) => {
              return (
                <div>
                  <Space size="large" wrap>
                    {fields.map((field, index) => (
                      <Form.Item key={field.key}>
                        <Space direction="vertical">
                          <div style={{ display: "flex" }}>
                            <Form.Item
                              style={{ flex: 1 }}
                              label={t("jobs.fields.status")}
                              key={`${index}status`}
                              name={[field.name, "status"]}
                              rules={[
                                {
                                  required: true
                                  //message: t("general.validation.required"),
                                }
                              ]}
                            >
                              <Select>
                                {productionStatus.map((item, idx) => (
                                  <Select.Option key={idx} value={item}>
                                    {item}
                                  </Select.Option>
                                ))}
                              </Select>
                            </Form.Item>
                            <DeleteFilled
                              onClick={() => {
                                remove(field.name);
                              }}
                            />
                          </div>
                          <Form.Item
                            label={t("bodyshop.fields.statuses.color")}
                            key={`${index}color`}
                            name={[field.name, "color"]}
                            rules={[
                              {
                                required: true
                                //message: t("general.validation.required"),
                              }
                            ]}
                          >
                            <ColorPicker />
                          </Form.Item>
                        </Space>
                      </Form.Item>
                    ))}
                  </Space>
                  <Form.Item>
                    <Button
                      type="dashed"
                      onClick={() => {
                        add();
                      }}
                      style={{ width: "100%" }}
                    >
                      {t("general.actions.add")}
                    </Button>
                  </Form.Item>
                </div>
              );
            }}
          </Form.List>
        </LayoutFormRow>
      )}
    </SelectorDiv>
  );
}

export const ColorPicker = ({ value, onChange, ...restProps }) => {
  const handleChange = (color) => {
    if (onChange) onChange(color.rgb);
  };
  return <ChromePicker {...restProps} color={value} onChangeComplete={handleChange} />;
};
