/*
 This file is part of GNU Taler
 (C) 2022 Taler Systems S.A.

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

 GNU Taler 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 General Public License for more details.

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

import { Logger } from "@gnu-taler/taler-util";
import {
  ErrorType,
  HttpResponsePaginated,
  useTranslationContext,
} from "@gnu-taler/web-util/lib/index.browser";
import { Fragment, h, VNode } from "preact";
import { Loading } from "../components/Loading.js";
import { useBackendContext } from "../context/backend.js";
import { PageStateType, usePageContext } from "../context/pageState.js";
import { AccountPage } from "./AccountPage.js";
import { AdminPage } from "./AdminPage.js";
import { LoginForm } from "./LoginForm.js";
import { WithdrawalQRCode } from "./WithdrawalQRCode.js";

const logger = new Logger("AccountPage");

/**
 * show content based on state:
 * - LoginForm if the user is not logged in
 * - qr code if withdrawal in progress
 * - else account information
 * Use the handler to catch error cases
 *
 * @param param0
 * @returns
 */
export function HomePage({ onRegister }: { onRegister: () => void }): VNode {
  const backend = useBackendContext();
  const { pageState, pageStateSetter } = usePageContext();
  const { i18n } = useTranslationContext();

  function saveError(error: PageStateType["error"]): void {
    pageStateSetter((prev) => ({ ...prev, error }));
  }

  function saveErrorAndLogout(error: PageStateType["error"]): void {
    saveError(error);
    backend.logOut();
  }

  function clearCurrentWithdrawal(): void {
    pageStateSetter((prevState: PageStateType) => {
      return {
        ...prevState,
        withdrawalId: undefined,
        talerWithdrawUri: undefined,
        withdrawalInProgress: false,
      };
    });
  }

  if (backend.state.status === "loggedOut") {
    return <LoginForm onRegister={onRegister} />;
  }

  const { withdrawalId, talerWithdrawUri } = pageState;

  if (talerWithdrawUri && withdrawalId) {
    return (
      <WithdrawalQRCode
        account={backend.state.username}
        withdrawalId={withdrawalId}
        talerWithdrawUri={talerWithdrawUri}
        onConfirmed={() => {
          pageStateSetter((prevState) => {
            const { talerWithdrawUri, ...rest } = prevState;
            // remove talerWithdrawUri and add info
            return {
              ...rest,
              info: i18n.str`Withdrawal confirmed!`,
            };
          });
        }}
        onError={(error) => {
          pageStateSetter((prevState) => {
            const { talerWithdrawUri, ...rest } = prevState;
            // remove talerWithdrawUri and add error
            return {
              ...rest,
              error,
            };
          });
        }}
        onAborted={clearCurrentWithdrawal}
        onLoadNotOk={handleNotOkResult(
          backend.state.username,
          saveError,
          i18n,
          onRegister,
        )}
      />
    );
  }

  if (backend.state.isUserAdministrator) {
    return (
      <AdminPage
        onLoadNotOk={handleNotOkResult(
          backend.state.username,
          saveErrorAndLogout,
          i18n,
          onRegister,
        )}
      />
    );
  }

  return (
    <AccountPage
      account={backend.state.username}
      onLoadNotOk={handleNotOkResult(
        backend.state.username,
        saveErrorAndLogout,
        i18n,
        onRegister,
      )}
    />
  );
}

function handleNotOkResult(
  account: string,
  onErrorHandler: (state: PageStateType["error"]) => void,
  i18n: ReturnType<typeof useTranslationContext>["i18n"],
  onRegister: () => void,
): <T>(result: HttpResponsePaginated<T, SandboxBackend.SandboxError>) => VNode {
  return function handleNotOkResult2<T>(
    result: HttpResponsePaginated<T, SandboxBackend.SandboxError>,
  ): VNode {
    if (result.clientError && result.isUnauthorized) {
      onErrorHandler({
        title: i18n.str`Wrong credentials for "${account}"`,
      });
      return <LoginForm onRegister={onRegister} />;
    }
    if (result.clientError && result.isNotfound) {
      onErrorHandler({
        title: i18n.str`Username or account label "${account}" not found`,
      });
      return <LoginForm onRegister={onRegister} />;
    }
    if (result.loading) return <Loading />;
    if (!result.ok) {
      switch (result.type) {
        case ErrorType.TIMEOUT: {
          onErrorHandler({
            title: i18n.str`Request timeout, try again later.`,
          });
          break;
        }
        case ErrorType.CLIENT: {
          const errorData = result.payload;
          onErrorHandler({
            title: i18n.str`Could not load due to a client error`,
            description: errorData.error.description,
            debug: JSON.stringify(result),
          });
          break;
        }
        case ErrorType.SERVER: {
          const errorData = result.error;
          onErrorHandler({
            title: i18n.str`Server returned with error`,
            description: errorData.error.description,
            debug: JSON.stringify(result),
          });
          break;
        }
        case ErrorType.UNEXPECTED: {
          onErrorHandler({
            title: i18n.str`Unexpected error.`,
            description: `Diagnostic from ${result.info?.url} is "${result.message}"`,
            debug: JSON.stringify(result.exception),
          });
          break;
        }
        default: {
          assertUnreachable(result);
        }
      }

      return <LoginForm onRegister={onRegister} />;
    }
    return <div />;
  };
}
export function assertUnreachable(x: never): never {
  throw new Error("Didn't expect to get here");
}
