package com.ecep.contract.cloud.u8;

import java.util.function.Consumer;

import org.springframework.beans.BeansException;

import com.ecep.contract.SpringApp;
import com.ecep.contract.ds.contract.service.ContractGroupService;
import com.ecep.contract.ds.contract.service.ContractKindService;
import com.ecep.contract.ds.contract.service.ContractTypeService;
import com.ecep.contract.ds.other.service.EmployeeService;
import com.ecep.contract.model.ContractGroup;
import com.ecep.contract.model.ContractKind;
import com.ecep.contract.model.ContractType;
import com.ecep.contract.model.Employee;

import lombok.Getter;
import lombok.Setter;

public class ContractSyncContext {
    <T> T getBean(Class<T> requiredType) throws BeansException {
        return SpringApp.getBean(requiredType);
    }

    private ContractTypeService contractTypeService;
    private ContractGroupService contractGroupService;
    private ContractKindService contractKindService;
    private EmployeeService employeeService;

    @Getter
    @Setter
    Consumer<String> consumer;

    public ContractSyncContext() {
    }

    private ContractTypeService getContractTypeService() {
        if (contractTypeService == null) {
            contractTypeService = getBean(ContractTypeService.class);
        }
        return contractTypeService;
    }

    private ContractGroupService getContractGroupService() {
        if (contractGroupService == null) {
            contractGroupService = getBean(ContractGroupService.class);
        }
        return contractGroupService;
    }

    private ContractKindService getContractKindService() {
        if (contractKindService == null) {
            contractKindService = getBean(ContractKindService.class);
        }
        return contractKindService;
    }

    private EmployeeService getEmployeeService() {
        if (employeeService == null) {
            employeeService = getBean(EmployeeService.class);
        }
        return employeeService;
    }

    public ContractType getTypeByCode(String typeCode) {
        return getContractTypeService().findByCode(typeCode);
    }

    public ContractGroup getGroupByCode(String groupCode) {
        return getContractGroupService().findByCode(groupCode);
    }

    public ContractKind getKindByName(String kindName) {
        return getContractKindService().findByName(kindName);
    }



    public void updateMessage(String message) {
        Consumer<String> consumer = getConsumer();
        if (consumer != null) {
            consumer.accept(message);
        }
    }
    public Employee findEmployeeByCode(String personCode) {
        return getEmployeeService().findByCode(personCode);
    }
    public Employee findEmployeeByName(String personName) {
        if (personName == null) {
            return null;
        }
        return getEmployeeService().findByName(personName);
    }
}
