package com.ecep.contract.vm;

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

import com.ecep.contract.vo.CloudYuVo;

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 CloudYuInfoViewModel extends IdentityViewModel<CloudYuVo> {
    /**
     * 云端Id
     */
    private SimpleStringProperty cloudId = new SimpleStringProperty();
    /**
     * 公司ID
     */
    private SimpleObjectProperty<Integer> company = new SimpleObjectProperty<>();
    /**
     * 最后更新日期
     */
    private SimpleObjectProperty<LocalDateTime> latestUpdate = new SimpleObjectProperty<>();

    private SimpleStringProperty vendorCode = new SimpleStringProperty();
    private SimpleStringProperty vendorClassCode = new SimpleStringProperty();
    private SimpleObjectProperty<LocalDate> vendorDevelopDate = new SimpleObjectProperty<>();

    private SimpleStringProperty customerCode = new SimpleStringProperty();
    private SimpleStringProperty customerClassCode = new SimpleStringProperty();
    private SimpleObjectProperty<LocalDate> customerDevelopDate = new SimpleObjectProperty<>();

    private SimpleObjectProperty<LocalDateTime> cloudLatest = new SimpleObjectProperty<>();

    /**
     * Version
     */
    private SimpleIntegerProperty version = new SimpleIntegerProperty();

    @Override
    protected void updateFrom(CloudYuVo info) {
        super.updateFrom(info);
        vendorCode.set(info.getExceptionMessage());
        vendorDevelopDate.set(info.getVendorUpdateDate());
        customerDevelopDate.set(info.getCustomerUpdateDate());
        cloudLatest.set(info.getCloudLatest());
        latestUpdate.set(info.getLatestUpdate());
        version.set(info.getVersion());
    }

    @Override
    public boolean copyTo(CloudYuVo info) {
        boolean modified = super.copyTo(info);
        if (copyTo_((CloudYuVo) info)) {
            modified = true;
        }
        return modified;
    }

    private boolean copyTo_(CloudYuVo info) {
        boolean modified = false;
        if (!Objects.equals(info.getExceptionMessage(), vendorCode.get())) {
            info.setExceptionMessage(vendorCode.get());
            modified = true;
        }
        if (!Objects.equals(info.getVendorUpdateDate(), vendorDevelopDate.get())) {
            info.setVendorUpdateDate(vendorDevelopDate.get());
            modified = true;
        }
        if (!Objects.equals(info.getCustomerUpdateDate(), customerDevelopDate.get())) {
            info.setCustomerUpdateDate(customerDevelopDate.get());
            modified = true;
        }

        if (!Objects.equals(info.getCloudLatest(), cloudLatest.get())) {
            info.setCloudLatest(cloudLatest.get());
            modified = true;
        }
        if (!Objects.equals(info.getLatestUpdate(), latestUpdate.get())) {
            info.setLatestUpdate(latestUpdate.get());
            modified = true;
        }
        if (!Objects.equals(info.getVersion(), version.get())) {
            info.setVersion(version.get());
            modified = true;
        }

        return modified;
    }

    public static CloudYuInfoViewModel from(CloudYuVo cc) {
        CloudYuInfoViewModel model = new CloudYuInfoViewModel();
        model.update(cc);
        return model;
    }

}
