package com.ecep.contract.vm;

import java.util.Objects;

import com.ecep.contract.model.Contract;
import com.ecep.contract.model.Inventory;
import com.ecep.contract.model.Invoice;
import com.ecep.contract.model.PurchaseBillVoucher;
import com.ecep.contract.model.PurchaseBillVoucherItem;
import com.ecep.contract.model.PurchaseOrderItem;

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<PurchaseBillVoucherItem> {

    private SimpleIntegerProperty id = new SimpleIntegerProperty();
    private SimpleIntegerProperty refId = new SimpleIntegerProperty();
    /**
     * 关联的销售订单
     */
    private SimpleObjectProperty<PurchaseBillVoucher> voucher = new SimpleObjectProperty<>();
    private SimpleObjectProperty<PurchaseOrderItem> orderItem = new SimpleObjectProperty<>();
    private SimpleObjectProperty<Invoice> invoice = new SimpleObjectProperty<>();
    private SimpleObjectProperty<Inventory> inventory = new SimpleObjectProperty<>();
    private SimpleObjectProperty<Contract> contract = new SimpleObjectProperty<>();

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

    @Override
    protected void updateFrom(PurchaseBillVoucherItem v) {
        super.updateFrom(v);
        getId().set(v.getId());
        getRefId().set(v.getRefId());
        getVoucher().set(v.getVoucher());
        getOrderItem().set(v.getOrderItem());
        getInvoice().set(v.getInvoice());
        getInventory().set(v.getInventory());
        getContract().set(v.getContract());
        getQuantity().set(v.getQuantity());
        getPrice().set(v.getPrice());
        getDescription().set(v.getDescription());
    }

    @Override
    public boolean copyTo(PurchaseBillVoucherItem v) {
        boolean modified = super.copyTo(v);

        if (!Objects.equals(id.get(), v.getId())) {
            v.setId(id.get());
            modified = true;
        }
        if (!Objects.equals(voucher.get(), v.getVoucher())) {
            v.setVoucher(voucher.get());
            modified = true;
        }
        if (!Objects.equals(orderItem.get(), v.getOrderItem())) {
            v.setOrderItem(orderItem.get());
            modified = true;
        }
        if (!Objects.equals(invoice.get(), v.getInvoice())) {
            v.setInvoice(invoice.get());
            modified = true;
        }
        if (!Objects.equals(inventory.get(), v.getInventory())) {
            v.setInventory(inventory.get());
            modified = true;
        }
        if (!Objects.equals(contract.get(), v.getContract())) {
            v.setContract(contract.get());
            modified = true;
        }
        if (!Objects.equals(refId.get(), v.getRefId())) {
            v.setRefId(refId.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(PurchaseBillVoucherItem item) {
        PurchaseBillVoucherItemViewModel viewModel = new PurchaseBillVoucherItemViewModel();
        viewModel.update(item);
        return viewModel;
    }
}
