<script lang="ts">
  import Markdown from "$lib/components/common/Markdown.svelte";
  import Progress from "$lib/components/common/Progress.svelte";
  import Section from "$lib/components/layout/Section.svelte";
  import Option from "$lib/components/layout/Option.svelte";
  import OptionsRow from "$lib/components/layout/OptionsRow.svelte";
  import type { Requirement } from "$lib/models/project";
  import LabeledProgress from "../common/LabeledProgress.svelte";
  import LabeledProgressRow from "../common/LabeledProgressRow.svelte";
  import { STATUS_ICONS } from "../common/StatusIcon.svelte";
  import ItemEntry from "./ItemSubSection.svelte";
  import Icon from "../layout/Icon.svelte";
  import { projectPrettyId } from "$lib/utils/prettyIds";
  import AggregateAmountRow from "./AggregateAmountRow.svelte";
  import TagRow from "../common/TagRow.svelte";

  export let requirement: Requirement;
</script>

<div id={projectPrettyId(requirement)} />
<Section title={requirement.name} icon={STATUS_ICONS[requirement.status]} status={requirement.status}>
  <Progress alwaysSmooth titlePercentageOnly thin green status={requirement.status} count={requirement.totalAcquired} target={requirement.totalRequired} />
  <Progress alwaysSmooth titlePercentageOnly thinner gray count={requirement.totalPlanned} target={requirement.totalRequired} />
  <TagRow names={requirement.tags} />
  <Markdown source={requirement.description} />
  <OptionsRow slot="right">
    <Option open={{name: "item.multischedule", items: requirement.items}}><Icon name="calendar" /></Option>
    <Option open={{name: "item.create", requirement}}><Icon name="plus" /></Option>
    <Option open={{name: "requirement.edit", requirement}}><Icon name="pen" /></Option>
    <Option open={{name: "requirement.delete", requirement}} color="red"><Icon name="trash" /></Option>
  </OptionsRow>
  <LabeledProgressRow>
    {#each requirement.stats as stat (stat.id)}
      {#if !requirement.isCoarse || stat.required > 0}
        <LabeledProgress count={stat.acquired} target={stat.required} planned={stat.planned} name={stat.name} />
      {/if}
    {/each}
  </LabeledProgressRow>
  {#each (requirement.items||[]) as item (item.id)}
    <ItemEntry item={item} />
  {/each}
  {#if requirement.aggregateRequired > 0}
    <AggregateAmountRow items={requirement.items||[]} totalAcquired={requirement.totalAcquired} aggregateRequired={requirement.aggregateRequired} />
  {/if}
</Section>