package com.ecep.contract.vm;

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

import com.ecep.contract.model.CompanyContact;

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 CompanyContactViewModel extends IdentityViewModel<CompanyContact> {
    private SimpleIntegerProperty id = new SimpleIntegerProperty();
    private SimpleStringProperty name = new SimpleStringProperty();
    private SimpleStringProperty position = new SimpleStringProperty();
    private SimpleStringProperty phone = new SimpleStringProperty();
    private SimpleStringProperty email = new SimpleStringProperty();
    private SimpleStringProperty address = new SimpleStringProperty();
    private SimpleStringProperty u8Code = new SimpleStringProperty();
    private SimpleStringProperty memo = new SimpleStringProperty();
    private SimpleIntegerProperty version = new SimpleIntegerProperty();
    private SimpleObjectProperty<LocalDate> created = new SimpleObjectProperty<>();

    public static CompanyContactViewModel from(CompanyContact contact) {
        CompanyContactViewModel model = new CompanyContactViewModel();
        model.update(contact);
        return model;
    }


    public void updateFrom(CompanyContact v) {
        id.set(v.getId());
        name.set(v.getName());
        position.set(v.getPosition());
        phone.set(v.getPhone());
        email.set(v.getEmail());
        address.set(v.getAddress());
        u8Code.set(v.getU8Code());
        created.set(v.getCreated());
        memo.set(v.getMemo());
        version.set(v.getVersion());
    }

    public boolean copyTo(CompanyContact v) {
        boolean modified = super.copyTo(v);
        if (!Objects.equals(id.get(), v.getId())) {
            v.setId(id.get());
            modified = true;
        }
        if (!Objects.equals(name.get(), v.getName())) {
            v.setName(name.get());
            modified = true;
        }
        if (!Objects.equals(position.get(), v.getPosition())) {
            v.setPosition(position.get());
            modified = true;
        }
        if (!Objects.equals(phone.get(), v.getPhone())) {
            v.setPhone(phone.get());
            modified = true;
        }
        if (!Objects.equals(email.get(), v.getEmail())) {
            v.setEmail(email.get());
            modified = true;
        }
        if (!Objects.equals(address.get(), v.getAddress())) {
            v.setAddress(address.get());
            modified = true;
        }
        if (!Objects.equals(u8Code.get(), v.getU8Code())) {
            v.setU8Code(u8Code.get());
            modified = true;
        }
        if (!Objects.equals(created.get(), v.getCreated())) {
            v.setCreated(created.get());
            modified = true;
        }
        if (!Objects.equals(memo.get(), v.getMemo())) {
            v.setMemo(memo.get());
            modified = true;
        }
        return modified;
    }
}
