package com.ecep.contract.vm;

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

import com.ecep.contract.model.Inventory;
import com.ecep.contract.model.PurchaseOrder;
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 PurchaseOrderItemViewModel extends IdentityViewModel<PurchaseOrderItem> {
    /**
     * 关联的销售订单
     */
    private SimpleObjectProperty<PurchaseOrder> order = new SimpleObjectProperty<>();
    private SimpleObjectProperty<Inventory> inventory = new SimpleObjectProperty<>();
    private SimpleIntegerProperty refId = new SimpleIntegerProperty();
    private SimpleDoubleProperty quantity = new SimpleDoubleProperty();
    private SimpleDoubleProperty price = new SimpleDoubleProperty();
    private SimpleDoubleProperty taxRate = new SimpleDoubleProperty();
    private SimpleDoubleProperty exclusiveTaxPrice = new SimpleDoubleProperty();
    private SimpleObjectProperty<LocalDate> arriveDate = new SimpleObjectProperty<>();
    private SimpleStringProperty description = new SimpleStringProperty();


    @Override
    protected void updateFrom(PurchaseOrderItem v) {
        super.updateFrom(v);
        getOrder().set(v.getOrder());
        getInventory().set(v.getInventory());
        getRefId().set(v.getRefId());
        getQuantity().set(v.getQuantity());
        getPrice().set(v.getPrice());
        getTaxRate().set(v.getTaxRate());
        getExclusiveTaxPrice().set(v.getExclusiveTaxPrice());
        getArriveDate().set(v.getArriveDate());
        getDescription().set(v.getDescription());


    }

    @Override
    public boolean copyTo(PurchaseOrderItem v) {
        boolean modified = super.copyTo(v);
        if (!Objects.equals(order.get(), v.getOrder())) {
            v.setOrder(order.get());
            modified = true;
        }
        if (!Objects.equals(inventory.get(), v.getInventory())) {
            v.setInventory(inventory.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(taxRate.get(), v.getTaxRate())) {
            v.setTaxRate(taxRate.get());
            modified = true;
        }
        if (!Objects.equals(price.get(), v.getPrice())) {
            v.setPrice(price.get());
            modified = true;
        }
        if (!Objects.equals(exclusiveTaxPrice.get(), v.getExclusiveTaxPrice())) {
            v.setExclusiveTaxPrice(exclusiveTaxPrice.get());
            modified = true;
        }
        if (!Objects.equals(arriveDate.get(), v.getArriveDate())) {
            v.setArriveDate(arriveDate.get());
            modified = true;
        }
        if (!Objects.equals(description.get(), v.getDescription())) {
            v.setDescription(description.get());
            modified = true;
        }

        return modified;
    }

    public static PurchaseOrderItemViewModel from(PurchaseOrderItem item) {
        PurchaseOrderItemViewModel viewModel = new PurchaseOrderItemViewModel();
        viewModel.update(item);
        return viewModel;
    }
}
