package com.ecep.contract.vm;

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

import com.ecep.contract.VendorType;
import com.ecep.contract.model.Company;
import com.ecep.contract.model.CompanyContact;
import com.ecep.contract.model.CompanyVendor;
import com.ecep.contract.model.VendorCatalog;
import com.ecep.contract.model.VendorGroup;

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<CompanyVendor> {

    private SimpleStringProperty code = new SimpleStringProperty();
    private SimpleStringProperty state = new SimpleStringProperty();
    /**
     * 类型
     */
    private SimpleObjectProperty<VendorType> type = new SimpleObjectProperty<>();
    /**
     * 分组
     */
    private SimpleObjectProperty<VendorGroup> group = new SimpleObjectProperty<>();
    /**
     * 是否为协议供应商
     */
    private SimpleBooleanProperty protocolProvider = new SimpleBooleanProperty(false);
    /**
     * 分组
     */
    private SimpleObjectProperty<VendorCatalog> catalog = new SimpleObjectProperty<>();
    /**
     * 关联的企业
     */
    private SimpleObjectProperty<Company> company = new SimpleObjectProperty<>();
    /**
     * 开发日期
     */
    private SimpleObjectProperty<LocalDate> developDate = new SimpleObjectProperty<>();
    /**
     * 对口联系人
     */
    private SimpleObjectProperty<CompanyContact> 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(CompanyVendor companyVendor) {
        CompanyVendorViewModel model = new CompanyVendorViewModel();
        model.update(companyVendor);
        return model;
    }

    public void updateFrom(CompanyVendor v) {
        super.updateFrom(v);
//        getState().set(v.getState());
        getType().set(v.getType());
        getProtocolProvider().set(v.isProtocolProvider());
        getCatalog().set(v.getCatalog());
        getCompany().set(v.getCompany());
        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.getContact());
        version.set(v.getVersion());
    }

    public boolean copyTo(CompanyVendor 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.getCatalog())) {
            v.setCatalog(catalog.get());
            modified = true;
        }

        if (!Objects.equals(company.get(), v.getCompany())) {
            v.setCompany(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.getContact())) {
            v.setContact(contact.get());
            modified = true;
        }

        if (created.get() == null) {
            v.setCreated(null);
        } else {
            Instant instant = created.get().toInstant(ZoneOffset.ofHours(8));
            if (!Objects.equals(instant, v.getCreated())) {
                v.setCreated(instant);
                modified = true;
            }
        }
        return modified;
    }
}
