package com.ecep.contract.vm;

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

import com.ecep.contract.model.SalesOrder;
import com.ecep.contract.model.SalesOrderItem;

import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;

@Data
@EqualsAndHashCode(callSuper = false)
public class SalesOrderItemViewModel extends IdentityViewModel<SalesOrderItem> {
    /**
     * 关联的销售订单
     */
    private SimpleObjectProperty<SalesOrder> order = new SimpleObjectProperty<>();

    private SimpleStringProperty code = new SimpleStringProperty();
    private SimpleStringProperty name = new SimpleStringProperty();
    private SimpleDoubleProperty quantity = new SimpleDoubleProperty();
    private SimpleDoubleProperty price = new SimpleDoubleProperty();
    private SimpleDoubleProperty taxRate = new SimpleDoubleProperty();
    private SimpleDoubleProperty exclusiveTaxPrice = new SimpleDoubleProperty();
    private SimpleObjectProperty<LocalDate> startDate = new SimpleObjectProperty<>();
    private SimpleObjectProperty<LocalDate> endDate = new SimpleObjectProperty<>();
    private SimpleStringProperty description = new SimpleStringProperty();

    @Override
    protected void updateFrom(SalesOrderItem v) {
        super.updateFrom(v);
        getOrder().set(v.getOrder());
        getCode().set(v.getCode());
        getName().set(v.getName());
        getQuantity().set(v.getQuantity());
        getPrice().set(v.getPrice());
        getTaxRate().set(v.getTaxRate());
        getExclusiveTaxPrice().set(v.getExclusiveTaxPrice());
        getStartDate().set(v.getStartDate());
        getEndDate().set(v.getEndDate());
        getDescription().set(v.getDescription());
    }

    @Override
    public boolean copyTo(SalesOrderItem v) {
        boolean modified = super.copyTo(v);
        if (!Objects.equals(order.get(), v.getOrder())) {
            v.setOrder(order.get());
            modified = true;
        }
        if (!Objects.equals(code.get(), v.getCode())) {
            v.setCode(code.get());
            modified = true;
        }
        if (!Objects.equals(name.get(), v.getName())) {
            v.setName(name.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(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(startDate.get(), v.getStartDate())) {
            v.setStartDate(startDate.get());
            modified = true;
        }
        if (!Objects.equals(endDate.get(), v.getEndDate())) {
            v.setEndDate(endDate.get());
            modified = true;
        }
        if (!Objects.equals(description.get(), v.getDescription())) {
            v.setDescription(description.get());
            modified = true;
        }

        return modified;
    }

    public static SalesOrderItemViewModel from(SalesOrderItem item) {
        SalesOrderItemViewModel viewModel = new SalesOrderItemViewModel();
        // viewModel.updateFrom(item);
        viewModel.update(item);
        return viewModel;
    }
}
