package com.ecep.contract.service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.function.Consumer;

import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.ecep.contract.CompanyFileType;
import com.ecep.contract.MessageHolder;
import com.ecep.contract.MyDateTimeUtils;
import com.ecep.contract.util.ParamUtils;
import com.ecep.contract.vm.CompanyFileViewModel;
import com.ecep.contract.vo.CompanyFileVo;
import com.ecep.contract.vo.CompanyVo;

import javafx.collections.ObservableList;

@Service
public class CompanyFileService extends QueryService<CompanyFileVo, CompanyFileViewModel> {
    public List<CompanyFileVo> findByCompany(CompanyVo company) {
        if (company == null) {
            return List.of();
        }
        return findAll(ParamUtils.equal("company", company.getId()), Pageable.unpaged()).getContent();
    }

    public List<CompanyFileVo> findByCompanyAndPath(CompanyVo company, String absolutePath) {
        return findAll(ParamUtils.builder().equals("company", company.getId()).equals("filePath", absolutePath).build(),
                Pageable.unpaged()).getContent();
    }

    public void verify(CompanyVo company, LocalDate verifyDate, MessageHolder holder) {
        // 查询公司的资信评估报告
        List<CompanyFileVo> files = findFileByCompanyAndType(company, CompanyFileType.CreditReport);
        CompanyFileVo companyFile = files.stream()
                .filter(v -> v.getApplyDate() != null && v.getExpiringDate() != null)
                .filter(v -> MyDateTimeUtils.dateValidFilter(verifyDate, v.getApplyDate(), v.getExpiringDate(), 30))
                .findFirst().orElse(null);
        if (companyFile == null) {
            List<LocalDate> dates = new ArrayList<>();

            files.stream()
                    .filter(v -> v.getApplyDate() != null && !verifyDate.isBefore(v.getApplyDate()))
                    .max(Comparator.comparing(CompanyFileVo::getApplyDate))
                    .map(CompanyFileVo::getApplyDate)
                    .ifPresent(dates::add);

            files.stream()
                    .filter(v -> v.getExpiringDate() != null && !verifyDate.isAfter(v.getExpiringDate()))
                    .min(Comparator.comparing(CompanyFileVo::getApplyDate))
                    .map(CompanyFileVo::getApplyDate)
                    .ifPresent(dates::add);

            if (dates.isEmpty()) {
                holder.error("未匹配到资信评估报告");
            } else if (dates.size() == 1) {
                holder.error("未匹配到资信评估报告, 最接近日期：" + dates.getFirst());
            } else {
                LocalDate localDate = dates.stream().max(LocalDate::compareTo).orElse(null);
                holder.error("未匹配到资信评估报告, 最接近日期：" + localDate);
            }
        }
    }

    private List<CompanyFileVo> findFileByCompanyAndType(CompanyVo company, CompanyFileType companyFileType) {
        return findAll(ParamUtils.builder().equals("company", company.getId()).equals("type", companyFileType).build(),
                Pageable.unpaged()).getContent();
    }

    public LocalDate getNextCreditReportDate(CompanyVo company, Consumer<String> state) {
        // TODO Auto-generated method stub
        throw new UnsupportedOperationException("Unimplemented method 'getNextCreditReportDate'");
    }

    public void copyAsMatched(CompanyVo company, LocalDate applyDate, Consumer<String> state) {
        // TODO Auto-generated method stub
        throw new UnsupportedOperationException("Unimplemented method 'copyAsMatched'");
    }

    public void copyAsMatchedByContract(CompanyVo company, ObservableList<String> list) {
        // TODO Auto-generated method stub
        throw new UnsupportedOperationException("Unimplemented method 'copyAsMatchedByContract'");
    }

}
