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.model.Company;
import com.ecep.contract.model.CompanyContact;
import com.ecep.contract.model.CompanyCustomer;

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 CompanyCustomerViewModel extends IdentityViewModel<CompanyCustomer> {
    private SimpleStringProperty state = new SimpleStringProperty();
    /**
     * 关联的企业
     */
    private SimpleObjectProperty<Company> company = new SimpleObjectProperty<>();
    /**
     * 发展日期
     */
    private SimpleObjectProperty<LocalDate> developDate = new SimpleObjectProperty<>();
    /**
     * 文件夹
     */
    private SimpleStringProperty path = new SimpleStringProperty();
    /**
     * 关联联系人
     */
    private SimpleObjectProperty<CompanyContact> contact = new SimpleObjectProperty<>();

    private SimpleStringProperty description = new SimpleStringProperty();
    private SimpleObjectProperty<LocalDateTime> created = new SimpleObjectProperty<>();
    private SimpleIntegerProperty version = new SimpleIntegerProperty();

    public static CompanyCustomerViewModel from(CompanyCustomer cc) {
        CompanyCustomerViewModel model = new CompanyCustomerViewModel();
        model.update(cc);
        return model;
    }

    public void updateFrom(CompanyCustomer c) {
        super.updateFrom(c);
        getCompany().set(c.getCompany());
        getDevelopDate().set(c.getDevelopDate());
        getContact().set(c.getContact());
        getPath().set(c.getPath());
        getDescription().set(c.getDescription());

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

    public boolean copyTo(CompanyCustomer v) {
        boolean modified = super.copyTo(v);
        if (!Objects.equals(company.get(), v.getCompany())) {
            v.setCompany(company.get());
            modified = true;
        }
        if (!Objects.equals(contact.get(), v.getContact())) {
            v.setContact(contact.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(description.get(), v.getDescription())) {
            v.setDescription(description.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;
    }
}


