package com.ecep.contract.vm;

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

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

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 SalesOrderViewModel extends IdentityViewModel<SalesOrder> {

    SimpleIntegerProperty id = 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<LocalDate> makerDate = new SimpleObjectProperty<>();
    SimpleObjectProperty<LocalDate> verifierDate = new SimpleObjectProperty<>();
    SimpleStringProperty description = new SimpleStringProperty();

    @Override
    protected void updateFrom(SalesOrder v) {
        getId().set(v.getId());
        getContract().set(v.getContract());
        getEmployee().set(v.getEmployee());
        getCode().set(v.getCode());
        getMaker().set(v.getMaker());
        getVerifier().set(v.getVerifier());
        getMakerDate().set(v.getMakerDate());
        getVerifierDate().set(v.getVerifierDate());
        super.updateFrom(v);
    }

    @Override
    public boolean copyTo(SalesOrder v) {
        boolean modified = super.copyTo(v);
        if (!Objects.equals(id.get(), v.getId())) {
            v.setId(id.get());
            modified = true;
        }

        if (!Objects.equals(contract.get(), v.getContract())) {
            v.setContract(contract.get());
            modified = true;
        }

        if (!Objects.equals(employee.get(), v.getEmployee())) {
            v.setEmployee(employee.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(makerDate.get(), v.getMakerDate())) {
            v.setMakerDate(makerDate.get());
            modified = true;
        }
        if (!Objects.equals(verifier.get(), v.getVerifier())) {
            v.setVerifier(verifier.get());
            modified = true;
        }
        if (!Objects.equals(verifierDate.get(), v.getVerifierDate())) {
            v.setVerifierDate(verifierDate.get());
            modified = true;
        }

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

    public static SalesOrderViewModel from(SalesOrder order) {
        SalesOrderViewModel model = new SalesOrderViewModel();
        model.update(order);
        return model;
    }
}
