package org.egl_cepgl.pm.service;

import lombok.extern.slf4j.Slf4j;
import org.egl_cepgl.pm.dto.*;
import org.egl_cepgl.pm.model.CustomModel;
import org.egl_cepgl.pm.repository.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
public class CustomService
{
    private FileCategoryRepository fileCategoryRepository;
    private ApplicantRepository applicantRepository;
    private EnterpriseRepository enterpriseRepository;
    private ApplicantQualificationRepository applicantQualificationRepository;
    private EnterpriseQualificationRepository enterpriseQualificationRepository;
    private ProjectRepository projectRepository;
    private ProcurementCategoryRepository procurementCategoryRepository;
    private CountryRepository countryRepository;

    @Autowired
    public CustomService(
        FileCategoryRepository fileCategoryRepository,
        ApplicantRepository applicantRepository,
        EnterpriseRepository enterpriseRepository,
        ApplicantQualificationRepository applicantQualificationRepository,
        EnterpriseQualificationRepository enterpriseQualificationRepository,
        ProjectRepository projectRepository,
        ProcurementCategoryRepository procurementCategoryRepository,
        CountryRepository countryRepository
    ){
        this.fileCategoryRepository= fileCategoryRepository;
        this.applicantRepository= applicantRepository;
        this.enterpriseRepository= enterpriseRepository;
        this.applicantQualificationRepository= applicantQualificationRepository;
        this.enterpriseQualificationRepository= enterpriseQualificationRepository;
        this.projectRepository= projectRepository;
        this.procurementCategoryRepository= procurementCategoryRepository;
        this.countryRepository= countryRepository;
    }

    public List<?> findAll(String modelType)
    {
        List<?> customModels;
        switch (modelType) {
            case "file_category":
                customModels = this.fileCategoryRepository.findAllByOrderByNamepAsc().stream().
                        map(FileCategoryDto::fromEntity).
                        collect(Collectors.toList());
                break;
            case "applicant_qual":
                customModels = this.applicantQualificationRepository.findAllByOrderByNamepAsc().stream().
                        map(ApplicantQualificationDto::fromEntity).
                        collect(Collectors.toList());
                break;
            case "enterprise_qual":
                customModels = this.enterpriseQualificationRepository.findAllByOrderByNamepAsc().stream().
                        map(EnterpriseQualificationDto::fromEntity).
                        collect(Collectors.toList());
                break;
            case "project":
                customModels = this.projectRepository.findAllByOrderByNamepAsc().stream().
                        map(ProjectDto::fromEntity).
                        collect(Collectors.toList());
                break;
            case "procurement_category":
                customModels = this.procurementCategoryRepository.findAllByOrderByNamepAsc().stream().
                        map(ProCategoryDto::fromEntity).
                        collect(Collectors.toList());
                break;
            case "country":
                customModels = this.countryRepository.findAllByOrderByNamepAsc().stream().
                        map(CountryDto::fromEntity).
                        collect(Collectors.toList());
                break;
            default:
                customModels = new ArrayList<>();
                break;
        }
//        List<CustomModel> obj= new ArrayList<>();
//        for(Map<String, Object> k : customModels) {
//            obj.add(new CustomModel(k.get("id").toString(), k.get("namep").toString()));
//        }
        return customModels;
    }
}
