package com.ecep.contract.vm;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Objects;

import com.ecep.contract.vo.CompanyCustomerEntityVo;

import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;

@Data
@EqualsAndHashCode(callSuper = false)
public class CustomerEntityViewModel extends IdentityViewModel<CompanyCustomerEntityVo> {
    private SimpleStringProperty name = new SimpleStringProperty();
    private SimpleStringProperty abbName = new SimpleStringProperty();
    private SimpleStringProperty code = new SimpleStringProperty();
    /**
     * 分组, CustomerCatalog
     */
    private SimpleObjectProperty<Integer> catalog = new SimpleObjectProperty<>();
    /**
     * 关联的企业, CompanyCustomer
     */
    private SimpleObjectProperty<Integer> customer = new SimpleObjectProperty<>();
    /**
     * 发展日期
     */
    private SimpleObjectProperty<LocalDate> developDate = new SimpleObjectProperty<>();
    /**
     * 创建人, Employee
     */
    private SimpleObjectProperty<Integer> creator = new SimpleObjectProperty<>();
    /**
     * 修改人, Employee
     */
    private SimpleObjectProperty<Integer> modifier = new SimpleObjectProperty<>();
    private SimpleObjectProperty<LocalDate> modifyDate = new SimpleObjectProperty<>();
    /**
     * 从U8系统同步时间
     */
    private SimpleObjectProperty<LocalDate> updatedDate = new SimpleObjectProperty<>();
    private SimpleObjectProperty<LocalDateTime> fetchedTime = new SimpleObjectProperty<>();

    public static CustomerEntityViewModel from(CompanyCustomerEntityVo cc) {
        CustomerEntityViewModel model = new CustomerEntityViewModel();
        model.update(cc);
        return model;
    }

    @Override
    protected void updateFrom(CompanyCustomerEntityVo v) {
        super.updateFrom(v);
        getName().set(v.getName());
        getAbbName().set(v.getAbbName());
        getCode().set(v.getCode());
        getCatalog().set(v.getCustomerCatalogId());
        getCustomer().set(v.getCustomerId());
        getDevelopDate().set(v.getDevelopDate());
        getCreator().set(v.getCreatorId());
        getModifier().set(v.getModifierId());
        getModifyDate().set(v.getModifyDate());
        getUpdatedDate().set(v.getUpdatedDate());
        getFetchedTime().set(v.getFetchedTime());
    }

    @Override
    public boolean copyTo(CompanyCustomerEntityVo v) {
        boolean modified = super.copyTo(v);
        if (!Objects.equals(name.get(), v.getName())) {
            v.setName(name.get());
            modified = true;
        }
        if (!Objects.equals(abbName.get(), v.getAbbName())) {
            v.setAbbName(abbName.get());
            modified = true;
        }
        if (!Objects.equals(code.get(), v.getCode())) {
            v.setCode(code.get());
            modified = true;
        }
        if (!Objects.equals(catalog.get(), v.getCustomerCatalogId())) {
            v.setCustomerCatalogId(catalog.get());
            modified = true;
        }
        if (!Objects.equals(customer.get(), v.getCustomerId())) {
            v.setCustomerId(customer.get());
            modified = true;
        }
        if (!Objects.equals(developDate.get(), v.getDevelopDate())) {
            v.setDevelopDate(developDate.get());
            modified = true;
        }
        if (!Objects.equals(creator.get(), v.getCreatorId())) {
            v.setCreatorId(creator.get());
            modified = true;
        }
        if (!Objects.equals(modifier.get(), v.getModifierId())) {
            v.setModifierId(modifier.get());
            modified = true;
        }
        if (!Objects.equals(modifyDate.get(), v.getModifyDate())) {
            v.setModifyDate(modifyDate.get());
            modified = true;
        }
        if (!Objects.equals(updatedDate.get(), v.getUpdatedDate())) {
            v.setUpdatedDate(updatedDate.get());
            modified = true;
        }
        if (!Objects.equals(fetchedTime.get(), v.getFetchedTime())) {
            v.setFetchedTime(fetchedTime.get());
            modified = true;
        }
        return modified;
    }
}
