package com.ecep.contract.vm;

import java.util.Objects;

import com.ecep.contract.model.Contract;
import com.ecep.contract.model.ExtendVendorInfo;
import com.ecep.contract.model.VendorGroup;

import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleObjectProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;

@Data
@EqualsAndHashCode(callSuper = false)
public class ExtendVendorInfoViewModel extends IdentityViewModel<ExtendVendorInfo> {
    /**
     * 关联的合同
     */
    private SimpleObjectProperty<Contract> contract = new SimpleObjectProperty<>();
    /**
     * 供应商分组
     */
    private SimpleObjectProperty<VendorGroup> group = new SimpleObjectProperty<>();

    /**
     * 合同序号
     */
    private SimpleIntegerProperty codeSequenceNumber = new SimpleIntegerProperty();

    /**
     * 是否已指定供应商
     */
    private SimpleBooleanProperty assignedProvider = new SimpleBooleanProperty();
    /**
     * 是否是预采购
     */
    private SimpleBooleanProperty prePurchase = new SimpleBooleanProperty();

    public static ExtendVendorInfoViewModel from(ExtendVendorInfo v) {
        ExtendVendorInfoViewModel vm = new ExtendVendorInfoViewModel();
        vm.updateFrom(v);
        return vm;
    }

    @Override
    protected void updateFrom(ExtendVendorInfo v) {
        super.updateFrom(v);
        getContract().set(v.getContract());
        getGroup().set(v.getGroup());
        getCodeSequenceNumber().set(v.getCodeSequenceNumber());
        getAssignedProvider().set(v.isAssignedProvider());
        getPrePurchase().set(v.isPrePurchase());
    }

    @Override
    public boolean copyTo(ExtendVendorInfo v) {
        boolean modified = super.copyTo(v);
        if (!Objects.equals(contract.get(), v.getContract())) {
            v.setContract(contract.get());
            modified = true;
        }
        if (!Objects.equals(group.get(), v.getGroup())) {
            v.setGroup(group.get());
            modified = true;
        }
        if (!Objects.equals(codeSequenceNumber.get(), v.getCodeSequenceNumber())) {
            v.setCodeSequenceNumber(codeSequenceNumber.get());
            modified = true;
        }
        if (!Objects.equals(assignedProvider.get(), v.isAssignedProvider())) {
            v.setAssignedProvider(assignedProvider.get());
            modified = true;
        }
        if (!Objects.equals(prePurchase.get(), v.isPrePurchase())) {
            v.setPrePurchase(prePurchase.get());
            modified = true;
        }
        return modified;
    }
}
