import { getErrorMessage } from "@dashboard/components/Attributes/utils";
import {
  PageErrorWithAttributesFragment,
  ProductErrorWithAttributesFragment,
} from "@dashboard/graphql";
import { commonMessages } from "@dashboard/intl";
import { joinDateTime, splitDateTime } from "@dashboard/misc";
import { TextField } from "@material-ui/core";
import { TextFieldProps } from "@material-ui/core/TextField";
import React from "react";
import { useIntl } from "react-intl";

import { useStyles } from "./styles";

type DateTimeFieldProps = Omit<TextFieldProps, "label" | "error"> & {
  onChange: (value: string) => void;
  error: ProductErrorWithAttributesFragment | PageErrorWithAttributesFragment;
  value: string;
};

export const DateTimeField: React.FC<DateTimeFieldProps> = ({
  disabled,
  error,
  name,
  onChange,
  value,
}) => {
  const intl = useIntl();
  const classes = useStyles();

  const parsedValue = value ? splitDateTime(value) : { date: "", time: "" };

  return (
    <>
      <TextField
        fullWidth
        disabled={disabled}
        error={!!error}
        helperText={getErrorMessage(error, intl)}
        label={intl.formatMessage(commonMessages.date)}
        name={`${name}:date`}
        onChange={event => {
          const date = event.target.value;

          onChange(joinDateTime(date, parsedValue.time));
        }}
        type="date"
        value={parsedValue.date}
        InputLabelProps={{ shrink: true }}
        InputProps={{
          classes: {
            root: classes.dateField,
          },
        }}
      />
      <TextField
        fullWidth
        disabled={disabled}
        error={!!error}
        helperText={getErrorMessage(error, intl)}
        label={intl.formatMessage(commonMessages.time)}
        name={`${name}:time`}
        onChange={event => {
          const time = event.target.value;

          onChange(joinDateTime(parsedValue.date, time));
        }}
        type="time"
        value={parsedValue.time}
        InputLabelProps={{ shrink: true }}
        InputProps={{
          classes: {
            root: classes.timeField,
          },
        }}
      />
    </>
  );
};
