<script setup lang="ts">
import { ref, onBeforeMount } from "vue";
import type { Task } from "@/types";
import { getTasks, createTask, deleteTask } from "@/api/tasks";
import { CirclePlus } from "@lucide/vue";
import TaskItem from "@/components/TaskItem.vue";
import { taskCompleted } from "@/lib/tasks";

const taskInput = ref<string>("");
const tasks = ref<Task[]>([]);
const list = ref<string>("test");
const showLastChecked = ref<boolean>(false);
const showNextReset = ref<boolean>(false);
const separateCompleted = ref<boolean>(false);

function fetchTasks() {
    void getTasks(list.value)
        .then((data: Task[]) => {
            tasks.value = data;
            setTimeout(fetchTasks, 10000);
        });
}

function onNewTask() {
    if (taskInput.value.trim()) {
        void createTask(list.value, {
            name: taskInput.value.trim(),
            reset_cron: null,
        })
            .then((data: Task) => {
                tasks.value.push(data);
            })
            .finally(() => {
                taskInput.value = "";
            });
    }
}

function onCloneTask(task: Task) {
    void createTask(list.value, {
        name: task.name,
        reset_cron: task.reset_cron,
    }).then((data: Task) => {
        tasks.value.push(data);
    })
}

function onDeleteTask(task: Task) {
    void deleteTask(task.id);

    tasks.value.splice(tasks.value.indexOf(task), 1);
}

onBeforeMount(fetchTasks);
</script>

<template>
    <ul class="list">
        <template v-for="(task, i) in tasks" :key="task.id">
            <li v-if="tasks[i] && (!separateCompleted || !taskCompleted(task))">
                <task-item
                    v-model="tasks[i]"
                    :show-last-checked="showLastChecked"
                    :show-next-reset="showNextReset"
                    @clone="onCloneTask"
                    @delete="onDeleteTask"
                />
            </li>
        </template>
        <li class="list-row">
            <div class="list-col-grow">
                <input
                    v-model="taskInput"
                    type="text"
                    placeholder="New Task"
                    class="input"
                    @keyup.enter="onNewTask"
                />
            </div>
            <div>
                <button
                    class="btn btn-square"
                    :disabled="taskInput.trim().length === 0"
                    @click="onNewTask"
                >
                    <CirclePlus />
                </button>
            </div>
        </li>
    </ul>
    <div v-if="separateCompleted" class="collapse collapse-arrow">
      <input id="completed-dropdown" type="checkbox" />
      <div class="collapse-title mb-0">Completed</div>
      <div class="collapse-content p-0">
          <ul class="list">
              <template v-for="(task, i) in tasks" :key="`${task.id}-completed`">
                    <li v-if="tasks[i] && taskCompleted(task)">
                        <task-item
                            v-model="tasks[i]"
                            :show-last-checked="showLastChecked"
                            :show-next-reset="showNextReset"
                            @clone="onCloneTask"
                            @delete="onDeleteTask"
                        />
                    </li>
              </template>
          </ul>
      </div>
    </div>
    <div class="collapse collapse-arrow  font-light text-sm">
      <input id="options-dropdown" type="checkbox"/>
      <div class="collapse-title">Options</div>
      <div class="collapse-content text-sm">
          <fieldset class="fieldset flex flex-col gap-2">
            <label class="label">
              <input v-model="showLastChecked" type="checkbox" class="checkbox" />
              Show last checked time
            </label>
            <label class="label">
              <input v-model="showNextReset" type="checkbox" class="checkbox" />
              Show next reset time
            </label>
            <label class="label">
              <input v-model="separateCompleted" type="checkbox" class="checkbox" />
              Move completed tasks below
            </label>
          </fieldset>
      </div>
    </div>
</template>
