package com.example.todolist.form;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.List;

import jakarta.validation.Valid;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;

import com.example.todolist.common.Utils;
import com.example.todolist.entity.Task;
import com.example.todolist.entity.Todo;

import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
public class TodoData {
    private Integer id;

    @NotBlank
    private String title;

    @NotNull
    private Integer importance;

    @Min(value = 0)
    private Integer urgency;

    private String deadline;
    private String done;

    @Valid
    private List<TaskData> taskList;

    private TaskData newTask;

    /**
     * 入力データからEntityを生成して返す
     */
    public Todo toEntity() {
        Todo todo = new Todo();
        todo.setId(id);
        todo.setTitle(title);
        todo.setImportance(importance);
        todo.setUrgency(urgency);
        todo.setDone(done);

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        try {
            todo.setDeadline(LocalDate.parse(deadline, formatter));
        } catch (DateTimeParseException e) {
            todo.setDeadline(null);
        }

        //	Task 部分
        LocalDate date;
        Task task;
        if (taskList != null) {
            for (TaskData taskData : taskList) {
                date = Utils.str2dateOrNull(taskData.getDeadline());
                task = new Task(
                        taskData.getId(), null, taskData.getTitle(), date, taskData.getDone());
                todo.addTask(task);
            }
        }

        return todo;
    }

    //  Todoの内容から入力画面へ渡すTodoDataを生成する
    public TodoData(Todo todo) {
        // Todo部分
        this.id = todo.getId();
        this.title = todo.getTitle();
        this.importance = todo.getImportance();
        this.urgency = todo.getUrgency();
        this.deadline = Utils.date2str(todo.getDeadline());
        this.done = todo.getDone();

        // 登録済Task
        this.taskList = new ArrayList<>();
        String dt;
        for (Task task : todo.getTaskList()) {
            dt = Utils.date2str(task.getDeadline());
            this.taskList.add(
                    new TaskData(task.getId(), task.getTitle(), dt, task.getDone()));
        }

        // 追加用Task
        newTask = new TaskData();
    }

    //  TodoForm画面の新規タスク入力行の内容からTaskオブジェクトを生成して返す
    public Task toTaskEntity() {
        Task task = new Task();
        task.setId(newTask.getId());
        task.setTitle(newTask.getTitle());
        task.setDone(newTask.getDone());
        task.setDeadline(Utils.str2date(newTask.getDeadline()));

        return task;
    }

}
