import { FunctionalComponent, h } from "preact";
import { Project } from "../../models/project";
import { useProjectEstimations } from "../../hooks/use-project-estimations";
import { getCurrency, defaults, getTaskCategoryCost } from "../../models/params";
import { getMinMaxCosts, Cost } from "../../util/stat";
import * as style from './style.module.css';
import ProjectTimeUnit from "../../components/project-time-unit";

export interface FinancialPreviewProps {
    project: Project
}

const FinancialPreview: FunctionalComponent<FinancialPreviewProps> = ({ project }) => {
    const estimations = useProjectEstimations(project);
    const costs = getMinMaxCosts(project, estimations.p99);
    
    return (
        <div class="table-container">
            <table class="table is-bordered is-striped is-fullwidth">
                <thead>
                    <tr>
                        <th colSpan={2}>
                          <span>Prévisionnel financier</span><br />
                          <span class="is-size-7 has-text-weight-normal">confiance >= 99.7%</span>
                        </th>
                    </tr>
                    <tr>
                        <th class="is-narrow">Temps</th>
                        <th>Coût</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td class="is-narrow">Maximum</td>
                        <td>
                          <CostDetails project={project} cost={costs.max} />
                        </td>
                    </tr>
                    <tr>
                        <td class="is-narrow">Minimum</td>
                        <td>
                          <CostDetails project={project} cost={costs.min} />
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
    );
};

export interface CostDetailsProps {
  project: Project
  cost: Cost
}

export const CostDetails:FunctionalComponent<CostDetailsProps> = ({ project, cost }) => {
  return (
    <details>
      <summary><strong>
        ≈ {cost.totalCost} {getCurrency(project)}</strong>
        <span class="is-pulled-right">{cost.totalTime} <ProjectTimeUnit project={project} /></span>
      </summary>
      <table class={`table is-fullwidth`}>
        <tbody>
        {
          Object.keys(cost.details).map(taskCategoryId => {
            const taskCategory = project.params.taskCategories[taskCategoryId];
            const details = cost.details[taskCategoryId];
            return (
              <tr key={`task-category-cost-${taskCategory.id}`}>
                <td class={`${style.noBorder} is-size-6`}>{taskCategory.label}</td>
                <td class={`${style.noBorder} is-size-6`}>{details.cost} {getCurrency(project)}</td>
                <td class={`${style.noBorder} is-size-6`}>{details.time} <ProjectTimeUnit project={project} /> × {getTaskCategoryCost(taskCategory)} {getCurrency(project)}</td>
              </tr>
            )
          })
        }
        </tbody>
      </table>
    </details>
  );
};

export default FinancialPreview;
