import { Trash } from "react-bootstrap-icons";
import Button from "react-bootstrap/Button";
import ButtonGroup from "react-bootstrap/ButtonGroup";
import ButtonToolbar from "react-bootstrap/ButtonToolbar";
import Placeholder from "react-bootstrap/Placeholder";
import React from "react";

import Client from "./client/Client";
import DateTime from "./components/DateTime";
import { IAccount, IEmailStub, IMailbox } from "./client/types";

type EmailSummaryProps = {
	account: IAccount;
	client: Client;
	emailId: string;
	emailStub: IEmailStub | null;
	mailbox: IMailbox;
};
type EmailSummaryState = {};

class EmailSummary extends React.Component<
	EmailSummaryProps,
	EmailSummaryState
> {
	componentDidMount() {
		this.ensureData();
	}
	componentDidUpdate() {
		this.ensureData();
	}

	ensureData() {
		this.props.client.ensureEmailStub(
			this.props.account.id,
			this.props.emailId,
		);
	}

	render() {
		const href =
			"#" +
			this.props.account.id +
			"/" +
			this.props.mailbox.id +
			"/" +
			this.props.emailId;
		const stub = this.props.emailStub;
		if (stub === null) {
			return <Placeholder />;
		}
		return (
			<div className="p-2 border" key={this.props.emailId}>
				<a className="btn" href={href}>
					<DateTime d={stub.receivedAt} />
					<span>
						{" - " +
							(stub.from == null ? "?" : stub.from[0].name) +
							" - " +
							stub.subject}
					</span>
				</a>
				<ButtonToolbar>
					<ButtonGroup className="me-2">
						<Button
							onClick={() => {
								this.props.client.emailMoveTrash(
									this.props.account,
									this.props.emailId,
								);
							}}
						>
							<Trash />
						</Button>
						<Button>2</Button>
					</ButtonGroup>
				</ButtonToolbar>
			</div>
		);
	}
}
export default EmailSummary;
