/*
 This file is part of GNU Taler
 (C) 2021-2024 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 { assertUnreachable, TranslatedString } from "@gnu-taler/taler-util";
import { useTranslationContext } from "@gnu-taler/web-util/browser";
import { format } from "date-fns";
import { h, VNode } from "preact";
import { useEffect, useState } from "preact/hooks";
import {
  datetimeFormatForPreferences,
  usePreference,
} from "../../../../hooks/preference.js";
import { Tooltip } from "../../../../components/Tooltip.js";

interface Props {
  events: Event[];
}

export function Timeline({ events: e }: Props) {
  const { i18n } = useTranslationContext();
  const events = [...e];
  events.push({
    when: new Date(),
    description: i18n.str`now`,
    type: "now",
  });

  events.sort((a, b) => a.when.getTime() - b.when.getTime());
  const [preferences] = usePreference();
  const [state, setState] = useState(events);
  useEffect(() => {
    const handle = setTimeout(() => {
      const eventsWithoutNow = state.filter((e) => e.type !== "now");
      eventsWithoutNow.push({
        when: new Date(),
        description: i18n.str`now`,
        type: "now",
      });
      setState(eventsWithoutNow);
    }, 1000);
    return () => {
      clearTimeout(handle);
    };
  });
  return (
    <div class="timeline">
      {events.map((e, i) => {
        return (
          <div key={i} class="timeline-item">
            {(() => {
              switch (e.type) {
                case "refund-deadline":
                  return (
                    <Tooltip
                      text={i18n.str`This is when the refund period has expired.`}
                    >
                      <i class="timeline-marker is-icon icon mdi mdi-flag" />
                    </Tooltip>
                  );
                case "pay-deadline":
                  return (
                    <Tooltip
                      text={i18n.str`This is when the time for making the payment has been expired.`}
                    >
                      <i class="timeline-marker is-icon icon mdi mdi-flag" />
                    </Tooltip>
                  );
                case "delivery":
                  return (
                    <span>
                      <i class="timeline-marker is-icon icon mdi mdi-delivery" />
                    </span>
                  );
                case "start":
                  return (
                    <span>
                      <i class="timeline-marker is-icon icon mdi mdi-flag " />
                    </span>
                  );
                case "wire-deadline":
                  return (
                    <Tooltip
                      text={i18n.str`This is when the wire transfer is going to be executed by the payment service provider.`}
                    >
                      <i class="timeline-marker is-icon icon mdi mdi-flag" />
                    </Tooltip>
                  );
                case "wired-pending":
                  return (
                    <Tooltip
                      text={i18n.str`This wire transfer has been notified by the payment service provider.`}
                    >
                      <i class="timeline-marker is-icon  icon mdi mdi-cash" />
                    </Tooltip>
                  );
                case "wired-confirmed":
                  return (
                    <Tooltip
                      text={i18n.str`This wire transfer has been notified manually or by the banking system.`}
                    >
                      <i class="timeline-marker is-icon is-success icon mdi mdi-cash" />
                    </Tooltip>
                  );
                case "wired-overdue":
                  return (
                    <Tooltip
                      text={i18n.str`This wire transfer has been notified by the payment service provider.`}
                    >
                      <i class="timeline-marker is-icon is-danger timeline-marker is-icon icon mdi mdi-cash" />
                    </Tooltip>
                  );
                case "refund-created":
                  return (
                    <Tooltip
                      text={i18n.str`This refund is waiting to be claimed by the customer.`}
                    >
                      <i class="timeline-marker is-icon is-danger icon mdi mdi-cash" />
                    </Tooltip>
                  );
                case "refund-taken":
                  return (
                    <Tooltip
                      text={i18n.str`This refund has been claimed by the customer.`}
                    >
                      <i class="timeline-marker is-icon is-success icon mdi mdi-cash" />
                    </Tooltip>
                  );
                case "now":
                  return (
                    <Tooltip text={i18n.str`The current moment in time.`}>
                      <i class="timeline-marker is-icon is-info icon mdi mdi-clock" />
                    </Tooltip>
                  );
                case "refund-missed": {
                  return (
                    <Tooltip
                      text={i18n.str`This refund can't be claimed because the wire transfer has already been made.`}
                    >
                      <i class="timeline-marker is-icon is-danger icon mdi mdi-cash" />
                    </Tooltip>
                  );
                }
                default: {
                  assertUnreachable(e.type);
                }
              }
            })()}
            <div class="timeline-content">
              {e.type !== "now" && (
                <p class="heading">
                  {format(e.when, datetimeFormatForPreferences(preferences))}
                </p>
              )}
              <p>{e.description}</p>
            </div>
          </div>
        );
      })}
    </div>
  );
}
export interface Event {
  when: Date;
  description: TranslatedString | VNode;
  type:
    | "start"
    | "refund-created"
    | "refund-taken"
    | "refund-missed"
    | "refund-deadline"
    | "pay-deadline"
    | "wire-deadline"
    | "wired-pending"
    | "wired-confirmed"
    | "wired-overdue"
    | "delivery"
    | "now";
}
