import { MailOutlined, PrinterOutlined } from "@ant-design/icons";
import { Space, Spin } from "antd";
import { useState } from "react";
import { connect } from "react-redux";
import { createStructuredSelector } from "reselect";
import { setEmailOptions } from "../../redux/email/email.actions";
import { GenerateDocument } from "../../utils/RenderTemplate";
import { TemplateList } from "../../utils/TemplateConstants";
import { useNotification } from "../../contexts/Notifications/notificationContext.jsx";

const mapStateToProps = createStructuredSelector({});

const mapDispatchToProps = (dispatch) => ({
  setEmailOptions: (e) => dispatch(setEmailOptions(e))
});

export function ChatPrintButton({ conversation }) {
  const [loading, setLoading] = useState(false);
  const notification = useNotification();

  const generateDocument = (type) => {
    setLoading(true);
    GenerateDocument(
      {
        name: TemplateList("messaging").conversation_list.key,
        variables: { id: conversation.id }
      },
      {
        subject: TemplateList("messaging").conversation_list.subject
      },
      type,
      conversation.id,
      notification
    ).catch(() => {
      console.warn("Something went wrong generating a document.");
    });
    setLoading(false);
  };

  return (
    <Space wrap>
      <PrinterOutlined onClick={() => generateDocument("p")} />
      <MailOutlined onClick={() => generateDocument("e")} />
      {loading && <Spin />}
    </Space>
  );
}

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