package com.ikon.projectmanagement.mapper;

import java.util.HashMap;

import org.springframework.stereotype.Component;

import com.ikon.projectmanagement.dto.request.ProductPSResourceAllocationRequestDto;
import com.ikon.projectmanagement.dto.response.ProductPSResourceAllocationResponseDto;
import com.ikon.projectmanagement.entity.ProductOfProject;
import com.ikon.projectmanagement.entity.ProductPSResourceAllocation;

@Component
public class ProductResourceAllocationMapper {

    public ProductPSResourceAllocationResponseDto toResponse(ProductPSResourceAllocation entity) {
        if (entity == null)
            return null;

        ProductPSResourceAllocationResponseDto dto = new ProductPSResourceAllocationResponseDto();
        dto.setId(entity.getId());
        dto.setResourceType(entity.getResourceType());
        dto.setRole(entity.getRole());
        dto.setGradeId(entity.getGradeId());
        dto.setEmployeeName(entity.getEmployeeName());
        dto.setTaskName(entity.getTaskName());
        dto.setResourceId(entity.getResourceId());
        dto.setTaskId(entity.getTaskId());
        dto.setAllocation(entity.getAllocation());
        dto.setDetailedAllocation(entity.getDetailedAllocation());
        return dto;
    }

    public ProductPSResourceAllocation toEntity(ProductPSResourceAllocationRequestDto dto, ProductOfProject product) {
        if (dto == null)
            return null;

        ProductPSResourceAllocation allocation = new ProductPSResourceAllocation();
        allocation.setResourceType(dto.getResourceType());
        allocation.setRole(dto.getRole());
        allocation.setGradeId(dto.getGradeId());
        allocation.setEmployeeName(dto.getEmployeeName());
        allocation.setTaskName(dto.getTaskName());
        allocation.setResourceId(dto.getResourceId());
        allocation.setTaskId(dto.getTaskId());
        allocation.setAllocation(dto.getAllocation() != null ? dto.getAllocation() : new HashMap<>());
        allocation.setDetailedAllocation(dto.getDetailedAllocation() != null ? dto.getDetailedAllocation() : new HashMap<>());
        allocation.setProductPS(product);
        return allocation;
    }

    public void updateEntity(ProductPSResourceAllocation entity, ProductPSResourceAllocationRequestDto dto) {
        if (entity == null || dto == null)
            return;

        if (dto.getResourceType() != null)
            entity.setResourceType(dto.getResourceType());
        if (dto.getRole() != null)
            entity.setRole(dto.getRole());
        if (dto.getGradeId() != null)
            entity.setGradeId(dto.getGradeId());
        if (dto.getEmployeeName() != null)
            entity.setEmployeeName(dto.getEmployeeName());
        if (dto.getTaskName() != null)
            entity.setTaskName(dto.getTaskName());
        if (dto.getResourceId() != null)
            entity.setResourceId(dto.getResourceId());
        if (dto.getTaskId() != null)
            entity.setTaskId(dto.getTaskId());
        if (dto.getAllocation() != null)
            entity.setAllocation(dto.getAllocation());
        if (dto.getDetailedAllocation() != null)
            entity.setDetailedAllocation(dto.getDetailedAllocation());
    }
}
