import ProjectTimeUnit from "./project-time-unit";
import { defaults, getRoundUpEstimations } from "../models/params";
import { Project } from "../models/project";
import { FunctionalComponent, Fragment, h } from "preact";
import { Estimation } from "../hooks/use-project-estimations";

export interface EstimationRangeProps {
    project: Project,
    estimation: Estimation
}

export const EstimationRange: FunctionalComponent<EstimationRangeProps> = ({ project, estimation }) => {
    const roundUp = getRoundUpEstimations(project);
    let e: number|string = roundUp ? Math.ceil(estimation.e) : estimation.e;
    let sd: number|string = roundUp ? Math.ceil(estimation.sd) : estimation.sd;
    const max = e+sd;
    const min = Math.max(e-sd, 0);
    if (!roundUp) {
        sd = sd.toFixed(2);
        e = e.toFixed(2);
    }
    return (
        <Fragment>
            <abbr title={`max: ${max.toFixed(2)}, min: ${min.toFixed(2)}`}>{`${e} ± ${sd}`}</abbr>&nbsp;<ProjectTimeUnit project={project} />
        </Fragment>
        
    );
}

export default EstimationRange;