<script lang="ts">
  import {
    addTodoItem,
    addTodoItemBackend,
    getOrCreateTodoList,
    removeTodoItem,
    removeTodoItemBackend,
    serializeTodoList,
    setTodoList,
    toggleTodoItem,
    toggleTodoItemBackend,
    todosStore,
    type TodoItem,
    updateTodoItemText,
    updateTodoItemTextBackend
  } from "$lib/stores/todos";
  import { get } from "svelte/store";

  export let openDocumentId = "";
  export let openDocumentName = "";
  export let openDocumentContent = "";
  export let onDocumentContentChange: (content: string) => void = () => {};

  let todoItems: TodoItem[] = [];
  let lastTodoDocumentId = "";
  let newTodoText = "";
  let editingTodoId: number | null = null;
  let editingTodoText = "";

  async function addTodo() {
    const text = newTodoText.trim();
    if (!text) return;
    newTodoText = "";
    const backendItem = await addTodoItemBackend(openDocumentId, text);
    if (backendItem) {
      todoItems = [backendItem, ...todoItems];
    } else {
      todoItems = addTodoItem(todoItems, text);
    }
    persistTodosForCurrentList();
  }

  async function toggleTodoDone(id: number) {
    const ok = await toggleTodoItemBackend(openDocumentId, id);
    if (!ok) {
      todoItems = toggleTodoItem(todoItems, id);
    } else {
      todoItems = todoItems.map((t) => (t.id === id ? { ...t, done: !t.done } : t));
    }
    persistTodosForCurrentList();
  }

  function startEditTodo(id: number) {
    const todo = todoItems.find((item) => item.id === id);
    if (!todo) return;
    editingTodoId = id;
    editingTodoText = todo.text;
  }

  async function saveEditTodo() {
    if (editingTodoId === null) return;
    const text = editingTodoText.trim();
    if (!text) return;
    const id = editingTodoId;
    editingTodoId = null;
    editingTodoText = "";
    const ok = await updateTodoItemTextBackend(openDocumentId, id, text);
    if (!ok) {
      todoItems = updateTodoItemText(todoItems, id, text);
    } else {
      todoItems = todoItems.map((t) => (t.id === id ? { ...t, text: text.trim() } : t));
    }
    persistTodosForCurrentList();
  }

  function cancelEditTodo() {
    editingTodoId = null;
    editingTodoText = "";
  }

  async function removeTodo(id: number) {
    if (editingTodoId === id) {
      cancelEditTodo();
    }
    const ok = await removeTodoItemBackend(openDocumentId, id);
    if (!ok) {
      todoItems = removeTodoItem(todoItems, id);
    } else {
      todoItems = todoItems.filter((t) => t.id !== id);
    }
    persistTodosForCurrentList();
  }

  function loadTodosForDocument(documentId: string) {
    if (!documentId) {
      todoItems = [];
      return;
    }

    const lists = get(todosStore);
    const result = getOrCreateTodoList(lists, documentId, openDocumentContent);
    if (result.lists !== lists) {
      todosStore.set(result.lists);
    }
    todoItems = result.todos;
  }

  function persistTodosForCurrentList() {
    if (!openDocumentId) return;
    const lists = get(todosStore);
    todosStore.set(setTodoList(lists, openDocumentId, todoItems));
    const markdown = serializeTodoList(openDocumentName, todoItems);
    onDocumentContentChange(markdown);
  }

  $: if (openDocumentId !== lastTodoDocumentId) {
    loadTodosForDocument(openDocumentId);
    editingTodoId = null;
    editingTodoText = "";
    newTodoText = "";
    lastTodoDocumentId = openDocumentId;
  }
</script>

<section class="todo-surface">
  <div class="todo-card">
    <form class="todo-create" on:submit|preventDefault={addTodo}>
      <input
        type="text"
        placeholder="Add a new to-do"
        bind:value={newTodoText}
        aria-label="Add to-do"
      />
      <button type="submit" class="todo-add-btn">Add</button>
    </form>

    <ul class="todo-list">
      {#each todoItems as todo}
        <li class="todo-item">
          <label class="todo-check">
            <input type="checkbox" checked={todo.done} on:change={() => toggleTodoDone(todo.id)} />
          </label>

          {#if editingTodoId === todo.id}
            <input
              type="text"
              class="todo-edit-input"
              bind:value={editingTodoText}
              on:keydown={(event) => {
                if (event.key === "Enter") saveEditTodo();
                if (event.key === "Escape") cancelEditTodo();
              }}
            />
            <div class="todo-actions">
              <button type="button" class="todo-btn save" on:click={saveEditTodo}>Save</button>
              <button type="button" class="todo-btn ghost" on:click={cancelEditTodo}>Cancel</button>
            </div>
          {:else}
            <span class="todo-text" class:is-done={todo.done}>{todo.text}</span>
            <div class="todo-actions">
              <button type="button" class="todo-btn ghost" on:click={() => startEditTodo(todo.id)}>Edit</button>
              <button type="button" class="todo-btn danger" on:click={() => removeTodo(todo.id)}>Remove</button>
            </div>
          {/if}
        </li>
      {/each}
    </ul>
  </div>
</section>

<style>
  .todo-surface {
    min-height: 0;
    flex: 1;
    padding: 8px;
    display: grid;
    place-items: center;
  }

  .todo-card {
    width: min(760px, 100%);
    border: 1px solid var(--border-soft);
    border-radius: 12px;
    background: var(--surface-1);
    box-shadow: 0 14px 34px rgba(0, 0, 0, 0.25);
    padding: 14px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    max-height: 100%;
    overflow: auto;
  }

  .todo-create {
    display: flex;
    gap: 8px;
  }

  .todo-create input,
  .todo-edit-input {
    width: 100%;
    border: 1px solid var(--border-soft);
    border-radius: 8px;
    background: var(--bg-app);
    color: var(--text-primary);
    padding: 8px 10px;
    font-size: 0.86rem;
  }

  .todo-add-btn {
    border-radius: 8px;
    border: 1px solid var(--border-strong);
    background: var(--surface-3);
    color: var(--text-primary);
    padding: 8px 12px;
    font-size: 0.82rem;
    cursor: pointer;
  }

  .todo-add-btn:hover {
    background: var(--bg-hover);
  }

  .todo-list {
    list-style: none;
    display: flex;
    flex-direction: column;
    gap: 8px;
  }

  .todo-item {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr) auto;
    align-items: center;
    gap: 10px;
    border: 1px solid var(--border-soft);
    border-radius: 8px;
    padding: 8px 10px;
    background: var(--bg-app);
  }

  .todo-check {
    display: grid;
    place-items: center;
  }

  .todo-text {
    font-size: 0.86rem;
    color: var(--text-primary);
  }

  .todo-text.is-done {
    color: var(--text-dim);
    text-decoration: line-through;
  }

  .todo-actions {
    display: flex;
    gap: 6px;
  }

  .todo-btn {
    border-radius: 7px;
    border: 1px solid var(--border-soft);
    background: var(--surface-1);
    color: var(--text-muted);
    padding: 6px 10px;
    font-size: 0.78rem;
    cursor: pointer;
  }

  .todo-btn.save {
    border-color: var(--border-strong);
    background: var(--surface-3);
    color: var(--text-primary);
  }

  .todo-btn.danger:hover,
  .todo-btn.ghost:hover,
  .todo-btn.save:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
  }
</style>
