package com.ecep.contract.vm;

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

import com.ecep.contract.model.Contract;
import com.ecep.contract.model.Employee;
import com.ecep.contract.model.PurchaseOrder;

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 PurchaseOrderViewModel extends IdentityViewModel<PurchaseOrder> {
    SimpleIntegerProperty refId = new SimpleIntegerProperty();
    SimpleObjectProperty<Contract> contract = new SimpleObjectProperty<>();
    SimpleObjectProperty<Employee> employee = new SimpleObjectProperty<>();
    SimpleStringProperty code = new SimpleStringProperty();
    SimpleObjectProperty<Employee> maker = new SimpleObjectProperty<>();
    SimpleObjectProperty<Employee> verifier = new SimpleObjectProperty<>();
    SimpleObjectProperty<Employee> closer = new SimpleObjectProperty<>();
    SimpleObjectProperty<LocalDateTime> makerDate = new SimpleObjectProperty<>();
    SimpleObjectProperty<LocalDateTime> modifyDate = new SimpleObjectProperty<>();
    SimpleObjectProperty<LocalDateTime> verifierDate = new SimpleObjectProperty<>();
    SimpleObjectProperty<LocalDateTime> closerDate = new SimpleObjectProperty<>();
    SimpleStringProperty description = new SimpleStringProperty();

    @Override
    protected void updateFrom(PurchaseOrder v) {
        super.updateFrom(v);
        getRefId().set(v.getRefId());
        getContract().set(v.getContract());
        getEmployee().set(v.getEmployee());
        getCode().set(v.getCode());
        getMaker().set(v.getMaker());
        getVerifier().set(v.getVerifier());
        getCloser().set(v.getCloser());
        getMakerDate().set(v.getMakerDate());
        getModifyDate().set(v.getModifyDate());
        getVerifierDate().set(v.getVerifierDate());
        getCloserDate().set(v.getCloserDate());
        getDescription().set(v.getDescription());
        super.updateFrom(v);
    }

    @Override
    public boolean copyTo(PurchaseOrder v) {
        boolean modified = super.copyTo(v);
        if (!Objects.equals(refId.get(), v.getRefId())) {
            v.setRefId(refId.get());
            modified = true;
        }
        if (!Objects.equals(contract.get(), v.getContract())) {
            v.setContract(contract.get());
            modified = true;
        }

        if (!Objects.equals(code.get(), v.getCode())) {
            v.setCode(code.get());
            modified = true;
        }
        if (!Objects.equals(maker.get(), v.getMaker())) {
            v.setMaker(maker.get());
            modified = true;
        }
        if (!Objects.equals(verifier.get(), v.getVerifier())) {
            v.setVerifier(verifier.get());
            modified = true;
        }
        if (!Objects.equals(closer.get(), v.getCloser())) {
            v.setCloser(closer.get());
            modified = true;
        }
        if (!Objects.equals(makerDate.get(), v.getMakerDate())) {
            v.setMakerDate(makerDate.get());
            modified = true;
        }
        if (!Objects.equals(modifyDate.get(), v.getModifyDate())) {
            v.setModifyDate(modifyDate.get());
            modified = true;
        }
        if (!Objects.equals(verifierDate.get(), v.getVerifierDate())) {
            v.setVerifierDate(verifierDate.get());
            modified = true;
        }
        if (!Objects.equals(closerDate.get(), v.getCloserDate())) {
            v.setCloserDate(closerDate.get());
            modified = true;
        }

        if (!Objects.equals(description.get(), v.getDescription())) {
            v.setDescription(description.get());
            modified = true;
        }
        return modified;
    }

    public static PurchaseOrderViewModel from(PurchaseOrder order) {
        PurchaseOrderViewModel model = new PurchaseOrderViewModel();
        model.update(order);
        return model;
    }
}
