package com.ecep.contract.vm;

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

import com.ecep.contract.model.CloudYu;
import com.ecep.contract.model.Company;
import com.ecep.contract.util.MyDateTimePropertyUtils;

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<CloudYu> {
        /**
     * 云端Id
     */
    private SimpleStringProperty cloudId = new SimpleStringProperty();
    /**
     * 公司
     */
    private SimpleObjectProperty<Company> company = new SimpleObjectProperty<>();
    /**
     * 最后更新日期
     */
    private SimpleObjectProperty<LocalDateTime> latest = new SimpleObjectProperty<>();
    /**
     * Version
     */
    private SimpleIntegerProperty version = new SimpleIntegerProperty();
    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<>();

    @Override
    protected void updateFrom(CloudYu info) {
        super.updateFrom(info);
        update_((CloudYu) info);
    }

    private void update_(CloudYu info) {
        vendorCode.set(info.getExceptionMessage());
        vendorDevelopDate.set(info.getVendorUpdateDate());
        customerDevelopDate.set(info.getCustomerUpdateDate());
        if (info.getCloudLatest() != null) {
            ZoneId zone = ZoneId.systemDefault();
            ZonedDateTime zonedDateTime = info.getCloudLatest().atZone(zone);
            LocalDateTime localDateTime = zonedDateTime.toLocalDateTime();
            cloudLatest.set(localDateTime);
        } else {
            cloudLatest.set(null);
        }
    }

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

    private boolean copyTo_(CloudYu 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;
        }

        Instant latest = MyDateTimePropertyUtils.localDateTimeToInstant(cloudLatest);
        if (!Objects.equals(info.getCloudLatest(), latest)) {
            info.setCloudLatest(latest);
            modified = true;
        }

        return modified;
    }

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


}
