package com.ecep.contract.vm;

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

import com.ecep.contract.vo.PurchaseOrderVo;

import javafx.beans.property.SimpleBooleanProperty;
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 PurchaseOrderViewModel extends IdentityViewModel<PurchaseOrderVo> {
    SimpleIntegerProperty refId = new SimpleIntegerProperty();
    SimpleObjectProperty<Integer> contractId = new SimpleObjectProperty<>();
    SimpleStringProperty code = new SimpleStringProperty();
    SimpleStringProperty name = new SimpleStringProperty();
    SimpleObjectProperty<Integer> vendorId = new SimpleObjectProperty<>();
    SimpleObjectProperty<LocalDate> orderDate = new SimpleObjectProperty<>();
    SimpleDoubleProperty totalAmount = new SimpleDoubleProperty();
    SimpleDoubleProperty taxAmount = new SimpleDoubleProperty();
    SimpleDoubleProperty taxRate = new SimpleDoubleProperty();
    SimpleIntegerProperty statusId = new SimpleIntegerProperty();
    SimpleStringProperty remark = new SimpleStringProperty();
    SimpleBooleanProperty active = new SimpleBooleanProperty();
    // 保留原有的员工相关字段
    SimpleObjectProperty<Integer> employee = new SimpleObjectProperty<>();
    SimpleObjectProperty<Integer> maker = new SimpleObjectProperty<>();
    SimpleObjectProperty<Integer> verifier = new SimpleObjectProperty<>();
    SimpleObjectProperty<Integer> closer = new SimpleObjectProperty<>();
    SimpleObjectProperty<LocalDateTime> makerDate = new SimpleObjectProperty<>();
    SimpleObjectProperty<LocalDateTime> modifyDate = new SimpleObjectProperty<>();
    SimpleObjectProperty<LocalDateTime> verifierDate = new SimpleObjectProperty<>();
    SimpleObjectProperty<LocalDateTime> closerDate = new SimpleObjectProperty<>();
    SimpleStringProperty description = new SimpleStringProperty();

    @Override
    protected void updateFrom(PurchaseOrderVo v) {
        super.updateFrom(v);
        getRefId().set(v.getId());
        if (v.getContractId() != null) {
            getContractId().set(v.getContractId());
        }
        getCode().set(v.getCode());
        getName().set(v.getName());
        if (v.getVendorId() != null) {
            getVendorId().set(v.getVendorId());
        }
        getOrderDate().set(v.getOrderDate());
        getTotalAmount().set(v.getTotalAmount() != null ? v.getTotalAmount() : 0.0);
        getTaxAmount().set(v.getTaxAmount() != null ? v.getTaxAmount() : 0.0);
        getTaxRate().set(v.getTaxRate() != null ? v.getTaxRate() : 0.0);
        if (v.getStatusId() != null) {
            getStatusId().set(v.getStatusId());
        }
        getRemark().set(v.getRemark());
        getActive().set(v.getActive() != null ? v.getActive() : false);
        
        // 保留原有的员工相关字段设置
        if (getEmployee() != null && getEmployee().get() != null) {
            getEmployee().set(getEmployee().get());
        }
        if (getMaker() != null && getMaker().get() != null) {
            getMaker().set(getMaker().get());
        }
        if (getVerifier() != null && getVerifier().get() != null) {
            getVerifier().set(getVerifier().get());
        }
        if (getCloser() != null && getCloser().get() != null) {
            getCloser().set(getCloser().get());
        }
        if (getMakerDate() != null && getMakerDate().get() != null) {
            getMakerDate().set(getMakerDate().get());
        }
        if (getModifyDate() != null && getModifyDate().get() != null) {
            getModifyDate().set(getModifyDate().get());
        }
        if (getVerifierDate() != null && getVerifierDate().get() != null) {
            getVerifierDate().set(getVerifierDate().get());
        }
        if (getCloserDate() != null && getCloserDate().get() != null) {
            getCloserDate().set(getCloserDate().get());
        }
        if (getDescription() != null && getDescription().get() != null) {
            getDescription().set(getDescription().get());
        }
    }

    @Override
    public boolean copyTo(PurchaseOrderVo v) {
        boolean modified = super.copyTo(v);
        if (!Objects.equals(refId.get(), v.getId())) {
            v.setId(refId.get());
            modified = true;
        }
        if (!Objects.equals(contractId.get(), v.getContractId())) {
            v.setContractId(contractId.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(vendorId.get(), v.getVendorId())) {
            v.setVendorId(vendorId.get());
            modified = true;
        }
        if (!Objects.equals(orderDate.get(), v.getOrderDate())) {
            v.setOrderDate(orderDate.get());
            modified = true;
        }
        if (!Objects.equals(totalAmount.get(), v.getTotalAmount())) {
            v.setTotalAmount(totalAmount.get());
            modified = true;
        }
        if (!Objects.equals(taxAmount.get(), v.getTaxAmount())) {
            v.setTaxAmount(taxAmount.get());
            modified = true;
        }
        if (!Objects.equals(taxRate.get(), v.getTaxRate())) {
            v.setTaxRate(taxRate.get());
            modified = true;
        }
        if (!Objects.equals(statusId.get(), v.getStatusId())) {
            v.setStatusId(statusId.get());
            modified = true;
        }
        if (!Objects.equals(remark.get(), v.getRemark())) {
            v.setRemark(remark.get());
            modified = true;
        }
        if (!Objects.equals(active.get(), v.getActive())) {
            v.setActive(active.get());
            modified = true;
        }
        // 保留原有的员工相关字段复制
        if (!Objects.equals(description.get(), v.getRemark())) {
            v.setRemark(description.get());
            modified = true;
        }
        return modified;
    }

    public static PurchaseOrderViewModel from(PurchaseOrderVo order) {
        PurchaseOrderViewModel model = new PurchaseOrderViewModel();
        model.update(order);
        return model;
    }
}
