/*
	GoToSocial
	Copyright (C) GoToSocial Authors admin@gotosocial.org
	SPDX-License-Identifier: AGPL-3.0-or-later

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU Affero General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Affero General Public License for more details.

	You should have received a copy of the GNU Affero General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

import React, { useEffect } from "react";
import { useSearch } from "wouter";
import { Error as ErrorCmp } from "../../../components/error";
import { useGetAccessTokenForAppMutation, useGetAppQuery } from "../../../lib/query/user/applications";
import Loading from "../../../components/loading";
import { useCallbackURL } from "./common";
import useFormSubmit from "../../../lib/form/submit";
import { useValue } from "../../../lib/form";
import MutationButton from "../../../components/form/mutation-button";
import FormWithData from "../../../lib/form/form-with-data";
import { App } from "../../../lib/types/application";

export function AppTokenCallback({}) {	
	
	
	// Read the callback authorization
	// code + state from the search params. 
	const search = useSearch();
	const urlQueryParams = new URLSearchParams(search);
	const code = urlQueryParams.get("code");
	const appId = urlQueryParams.get("state");

	if (!code || !appId) {
		const err = Error("code or app id not defined");
		return <ErrorCmp error={err} />;
	}

	return(
		<>
			<h2>Access Token</h2>
			<FormWithData
				dataQuery={useGetAppQuery}
				queryArg={appId}
				DataForm={AccessForAppForm}
				{...{ code: code }}
			/>
		</>
	);
}


function AccessForAppForm({ data: app, code }: { data: App, code: string }) {
	const redirectURI = useCallbackURL();
	
	// Prepare to call /oauth/token to
	// exchange code for access token.
	const form = {
		client_id: useValue("client_id", app.client_id),
		client_secret: useValue("client_secret", app.client_secret),
		redirect_uri: useValue("redirect_uri", redirectURI),
		code: useValue("code", code),
		grant_type: useValue("grant_type", "authorization_code"),
		
	};
	const [ submit, result ] = useFormSubmit(form, useGetAccessTokenForAppMutation());

	return (
		<form onSubmit={submit}>
			<MutationButton
				label="Gimme!"
				result={result}
				disabled={false}
			/>
		</form>
	);
} 
