package com.ecep.contract.service;

import java.io.File;
import java.time.LocalDate;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.ecep.contract.model.IdentityEntity;
import com.ecep.contract.util.FileUtils;
import com.ecep.contract.util.ParamUtils;
import com.ecep.contract.vm.CompanyOldNameViewModel;
import com.ecep.contract.vo.CompanyOldNameVo;
import com.ecep.contract.vo.CompanyVo;

@Service
public class CompanyOldNameService extends QueryService<CompanyOldNameVo, CompanyOldNameViewModel> {
    @Autowired
    private CompanyService companyService;

    public boolean makePathAbsent(CompanyOldNameVo companyOldName) {
        String path = companyOldName.getPath();
        if (StringUtils.hasText(path)) {
            File file = new File(path);
            if (file.exists()) {
                return false;
            }
        }

        File dir = makePath(companyOldName);
        if (dir == null) {
            return false;
        }
        if (!dir.exists()) {
            return false;
        }
        companyOldName.setPath(dir.getAbsolutePath());
        return true;
    }

    public File makePath(CompanyOldNameVo companyOldName) {
        String oldName = companyOldName.getName();
        File basePath = companyService.getBasePath();
        CompanyVo company = companyService.findById(companyOldName.getCompanyId());
        String district = company.getDistrict();
        if (StringUtils.hasText(district)) {
            String parentPrefix = FileUtils.getParentPrefixByDistrict(district);
            if (parentPrefix != null) {
                File parent = new File(basePath, parentPrefix);
                if (!parent.exists()) {
                    if (!parent.mkdir()) {
                        return null;
                    }
                }
                String fileName = FileUtils.escapeFileName(oldName);
                File dir = new File(parent, fileName);
                if (!dir.exists()) {
                    if (!dir.mkdir()) {
                        return null;
                    }
                }
                return dir;
            }
        }
        return null;
    }

    public CompanyOldNameVo findMatchByDate(CompanyVo company, LocalDate localDate) {
        Page<CompanyOldNameVo> page = findAll(ParamUtils.builder()
                .equals("ambiguity", false)
                .equals("company", company.getId())
                .and(b -> b.isNotNull("beginDate").greaterThan("beginDate", localDate))
                .and(b -> b.isNotNull("endDate").lessThan("endDate", localDate))
                .build(), Pageable.ofSize(1));
        if (page.isEmpty()) {
            return null;
        }
        return page.getContent().getFirst();
    }

    public List<CompanyOldNameVo> findAllByCompanyAndName(CompanyVo company, String oldName) {
        return findAll(ParamUtils.builder().equals("company", company.getId()).equals("oldName", oldName).build(),
                Pageable.unpaged()).getContent();
    }

    public List<CompanyOldNameVo> findAllByCompany(IdentityEntity company) {
        return findAll(ParamUtils.equal("company", company.getId()), Pageable.unpaged()).getContent();
    }
}
