import { Button, Input, Space, Spin } from "antd";
import { useTranslation } from "react-i18next";
import { connect } from "react-redux";
import { createStructuredSelector } from "reselect";
import {
  ExclamationCircleFilled,
  ExclamationCircleOutlined,
  UserDeleteOutlined,
  UsergroupDeleteOutlined
} from "@ant-design/icons";
import { selectBodyshop } from "../../redux/user/user.selectors";
import EmployeeSearchSelectComponent from "../employee-search-select/employee-search-select.component";
import { logImEXEvent } from "../../firebase/firebase.utils";

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

const mapDispatchToProps = () => ({
  //setUserLanguage: language => dispatch(setUserLanguage(language))
});

export default connect(mapStateToProps, mapDispatchToProps)(ProductionBoardFilters);

export function ProductionBoardFilters({ bodyshop, filter, setFilter, loading }) {
  const { t } = useTranslation();

  const toggleAlertFilter = () => {
    setFilter({ ...filter, alert: !filter.alert });
    logImEXEvent("visual_board_filter_alert", {});
  };

  const toggleUnassignedFilter = () => {
    setFilter({ ...filter, unassigned: !filter.unassigned });
    logImEXEvent("visual_board_filter_unassigned", {});
  };

  return (
    <Space wrap>
      {loading && <Spin />}
      <Input.Search
        //value={filter.search}
        placeholder={t("general.labels.search")}
        onChange={(e) => {
          setFilter({ ...filter, search: e.target.value });
          logImEXEvent("visual_board_filter_search", { search: e.target.value });
        }}
        enterButton
      />
      <EmployeeSearchSelectComponent
        style={{ minWidth: "20rem" }}
        options={bodyshop.employees.filter((e) => e.active)}
        value={filter.employeeId}
        placeholder={t("production.labels.employeesearch")}
        onChange={(emp) => {
          setFilter({ ...filter, employeeId: emp });
          logImEXEvent("visual_board_filter_alert", { employeeId: emp });
        }}
        allowClear
      />
      <Button
        type={filter?.alert ? "primary" : "default"}
        onClick={toggleAlertFilter}
        icon={filter?.alert ? <ExclamationCircleFilled /> : <ExclamationCircleOutlined />}
      >
        {t("production.labels.alerts")}
      </Button>
      <Button
        type={filter?.unassigned ? "primary" : "default"}
        onClick={toggleUnassignedFilter}
        icon={filter?.unassigned ? <UserDeleteOutlined /> : <UsergroupDeleteOutlined />}
      >
        {t("production.labels.unassigned")}
      </Button>
    </Space>
  );
}
