package com.ecep.contract.vm;

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

import com.ecep.contract.vo.CloudTycVo;

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

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

    private void update_(CloudTycVo info) {
        score.set(info.getScore());

        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(CloudTycVo info) {
        boolean modified = super.copyTo(info);
        if (copyTo_((CloudTycVo) info)) {
            modified = true;
        }
        return modified;
    }

    private boolean copyTo_(CloudTycVo info) {
        boolean modified = super.copyTo(info);
        if (!Objects.equals(info.getId(), getId().get())) {
            info.setId(getId().get());
            modified = true;
        }

        if (!Objects.equals(info.getScore(), getScore().get())) {
            info.setScore(getScore().get());
            modified = true;
        }

        if (!Objects.equals(info.getCloudLatest(), cloudLatest.get())) {
            info.setCloudLatest(cloudLatest.get());
            modified = true;
        }

        return modified;
    }

    public static CloudTycInfoViewModel from(CloudTycVo cc) {
        CloudTycInfoViewModel model = new CloudTycInfoViewModel();
        model.update(cc);
        return model;
    }
}
