package com.ecep.contract.vm;

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

import com.ecep.contract.vo.PurchaseBillVoucherVo;

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 PurchaseBillVoucherViewModel extends IdentityViewModel<PurchaseBillVoucherVo> {

    SimpleIntegerProperty refId = new SimpleIntegerProperty();
    SimpleStringProperty code = new SimpleStringProperty();
    /**
     * 公司， Company
     */
    SimpleObjectProperty<Integer> companyId = new SimpleObjectProperty<>();
    /**
     * 发票， Invoice
     */
    SimpleObjectProperty<Integer> invoiceId = new SimpleObjectProperty<>();
    SimpleObjectProperty<Integer> employeeId = new SimpleObjectProperty<>();
    SimpleObjectProperty<Integer> makerId = new SimpleObjectProperty<>();
    SimpleObjectProperty<Integer> verifierId = new SimpleObjectProperty<>();
    SimpleObjectProperty<LocalDateTime> makerDate = new SimpleObjectProperty<>();
    SimpleObjectProperty<LocalDateTime> modifyDate = new SimpleObjectProperty<>();
    SimpleObjectProperty<LocalDateTime> verifierDate = new SimpleObjectProperty<>();
    SimpleStringProperty description = new SimpleStringProperty();
    // Keep original employee fields for compatibility
    SimpleObjectProperty<Integer> employee = new SimpleObjectProperty<>();
    SimpleObjectProperty<Integer> maker = new SimpleObjectProperty<>();
    SimpleObjectProperty<Integer> verifier = new SimpleObjectProperty<>();

    @Override
    protected void updateFrom(PurchaseBillVoucherVo v) {
        super.updateFrom(v);
        getRefId().set(v.getRefId());
        getCode().set(v.getCode());
        getCompanyId().set(v.getCompanyId());
        getInvoiceId().set(v.getInvoiceId());
        getEmployeeId().set(v.getEmployeeId());
        getMakerId().set(v.getMakerId());
        getVerifierId().set(v.getVerifierId());
        getMakerDate().set(v.getMakerDate());
        getModifyDate().set(v.getModifyDate());
        getVerifierDate().set(v.getVerifierDate());
        getDescription().set(v.getDescription());

        // Map to original employee fields for compatibility
        if (v.getEmployeeId() != null) {
            getEmployee().set(v.getEmployeeId());
        }
        if (v.getMakerId() != null) {
            getMaker().set(v.getMakerId());
        }
        if (v.getVerifierId() != null) {
            getVerifier().set(v.getVerifierId());
        }
    }

    @Override
    public boolean copyTo(PurchaseBillVoucherVo v) {
        boolean modified = super.copyTo(v);
        if (!Objects.equals(refId.get(), v.getRefId())) {
            v.setRefId(refId.get());
            modified = true;
        }
        if (!Objects.equals(code.get(), v.getCode())) {
            v.setCode(code.get());
            modified = true;
        }
        if (!Objects.equals(companyId.get(), v.getCompanyId())) {
            v.setCompanyId(companyId.get());
            modified = true;
        }
        if (!Objects.equals(invoiceId.get(), v.getInvoiceId())) {
            v.setInvoiceId(invoiceId.get());
            modified = true;
        }
        if (!Objects.equals(employeeId.get(), v.getEmployeeId())) {
            v.setEmployeeId(employeeId.get());
            modified = true;
        }
        if (!Objects.equals(makerId.get(), v.getMakerId())) {
            v.setMakerId(makerId.get());
            modified = true;
        }
        if (!Objects.equals(verifierId.get(), v.getVerifierId())) {
            v.setVerifierId(verifierId.get());
            modified = true;
        }
        if (!Objects.equals(makerDate.get(), v.getMakerDate())) {
            v.setMakerDate(makerDate.get());
            modified = true;
        }
        if (!Objects.equals(modifyDate.get(), v.getModifyDate())) {
            v.setModifyDate(modifyDate.get());
            modified = true;
        }
        if (!Objects.equals(verifierDate.get(), v.getVerifierDate())) {
            v.setVerifierDate(verifierDate.get());
            modified = true;
        }
        if (!Objects.equals(description.get(), v.getDescription())) {
            v.setDescription(description.get());
            modified = true;
        }
        return modified;
    }

    public static PurchaseBillVoucherViewModel from(PurchaseBillVoucherVo voucher) {
        PurchaseBillVoucherViewModel model = new PurchaseBillVoucherViewModel();
        model.update(voucher);
        return model;
    }
}
