package com.ikon.projectmanagement.mapper;

import com.ikon.projectmanagement.dto.request.ScheduleDependencyDto;
import com.ikon.projectmanagement.dto.request.ScheduleGroupDto;
import com.ikon.projectmanagement.dto.request.ScheduleTaskDto;
import com.ikon.projectmanagement.dto.response.ScheduleResponseDto;
import com.ikon.projectmanagement.entity.ProjectSchedule;
import com.ikon.projectmanagement.entity.ProjectScheduleDependency;
import com.ikon.projectmanagement.entity.ProjectScheduleGroup;
import com.ikon.projectmanagement.entity.ProjectScheduleTask;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Component
public class ScheduleMapper {

    public ScheduleResponseDto toResponse(ProjectSchedule schedule) {
        List<ScheduleTaskDto> tasks = new ArrayList<>();
        for (ProjectScheduleTask task : schedule.getTasks()) {
            ScheduleTaskDto dto = new ScheduleTaskDto();
            dto.setId(task.getId());
            dto.setParentId(task.getParentId());
            dto.setTaskName(task.getTaskName());
            dto.setTaskDuration(task.getTaskDuration());
            dto.setTaskPredecessor(task.getTaskPredecessor());
            dto.setDependencyType(task.getDependencyType());
            dto.setTaskColour(task.getTaskColour());
            dto.setDelayDuration(task.getDelayDuration());
            dto.setTaskDescription(task.getTaskDescription());
            dto.setTaskStart(task.getTaskStart());
            dto.setTaskEnd(task.getTaskEnd());
            dto.setMilestoneTask(task.getMilestoneTask());
            tasks.add(dto);
        }

        List<ScheduleDependencyDto> dependencies = new ArrayList<>();
        for (ProjectScheduleDependency dep : schedule.getDependencies()) {
            ScheduleDependencyDto dto = new ScheduleDependencyDto();
            dto.setId(dep.getId());
            dto.setPredecessorId(dep.getPredecessorId());
            dto.setDependencyType(dep.getDependencyType());
            dependencies.add(dto);
        }

        Map<String, ScheduleGroupDto> groups = new LinkedHashMap<>();
        for (ProjectScheduleGroup g : schedule.getGroups()) {
            ScheduleGroupDto dto = new ScheduleGroupDto();
            dto.setId(g.getGroupKey());
            dto.setGroupName(g.getGroupName());
            dto.setColor(g.getColor());
            dto.setTaskIds(new ArrayList<>(g.getTaskIds()));
            groups.put(g.getGroupKey(), dto);
        }

        return new ScheduleResponseDto(schedule.getProjectIdentifier(), schedule.getProductIdentifier(), tasks, dependencies, groups);
    }

    public ProjectScheduleTask toTaskEntity(ScheduleTaskDto dto, ProjectSchedule schedule) {
        ProjectScheduleTask task = new ProjectScheduleTask();
        task.setId(dto.getId());
        task.setParentId(dto.getParentId());
        task.setTaskName(dto.getTaskName());
        task.setTaskDuration(dto.getTaskDuration());
        task.setTaskPredecessor(dto.getTaskPredecessor());
        task.setDependencyType(dto.getDependencyType());
        task.setTaskColour(dto.getTaskColour());
        task.setDelayDuration(dto.getDelayDuration());
        task.setTaskDescription(dto.getTaskDescription());
        task.setTaskStart(dto.getTaskStart());
        task.setTaskEnd(dto.getTaskEnd());
        task.setMilestoneTask(dto.getMilestoneTask());
        task.setSchedule(schedule);
        return task;
    }

    public ProjectScheduleDependency toDependencyEntity(ScheduleDependencyDto dto, ProjectSchedule schedule) {
        ProjectScheduleDependency dep = new ProjectScheduleDependency();
        dep.setId(dto.getId());
        dep.setPredecessorId(dto.getPredecessorId());
        dep.setDependencyType(dto.getDependencyType());
        dep.setSchedule(schedule);
        return dep;
    }

    public ProjectScheduleGroup toGroupEntity(String groupKey, ScheduleGroupDto dto, ProjectSchedule schedule) {
        ProjectScheduleGroup g = new ProjectScheduleGroup();
        g.setGroupKey(groupKey);
        g.setGroupName(dto.getGroupName());
        g.setColor(dto.getColor());
        g.setTaskIds(dto.getTaskIds() != null ? new ArrayList<>(dto.getTaskIds()) : new ArrayList<>());
        g.setSchedule(schedule);
        return g;
    }

}
