package com.ecep.contract.vm;

import com.ecep.contract.vo.SalesBillVoucherVo;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;

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

@Data
@EqualsAndHashCode(callSuper = false)
public class SalesBillVoucherViewModel extends IdentityViewModel<SalesBillVoucherVo> {

    SimpleIntegerProperty id = new SimpleIntegerProperty();
    SimpleIntegerProperty refId = new SimpleIntegerProperty();
    SimpleStringProperty code = new SimpleStringProperty();
    SimpleObjectProperty<Integer> company = new SimpleObjectProperty<>();
    /**
     * SaleOrder
     */
    SimpleObjectProperty<Integer> order = new SimpleObjectProperty<>();
    SimpleObjectProperty<Integer> employee = new SimpleObjectProperty<>();
    SimpleObjectProperty<Integer> maker = new SimpleObjectProperty<>();
    SimpleObjectProperty<Integer> verifier = new SimpleObjectProperty<>();
    SimpleObjectProperty<LocalDateTime> makerDate = new SimpleObjectProperty<>();
    SimpleObjectProperty<LocalDateTime> modifyDate = new SimpleObjectProperty<>();
    SimpleObjectProperty<LocalDateTime> verifierDate = new SimpleObjectProperty<>();
    SimpleStringProperty description = new SimpleStringProperty();

    @Override
    protected void updateFrom(SalesBillVoucherVo v) {
        getId().set(v.getId());
        getRefId().set(v.getRefId());
        getCode().set(v.getCode());
        getCompany().set(v.getCompanyId());
        getOrder().set(v.getOrderId());
        getEmployee().set(v.getEmployeeId());
        getMaker().set(v.getMakerId());
        getVerifier().set(v.getVerifierId());
        getMakerDate().set(v.getMakerDate());
        getModifyDate().set(v.getModifyDate());
        getVerifierDate().set(v.getVerifierDate());
        getDescription().set(v.getDescription());
        super.updateFrom(v);
    }

    @Override
    public boolean copyTo(SalesBillVoucherVo v) {
        boolean modified = super.copyTo(v);
        if (!Objects.equals(id.get(), v.getId())) {
            v.setId(id.get());
            modified = true;
        }
        if (!Objects.equals(refId.get(), v.getRefId())) {
            v.setRefId(refId.get());
            modified = true;
        }
        if (!Objects.equals(code.get(), v.getCode())) {
            v.setCode(code.get());
            modified = true;
        }
        if (!Objects.equals(company.get(), v.getCompanyId())) {
            v.setCompanyId(company.get());
            modified = true;
        }
        if (!Objects.equals(order.get(), v.getOrderId())) {
            v.setOrderId(order.get());
            modified = true;
        }
        if (!Objects.equals(employee.get(), v.getEmployeeId())) {
            v.setEmployeeId(employee.get());
            modified = true;
        }
        if (!Objects.equals(maker.get(), v.getMakerId())) {
            v.setMakerId(maker.get());
            modified = true;
        }
        if (!Objects.equals(verifier.get(), v.getVerifierId())) {
            v.setVerifierId(verifier.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(description.get(), v.getDescription())) {
            v.setDescription(description.get());
            modified = true;
        }
        return modified;
    }

    public static SalesBillVoucherViewModel from(SalesBillVoucherVo order) {
        SalesBillVoucherViewModel model = new SalesBillVoucherViewModel();
        model.update(order);
        return model;
    }
}
