<script lang="typescript">
    import { timeSpan, MONTH, YEAR } from "../time";
    import { I18n } from "../i18n";
    import { HistogramData } from "./histogram-graph";
    import { gatherData, buildHistogram, GraphData, AddedRange } from "./added";
    import pb from "../backend/proto";
    import HistogramGraph from "./HistogramGraph.svelte";

    export let sourceData: pb.BackendProto.GraphsOut | null = null;
    export let i18n: I18n;

    let svg = null as HTMLElement | SVGElement | null;
    let histogramData = null as HistogramData | null;
    let range = AddedRange.Month;

    let addedData: GraphData | null = null;
    $: if (sourceData) {
        addedData = gatherData(sourceData);
    }

    $: if (addedData) {
        histogramData = buildHistogram(addedData, range);
    }

    const title = i18n.tr(i18n.TR.STATISTICS_ADDED_TITLE);
    const month = timeSpan(i18n, 1 * MONTH);
    const month3 = timeSpan(i18n, 3 * MONTH);
    const year = timeSpan(i18n, 1 * YEAR);
    const all = i18n.tr(i18n.TR.STATISTICS_RANGE_ALL_TIME);
    const yText = i18n.tr(i18n.TR.STATISTICS_AXIS_LABEL_CARD_COUNT);
</script>

{#if histogramData}
    <div class="graph">
        <h1>{title}</h1>

        <div class="range-box-inner">
            <label>
                <input type="radio" bind:group={range} value={AddedRange.Month} />
                {month}
            </label>
            <label>
                <input type="radio" bind:group={range} value={AddedRange.Quarter} />
                {month3}
            </label>
            <label>
                <input type="radio" bind:group={range} value={AddedRange.Year} />
                {year}
            </label>
            <label>
                <input type="radio" bind:group={range} value={AddedRange.AllTime} />
                {all}
            </label>
        </div>

        <HistogramGraph data={histogramData} xText="Days" {yText} {i18n} />
    </div>
{/if}
