package com.ecep.contract.vm;

import java.util.Objects;

import com.ecep.contract.vo.PurchaseBillVoucherItemVo;

import javafx.beans.property.SimpleDoubleProperty;
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 PurchaseBillVoucherItemViewModel extends IdentityViewModel<PurchaseBillVoucherItemVo> {

    private SimpleIntegerProperty id = new SimpleIntegerProperty();
    private SimpleIntegerProperty refId = new SimpleIntegerProperty();
    /**
     * 关联的采购账单凭证ID
     */
    private SimpleObjectProperty<Integer> voucherId = new SimpleObjectProperty<>();
    /**
     * 关联的采购订单项ID
     */
    private SimpleObjectProperty<Integer> orderItemId = new SimpleObjectProperty<>();
    /**
     * 关联的发票ID
     */
    private SimpleObjectProperty<Integer> invoiceId = new SimpleObjectProperty<>();
    /**
     * 关联的库存ID
     */
    private SimpleObjectProperty<Integer> inventoryId = new SimpleObjectProperty<>();
    /**
     * 关联的合同ID
     */
    private SimpleObjectProperty<Integer> contractId = new SimpleObjectProperty<>();

    private SimpleDoubleProperty quantity = new SimpleDoubleProperty();
    private SimpleDoubleProperty price = new SimpleDoubleProperty();
    private SimpleStringProperty description = new SimpleStringProperty();

    @Override
    protected void updateFrom(PurchaseBillVoucherItemVo v) {
        super.updateFrom(v);
        getId().set(v.getId());
        getRefId().set(v.getRefId());
        getVoucherId().set(v.getVoucherId());
        getOrderItemId().set(v.getOrderItemId());
        getInvoiceId().set(v.getInvoiceId());
        getInventoryId().set(v.getInventoryId());
        getContractId().set(v.getContractId());
        getQuantity().set(v.getQuantity());
        getPrice().set(v.getPrice());
        getDescription().set(v.getDescription());
    }

    @Override
    public boolean copyTo(PurchaseBillVoucherItemVo 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(voucherId.get(), v.getVoucherId())) {
            v.setVoucherId(voucherId.get());
            modified = true;
        }

        if (!Objects.equals(orderItemId.get(), v.getOrderItemId())) {
            v.setOrderItemId(orderItemId.get());
            modified = true;
        }

        if (!Objects.equals(invoiceId.get(), v.getInvoiceId())) {
            v.setInvoiceId(invoiceId.get());
            modified = true;
        }

        if (!Objects.equals(inventoryId.get(), v.getInventoryId())) {
            v.setInventoryId(inventoryId.get());
            modified = true;
        }

        if (!Objects.equals(contractId.get(), v.getContractId())) {
            v.setContractId(contractId.get());
            modified = true;
        }
        if (!Objects.equals(quantity.get(), v.getQuantity())) {
            v.setQuantity(quantity.get());
            modified = true;
        }
        if (!Objects.equals(price.get(), v.getPrice())) {
            v.setPrice(price.get());
            modified = true;
        }
        if (!Objects.equals(description.get(), v.getDescription())) {
            v.setDescription(description.get());
            modified = true;
        }

        return modified;
    }

    public static PurchaseBillVoucherItemViewModel from(PurchaseBillVoucherItemVo item) {
        PurchaseBillVoucherItemViewModel viewModel = new PurchaseBillVoucherItemViewModel();
        viewModel.update(item);
        return viewModel;
    }
}
