import Metric from "../../../components/Metric/Metric";

import { formatCurrency, formatNumber } from "../../../helpers";
import { DecimalBigNumber } from "../../../helpers/DecimalBigNumber";
import { getTokenDecimals } from "../../../hooks/helpers";
import { useTotalSupply } from "../../../hooks/tokens";
import {
    useMinQuorum,
    useProposalThreshold,
    useProposalCount
} from "../../../hooks/governance";

export const MinQuorumPercentage = props => {
    const { numerator, denominator, percentage } = useMinQuorum(props.chainId);
    const { totalSupply } = useTotalSupply(props.chainId, "GHST");
    const decimals = getTokenDecimals(props.ghstSymbol);

    const value = new DecimalBigNumber(
        (totalSupply?._value ?? 0n) * numerator / denominator,
        decimals
    );

    const _props = {
        ...props,
        label: `Min Quorum`,
        tooltip: `Minimum $${props.ghstSymbol} turnout required for the proposal to become valid`,
    };
    const tokenValue = formatCurrency(value?.toString(), 2, props.ghstSymbol);
    const percentageValue = formatNumber(percentage * 100, 2);

    if (percentage) _props.metric = `${tokenValue} (${percentageValue}%)`;
    else _props.isLoading = true;

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

export const ProposalThreshold = props => {
    const { threshold } = useProposalThreshold(props.chainId, props.ghstSymbol);

    const _props = {
        ...props,
        label: "Min Collateral",
        tooltip: `Minimum $${props.ghstSymbol} required to be locked to create a proposal`,
    };

    if (threshold) _props.metric = `${formatCurrency(threshold.toString(), 2, props.ghstSymbol)}`;
    else _props.isLoading = true;

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

export const ProposalsCount = props => {
    const { proposalCount } = useProposalCount(props.chainId);

    const _props = {
        ...props,
        label: `Proposal Count`,
        tooltip: `Total proposals created`,
    };

    if (proposalCount || proposalCount === 0n) _props.metric = `${formatNumber(proposalCount.toString(), 0)}`;
    else _props.isLoading = true;

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