import Metric from "../../../components/Metric/Metric";
import { formatCurrency, formatNumber } from "../../../helpers";

import { useCurrentIndex } from "../../../hooks/staking";
import { useCirculatingSupply } from "../../../hooks/tokens";
import { useFtsoPrice } from "../../../hooks/prices";

import { DecimalBigNumber } from "../../../helpers/DecimalBigNumber";

export const CurrentIndex = props => {
    const { currentIndex } = useCurrentIndex(props.chainId);

    const _props = {
        ...props,
        label: `Current Index`,
        tooltip: `The current index indicates the amount of ${props.ftsoSymbol} a holder would possess if they had staked and maintained 1 ${props.ftsoSymbol} since its launch.`,
    };

    if (currentIndex) _props.metric = `${formatNumber(currentIndex, 2)}`;
    else _props.isLoading = true;

    return <Metric {..._props} />;
};

export const Apy = props => {
    const circulatingSupply = useCirculatingSupply(props.chainId, "STNK");

    let apy = Infinity;
    if (circulatingSupply._value > 0n) {
        const value = props.distribute.div(circulatingSupply);
        apy = 100 * (Math.pow(1 + parseFloat(value.toString()), 1095) - 1);
        if (apy === 0) apy = Infinity;
    }

    const _props = {
        ...props,
        label: "APY",
        tooltip: `The annualized rate of return, accounting for compounding from ${props.stnkSymbol}’s exponential rebasing.`,
    };

    if (apy) _props.metric = `${formatNumber(apy, 2)}${apy === Infinity ? "" : "%"}`;
    else _props.isLoading = true;

    return <Metric {..._props} />;
};

export const TotalDeposit = props => {
    const circulatingSupply = useCirculatingSupply(props.chainId, "STNK");
    const ftsoPrice = useFtsoPrice(props.chainId);
    const deposit = circulatingSupply.mul(ftsoPrice);

    const _props = {
        ...props,
        label: "Total Deposit",
        tooltip: `The total stablecoin reserves in the ghostDAO treasury backing the entire circulating supply of ${props.stnkSymbol}.`,
    };

    if (deposit) _props.metric = `${formatCurrency(deposit, 2)}`;
    else _props.isLoading = true;

    return <Metric {..._props} />;
};
