package ch.ds1.challengeTask.TodoList.todolist;

import ch.ds1.challengeTask.TodoList.entry.Entry;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(path = "api/todolist")
public class TodoListController {

  private final TodoListService todoListService;

  @Autowired
  public TodoListController(TodoListService todoListService) {
    this.todoListService = todoListService;
  }

  @GetMapping
  public List<TodoList> getTodoLists() {
    return todoListService.getTodoLists();
  }

  @GetMapping(path = "{todoListId}")
  public TodoList getTodoList(@PathVariable("todoListId") Long todoListId) {
    return todoListService.getTodoList(todoListId);
  }

  @PostMapping
  public TodoList registerNewTodoList(@RequestBody TodoList todoList) {
    return todoListService.addNewTodoList(todoList);
  }

  @DeleteMapping(path = "{todoListId}")
  public void deleteTodoList(@PathVariable("todoListId") Long todoListId) {
    todoListService.deleteTodoList(todoListId);
  }

  @PutMapping(path = "{todoListId}")
  public TodoList updateTodoList(
      @PathVariable("todoListId") Long todoListId,
      @RequestParam(required = false) String name) {
    return todoListService.updateTodoList(todoListId, name);
  }

  @GetMapping(path = "{todoListId}/entries")
  public List<Entry> getEntriesFromTodoList(@PathVariable Long todoListId) {
    return todoListService.getEntriesFromTodoList(todoListId);
  }

  @PostMapping(path = "{todoListId}/entries")
  public Entry registerNewEntryToTodoList(@PathVariable Long todoListId, @RequestBody Entry entry) {
    return todoListService.addNewEntryToTodoList(todoListId, entry);
  }

}
