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.*;
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 vendorCode = new SimpleStringProperty();
    SimpleDoubleProperty totalAmount = new SimpleDoubleProperty();
    SimpleDoubleProperty taxAmount = new SimpleDoubleProperty();
    SimpleFloatProperty taxRate = new SimpleFloatProperty();
    SimpleBooleanProperty taxRateLocked = 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());
        getVendorCode().set(v.getVendorCode());
        //
        getTotalAmount().set(v.getTotalAmount() != null ? v.getTotalAmount() : 0.0);
        getTaxAmount().set(v.getTaxAmount() != null ? v.getTaxAmount() : 0.0);
        getTaxRate().set(v.getTaxRate());
        taxRateLocked.set(v.isTaxRateLocked());

        // 保留原有的员工相关字段设置
        employee.set(v.getEmployeeId());
        maker.set(v.getEmployeeId());
        verifier.set(v.getVerifierId());
        closer.set(v.getCloserId());
        makerDate.set(v.getMakerDate());
        modifyDate.set(v.getModifyDate());
        verifierDate.set(v.getVerifierDate());
        closerDate.set(v.getCloserDate());
        description.set(v.getDescription());
    }

    @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(vendorCode.get(), v.getVendorCode())) {
            v.setVendorCode(vendorCode.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(taxRateLocked.get(), v.isTaxRateLocked())) {
            v.setTaxRateLocked(taxRateLocked.get());
            modified = true;
        }

        if (!Objects.equals(getEmployee().get(), v.getEmployeeId())) {
            v.setEmployeeId(getEmployee().get());
            modified = true;
        }

        if (!Objects.equals(getMaker().get(), v.getMakerId())) {
            v.setMakerId(getMaker().get());
            modified = true;
        }

        if (!Objects.equals(getVerifier().get(), v.getVerifierId())) {
            v.setVerifierId(getVerifier().get());
            modified = true;
        }

        if (!Objects.equals(getCloser().get(), v.getCloserId())) {
            v.setCloserId(getCloser().get());
            modified = true;
        }


        if (!Objects.equals(getMakerDate().get(), v.getMakerDate())) {
            v.setMakerDate(getMakerDate().get());
            modified = true;
        }


        if (!Objects.equals(getModifyDate().get(), v.getModifyDate())) {
            v.setModifyDate(getModifyDate().get());
            modified = true;
        }

        if (!Objects.equals(getCloserDate().get(), v.getCloserDate())) {
            v.setCloserDate(getCloserDate().get());
            modified = true;
        }

        if (!Objects.equals(description.get(), v.getDescription())) {
            v.setDescription(description.get());
            modified = true;
        }
        return modified;
    }

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