/*
 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/>
 */

/**
 *
 * @author Sebastian Javier Marchano (sebasjm)
 */
import { Amounts, TalerMerchantApi } from "@gnu-taler/taler-util";
import { useTranslationContext } from "@gnu-taler/web-util/browser";
import { h, VNode } from "preact";
import { useCallback, useState } from "preact/hooks";
import { undefinedIfEmpty } from "../../utils/table.js";
import { FormProvider } from "./FormProvider.js";
import { Input } from "./Input.js";
import { InputGroup } from "./InputGroup.js";
import { InputProps, useField } from "./useField.js";

export interface Props<T> extends InputProps<T> {
  isValid?: (e: any) => boolean;
}

type Entity = TalerMerchantApi.Tax;
export function InputTaxes<T>({ name, label }: Props<keyof T>): VNode {
  const { value: taxes, onChange } = useField<T>(name);
  const { i18n } = useTranslationContext();

  const [value, valueHandler] = useState<Partial<Entity>>({});

  const errors = undefinedIfEmpty({
    name: !value.name ? i18n.str`Required` : undefined,
    tax: !value.tax
      ? i18n.str`Required`
      : Amounts.parse(value.tax) === undefined
        ? i18n.str`Invalid`
        : undefined,
  });

  const hasErrors = errors !== undefined;

  const submit = useCallback((): void => {
    onChange([value as any, ...taxes] as any);
    valueHandler({});
  }, [value]);

  //FIXME: translating plural singular
  return (
    <InputGroup
      name="tax"
      label={label}
      alternative={
        taxes.length > 0 && (
          <p>
            <i18n.Translate>
              This product has {taxes.length} applicable taxes configured.
            </i18n.Translate>
          </p>
        )
      }
    >
      <FormProvider<Entity>
        name="tax"
        errors={errors}
        object={value}
        valueHandler={valueHandler}
      >
        <div class="field is-horizontal">
          <div class="field-label is-normal" />
          <div class="field-body" style={{ display: "block" }}>
            {taxes.map((v: any, i: number) => (
              <div
                key={i}
                class="tags has-addons mt-3 mb-0 mr-3"
                style={{ flexWrap: "nowrap" }}
              >
                <span
                  class="tag is-medium is-info mb-0"
                  style={{ maxWidth: "90%" }}
                >
                  <b>{v.tax}</b>: {v.name}
                </span>
                <a
                  class="tag is-medium is-danger is-delete mb-0"
                  onClick={() => {
                    onChange(taxes.filter((f: any) => f !== v) as any);
                    valueHandler(v);
                  }}
                />
              </div>
            ))}
            {!taxes.length && i18n.str`No taxes configured for this product.`}
          </div>
        </div>

        <Input<Entity>
          name="tax"
          label={i18n.str`Amount`}
          tooltip={i18n.str`Taxes can be in currencies that differ from the main currency used by the merchant.`}
        >
          <i18n.Translate>
            Enter the currency and value separated by a colon (e.g., &quot;USD:2.3&quot;).
          </i18n.Translate>
        </Input>

        <Input<Entity>
          name="name"
          label={i18n.str`Description`}
          tooltip={i18n.str`Legal name of the tax, e.g. VAT or import duties.`}
        />

        <div class="buttons is-right mt-5">
          <button
            class="button is-info"
            data-tooltip={i18n.str`Add tax to the tax list`}
            disabled={hasErrors}
            onClick={submit}
          >
            <i18n.Translate>Add</i18n.Translate>
          </button>
        </div>
      </FormProvider>
    </InputGroup>
  );
}
