import {OverlayTrigger} from "react-bootstrap";
import React from "react";


const resourcesWidthStyle = (occupied, max) => {
    return {
        width: `${Math.min(occupied * 100 / max, 100)}%`,
    }
};

function EEMRenderer({occupied, max}) {
    return (
        <div className="nbr-connectors">
            <div style={{...resourcesWidthStyle(occupied, max)}}></div>
        </div>
    )
}

function ClockRenderer({occupied, max}) {
    return (
        <div className="nbr-clocks">
            <div style={{...resourcesWidthStyle(occupied, max)}}></div>
        </div>
    )
}

const resource_progress_renderers = {
    "eem": EEMRenderer,
    "clk": ClockRenderer,
    "idc": EEMRenderer
}


function EEMTipRender({occupied, max}) {
    return (<p>{`${occupied}/${max} EEM connectors used`}</p>);
}

function IDCTipRender({occupied, max}) {
    return (<p>{`${occupied}/${max} IDC connectors used`}</p>);
}

function ClockTipRender({occupied, max}) {
    return (<p>{`${occupied}/${max} clock connectors used`}</p>);
}

const resource_tip = {
    "eem": EEMTipRender,
    "clk": ClockTipRender,
    "idc": IDCTipRender
}

function RenderResources({resources, library}) {
    if (!resources) return null;
    let result = [];

    resources.forEach((value, _) => {
        if (library[value.name]) result.push(library[value.name](value));
    });

    return result;
}

export function Resources({resources}) {
    return (
        <OverlayTrigger
            placement="top"
            trigger={['click', 'hover', 'focus']}
            overlay={({arrowProps, hasDoneInitialMeasure, show, ...props}) => (
                <div className="k-popup-connectors" {...props}>
                    <RenderResources
                        resources={resources}
                        library={resource_tip}
                    />
                </div>
            )}
            rootClose
        >
            <div className="progress-container">
                <RenderResources
                    resources={resources}
                    library={resource_progress_renderers}
                />
            </div>
        </OverlayTrigger>
    )
}