<script>
  import pluralize from "pluralize";
  import capitalize from "capitalize";

  import ModalFrame from "../components/ModalFrame";
  import SubGoalInput from "../components/SubGoalInput";

  import modal from "../stores/modal";
  import stufflog from "../stores/stufflog";

  import dateStr from "../utils/dateStr";

  export let period = {};

  let error = null;
  let goal = null;
  let activity = null;
  let subActivity = null;
  let dateInput = dateStr(period.from);
  let goalId = "";
  let subGoalId = "";
  let subActivityId = "";
  let amount = "200";
  let description = "";

  function addPeriodLog() {
    error = null;
    const addLog = {
      date: new Date(`${dateInput} 12:00:00.000`),
      goalId: goalId,
      subGoalId: subGoalId,
      subActivityId: subActivityId,
      amount: parseInt(amount),
      description: description,
    }

    stufflog.updatePeriod(period.id, {addLog}).then(() => {
      modal.close();
    }).catch(err => {
      error = err.message || err;
      console.warn(err);
    });
  }

  $: goal = period.goals.find(g => g.id === goalId) || null;
  $: activity = $stufflog.activities.find(a => a.id === goal.activityId) || null;
  $: subActivity = activity.subActivities.find(sg => sg.id === subActivityId) || null;

  $: if (period.goals.length > 0 && goalId === "") {
    goalId = period.goals[0].id;
  }
  $: if (activity != null && !activity.subActivities.find(s => s.id === subActivityId)) {
    if (activity.subActivities.length > 0) {
      subActivityId = activity.subActivities[0].id;
    } else {
      subActivityId = "";
    }
  }
  $: if (goal != null && !goal.subGoals.find(s => s.id === subGoalId)) {
    subGoalId = "";
  }
</script>

<ModalFrame title={`Add ${period.name} Log`} error={error} closable on:close={() => modal.close()}>
  <form on:submit|preventDefault={() => addPeriodLog()}>
    <label>Date</label>
    <input type="date" bind:value={dateInput} />

    <label>Goal</label>
    <select bind:value={goalId}>
      {#each period.goals as goal (goal.id)}
        <option value={goal.id}>{$stufflog.activities.find(a => a.id === goal.activityId).name}</option>
      {/each}
    </select>

    {#if (activity != null)}
      <label>Sub-Activity</label>
      <select bind:value={subActivityId}>
        {#each activity.subActivities as subActivity (subActivity.id)}
          <option value={subActivity.id}>{subActivity.name}</option>
        {/each}
      </select>
    {/if}

    {#if (goal != null && goal.subGoals.length > 0)}
      <label>Sub-Goal</label>
      <select bind:value={subGoalId}>
        <option value="">None</option>

        {#each goal.subGoals as subGoal (subGoal.id)}
          <option value={subGoal.id}>{subGoal.name} ({subGoal.multiplier.toFixed(2)})</option>
        {/each}
      </select>
    {/if}

    {#if (subActivity != null)}
      <label>{pluralize(capitalize(subActivity.unitName))}</label>
      <input type="number" bind:value={amount} />
    {/if}

    <label>Description</label>
    <input type="text" bind:value={description} />
  
    <hr />

    <button type="submit">Add Goal</button>
  </form>
</ModalFrame>

<style>
</style>