package com.ecep.contract.controller.tab;

import java.util.Map;
import java.util.logging.Level;

import com.ecep.contract.service.*;
import org.springframework.beans.BeansException;

import com.ecep.contract.MessageHolder;
import com.ecep.contract.service.VendorService;
import com.ecep.contract.util.BeanCacher;
import com.ecep.contract.vo.ContractGroupVo;
import com.ecep.contract.vo.ContractKindVo;
import com.ecep.contract.vo.ContractTypeVo;
import com.ecep.contract.vo.EmployeeVo;
import com.fasterxml.jackson.databind.ObjectMapper;

import lombok.Data;
import lombok.Getter;

@Data
public class ContractUpdater implements MessageHolder {

    @lombok.Data
    static class Data {

        @Getter
        private boolean changed = false;

        private String guid;
        private String code;
        private String name;
        private String parentCode;
        private String description;
        private String typeCode;
        private String groupCode;
        private String kindName;
        private String way;
        private String unit;
        private java.sql.Date orderDate;
        private java.sql.Date startDate;
        private java.sql.Date endDate;
        private String setupPerson;
        private java.sql.Date setupDate;
        private String inurePerson;
        private java.sql.Date inureDate;
        private String varyPerson;
        private java.sql.Date varyDate;
        private Double totalCurrency;
        private String personId;

        private void setValues(ContractUpdater updater, Map<String, Object> rs) {
            guid = (String) rs.get("GUID");
            code = (String) rs.get("strContractID");
            name = (String) rs.get("strContractName");
            parentCode = (String) rs.get("strParentID");
            description = (String) rs.get("strContractDesc");
            typeCode = (String) rs.get("strContractType");
            groupCode = (String) rs.get("strContractGrp");
            kindName = (String) rs.get("strContractKind");
            way = (String) rs.get("strWay");
            unit = (String) rs.get("strBisectionUnit");
            orderDate = updater.convertValue(rs.get("strContractOrderDate"), java.sql.Date.class);
            startDate = updater.convertValue(rs.get("strContractStartDate"), java.sql.Date.class);
            endDate = updater.convertValue(rs.get("strContractEndDate"), java.sql.Date.class);
            setupPerson = (String) rs.get("strSetupPerson");
            setupDate = updater.convertValue(rs.get("strSetupDate"), java.sql.Date.class);
            inurePerson = (String) rs.get("strInurePerson");
            inureDate = updater.convertValue(rs.get("strInureDate"), java.sql.Date.class);
            varyPerson = (String) rs.get("strVaryPerson");
            varyDate = updater.convertValue(rs.get("dtVaryDate"), java.sql.Date.class);
            totalCurrency = (Double) rs.get("dblTotalCurrency");
            personId = (String) rs.get("strPersonID");
        }

        private void changed() {
            changed = true;
        }
    }

    public static ContractUpdater with(MessageHolder holder) {
        ContractUpdater updater = new ContractUpdater();
        updater.holder = holder;
        return updater;
    }

    private BeanCacher beanCacher = new BeanCacher();

    private MessageHolder holder;

    public <T> T convertValue(Object fromValue, Class<T> toValueType) throws IllegalArgumentException {
        return getObjectMapper().convertValue(fromValue, toValueType);
    }

    @Override
    public void addMessage(Level level, String message) {
        holder.addMessage(level, message);
    }

    public void updateMessage(String message) {
        MessageHolder consumer = getHolder();
        if (consumer != null) {
            consumer.info(message);
        }
    }

    <T> T getBean(Class<T> requiredType) throws BeansException {
        return beanCacher.getBean(requiredType);
    }

    private ObjectMapper getObjectMapper() {
        return getBean(ObjectMapper.class);
    }

    private ContractService getContractService() {
        return getBean(ContractService.class);
    }

    private EmployeeService getEmployeeService() {
        return getBean(EmployeeService.class);
    }

    private ContractTypeVo getTypeByCode(String typeCode) {
        ContractTypeVo type = getBean(ContractTypeService.class).findByCode(typeCode);
        return type;
    }

    private ContractGroupVo getGroupByCode(String groupCode) {
        ContractGroupVo group = getBean(ContractGroupService.class).findByCode(groupCode);
        return group;
    }

    private ContractKindVo getKindByName(String kindName) {
        ContractKindVo kind = getBean(ContractKindService.class).findByName(kindName);
        return kind;
    }

    private EmployeeVo findEmployeeByCode(String personCode) {
        return getEmployeeService().findByCode(personCode);
    }

    private EmployeeVo findEmployeeByName(String personName) {
        if (personName == null) {
            return null;
        }
        return getEmployeeService().findByName(personName);
    }

    private VendorService getVendorService() {
        return getBean(VendorService.class);
    }

    private CustomerService getCustomerService() {
        return getBean(CustomerService.class);
    }

    private CompanyService getCompanyService() {
        return getBean(CompanyService.class);
    }

    private YongYouU8Service getU8Service() {
        return getBean(YongYouU8Service.class);
    }

}
