import "./App.css";
import "bootstrap/dist/css/bootstrap.min.css";
import * as base64 from "base-64";

//import MailboxList from "./Mailbox";
import AuthModal from "./AuthModal";
import React, { useEffect, useState } from "react";

interface IAuth {
	password: string;
	username: string;
}

const App = () => {
	const [state, setInternalState] = useState<IAuth | null>(null);

	const doLogin = (email: string, password: string) => {
		const domain = email.split("@")[1];
		const well_known_url = "https://" + domain + "/.well-known/jmap"
		let headers = new Headers();
		headers.append("Authorization", "Basic " + base64.encode(email + ":" + password));
		fetch(well_known_url, {
			method: "GET",
			headers: headers,
		})
			.then(response => response.json())
			.then(data => console.log(data))
			.catch(error => console.error(error));
	};
	const loadAuth = () => {
		const auth = localStorage.getItem("auth");
		if (!auth) return;
		setInternalState(JSON.parse(auth));
	};

	const setState = (auth: IAuth) => {
		localStorage.setItem("auth", JSON.stringify(auth));
	};

	useEffect(() => {
		loadAuth();
		//fetchUserData()
	}, []);

	return (
		<div className="App">
			{state ? <p>{state.username}</p> : <AuthModal doLogin={doLogin}></AuthModal>}
		</div>
	);
};

export default App;
