package com.ecep.contract.vm;

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

import com.ecep.contract.util.NumberUtils;
import com.ecep.contract.vo.ContractInvoiceVo;

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

@Data
@EqualsAndHashCode(callSuper = false)
public class ContractInvoiceViewModel extends IdentityViewModel<ContractInvoiceVo> {
    private final SimpleStringProperty code = new SimpleStringProperty();
    private final SimpleStringProperty name = new SimpleStringProperty();
    /**
     * 关联的合同对象, Contract
     */
    private final SimpleObjectProperty<Integer> contract = new SimpleObjectProperty<>();
    /**
     * 关联的发票对象, Invoice
     */
    private final SimpleObjectProperty<Integer> invoice = new SimpleObjectProperty<>();
    /**
     * 发票金额
     */
    private final SimpleObjectProperty<Double> amount = new SimpleObjectProperty<>();
    /**
     * 备注
     */
    private final SimpleStringProperty remark = new SimpleStringProperty();
    private final SimpleObjectProperty<LocalDate> createDate = new SimpleObjectProperty<>();
    private final SimpleObjectProperty<LocalDate> updateDate = new SimpleObjectProperty<>();
    private final SimpleObjectProperty<Integer> creator = new SimpleObjectProperty<>();
    private final SimpleObjectProperty<Integer> updater = new SimpleObjectProperty<>();

    @Override
    protected void updateFrom(ContractInvoiceVo v) {
        super.updateFrom(v);
        code.set(v.getCode());
        name.set(v.getName());
        contract.set(v.getContractId());
        invoice.set(v.getInvoiceId());
        amount.set(v.getAmount() != null ? v.getAmount() : 0.0);
        remark.set(v.getRemark());

        updateDate.set(v.getUpdateDate());
        creator.set(v.getSetupPersonId());
        updater.set(v.getUpdatePersonId());
    }

    @Override
    public boolean copyTo(ContractInvoiceVo v) {
        boolean modified = super.copyTo(v);

        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(contract.get(), v.getContractId())) {
            v.setContractId(contract.get());
            modified = true;
        }

        if (!Objects.equals(invoice.get(), v.getInvoiceId())) {
            v.setInvoiceId(invoice.get());
            modified = true;
        }

        if (!NumberUtils.equals(amount.get(), v.getAmount())) {
            v.setAmount(amount.get());
            modified = true;
        }

        if (!Objects.equals(remark.get(), v.getRemark())) {
            v.setRemark(remark.get());
            modified = true;
        }
        if (!Objects.equals(creator.get(), v.getSetupPersonId())) {
            v.setSetupPersonId(creator.get());
            modified = true;
        }
        if (!Objects.equals(updater.get(), v.getUpdatePersonId())) {
            v.setUpdatePersonId(updater.get());
            modified = true;
        }
        if (!Objects.equals(createDate.get(), v.getSetupDate())) {
            v.setSetupDate(createDate.get());
            modified = true;
        }
        if (!Objects.equals(updateDate.get(), v.getUpdateDate())) {
            v.setUpdateDate(updateDate.get());
            modified = true;
        }

        return modified;
    }

    /**
     * 从VO对象创建ViewModel实例
     * 
     * @param vo 发票VO对象
     * @return 发票ViewModel实例
     */
    public static ContractInvoiceViewModel from(ContractInvoiceVo vo) {
        ContractInvoiceViewModel viewModel = new ContractInvoiceViewModel();
        viewModel.updateFrom(vo);
        return viewModel;
    }
}