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 com.ecep.contract.vo.SalesOrderVo;
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<SalesOrderVo> {

    SimpleIntegerProperty id = new SimpleIntegerProperty();
    SimpleObjectProperty<Integer> contract = new SimpleObjectProperty<>();
    SimpleObjectProperty<Integer> employee = new SimpleObjectProperty<>();
    SimpleStringProperty code = new SimpleStringProperty();
    SimpleObjectProperty<Integer> maker = new SimpleObjectProperty<>();
    SimpleObjectProperty<Integer> verifier = new SimpleObjectProperty<>();
    SimpleObjectProperty<LocalDate> makerDate = new SimpleObjectProperty<>();
    SimpleObjectProperty<LocalDate> verifierDate = new SimpleObjectProperty<>();
    SimpleStringProperty description = new SimpleStringProperty();

    @Override
    protected void updateFrom(SalesOrderVo v) {
        getId().set(v.getId());
        getContract().set(v.getContractId());
        getEmployee().set(v.getEmployeeId());
        getCode().set(v.getCode());
        getMaker().set(v.getMakerId());
        getVerifier().set(v.getVerifierId());
        getMakerDate().set(v.getMakerDate());
        getVerifierDate().set(v.getVerifierDate());
        super.updateFrom(v);
    }

    @Override
    public boolean copyTo(SalesOrderVo 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.getContractId())) {
            v.setContractId(contract.get());
            modified = true;
        }

        if (!Objects.equals(employee.get(), v.getEmployeeId())) {
            v.setEmployeeId(employee.get());
            modified = true;
        }

        if (!Objects.equals(code.get(), v.getCode())) {
            v.setCode(code.get());
            modified = true;
        }
        if (!Objects.equals(maker.get(), v.getMakerId())) {
            v.setMakerId(maker.get());
            modified = true;
        }
        if (!Objects.equals(makerDate.get(), v.getMakerDate())) {
            v.setMakerDate(makerDate.get());
            modified = true;
        }
        if (!Objects.equals(verifier.get(), v.getVerifierId())) {
            v.setVerifierId(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(SalesOrderVo order) {
        SalesOrderViewModel model = new SalesOrderViewModel();
        model.update(order);
        return model;
    }
}
