package com.ecep.contract.vm;

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

import com.ecep.contract.VendorType;
import com.ecep.contract.vo.CompanyVendorVo;

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

@Data
@EqualsAndHashCode(callSuper = false)
public class CompanyVendorViewModel extends IdentityViewModel<CompanyVendorVo> {

    private SimpleStringProperty code = new SimpleStringProperty();
    private SimpleStringProperty state = new SimpleStringProperty();
    /**
     * 类型
     */
    private SimpleObjectProperty<VendorType> type = new SimpleObjectProperty<>();
    /**
     * 分组, VendorGroup
     */
    private SimpleObjectProperty<Integer> group = new SimpleObjectProperty<>();
    /**
     * 是否为协议供应商
     */
    private SimpleBooleanProperty protocolProvider = new SimpleBooleanProperty(false);
    /**
     * 分组，VendorGroup
     */
    private SimpleObjectProperty<Integer> catalog = new SimpleObjectProperty<>();
    /**
     * 关联的企业，Company
     */
    private SimpleObjectProperty<Integer> company = new SimpleObjectProperty<>();
    /**
     * 开发日期
     */
    private SimpleObjectProperty<LocalDate> developDate = new SimpleObjectProperty<>();
    /**
     * 对口联系人
     */
    private SimpleObjectProperty<Integer> contact = new SimpleObjectProperty<>();
    /**
     * 文件夹
     */
    private SimpleStringProperty path = new SimpleStringProperty();
    /**
     * 采购的产品
     */
    private SimpleStringProperty purchase = new SimpleStringProperty();
    /**
     * 说明
     */
    private SimpleStringProperty description = new SimpleStringProperty();
    /**
     * 记录创建日期
     */
    private SimpleObjectProperty<LocalDateTime> created = new SimpleObjectProperty<>();
    private SimpleIntegerProperty version = new SimpleIntegerProperty();

    public static CompanyVendorViewModel from(CompanyVendorVo companyVendor) {
        CompanyVendorViewModel model = new CompanyVendorViewModel();
        model.update(companyVendor);
        return model;
    }

    public void updateFrom(CompanyVendorVo v) {
        super.updateFrom(v);
        // getState().set(v.getState());
        getType().set(v.getType());
        getProtocolProvider().set(v.isProtocolProvider());
        getCatalog().set(v.getCatalogId());
        getCompany().set(v.getCompanyId());
        getDevelopDate().set(v.getDevelopDate());

        getPath().set(v.getPath());
        getPurchase().set(v.getPurchase());
        getDescription().set(v.getDescription());

        if (v.getCreated() != null) {
            ZoneId zone = ZoneId.systemDefault();
            ZonedDateTime zonedDateTime = v.getCreated().atZone(zone);
            LocalDateTime localDateTime = zonedDateTime.toLocalDateTime();
            getCreated().set(localDateTime);
        } else {
            getCreated().set(null);
        }

        getContact().set(v.getContactId());
        version.set(v.getVersion());
    }

    public boolean copyTo(CompanyVendorVo v) {
        boolean modified = super.copyTo(v);
        if (!Objects.equals(type.get(), v.getType())) {
            v.setType(type.get());
            modified = true;
        }
        if (!Objects.equals(protocolProvider.get(), v.isProtocolProvider())) {
            v.setProtocolProvider(protocolProvider.get());
            modified = true;
        }

        if (!Objects.equals(catalog.get(), v.getCatalogId())) {
            v.setCatalogId(catalog.get());
            modified = true;
        }

        if (!Objects.equals(company.get(), v.getCompanyId())) {
            v.setCompanyId(company.get());
            modified = true;
        }

        if (!Objects.equals(developDate.get(), v.getDevelopDate())) {
            v.setDevelopDate(developDate.get());
            modified = true;
        }

        if (!Objects.equals(path.get(), v.getPath())) {
            v.setPath(path.get());
            modified = true;
        }

        if (!Objects.equals(purchase.get(), v.getPurchase())) {
            v.setPurchase(purchase.get());
            modified = true;
        }

        if (!Objects.equals(description.get(), v.getDescription())) {
            v.setDescription(description.get());
            modified = true;
        }

        if (!Objects.equals(contact.get(), v.getContactId())) {
            v.setContactId(contact.get());
            modified = true;
        }

        if (!Objects.equals(created.get(), v.getCreated())) {
            v.setCreated(created.get());
            modified = true;
        }
        return modified;
    }
}
