package com.ecep.contract.vm;

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

import com.ecep.contract.util.NumberUtils;
import com.ecep.contract.vo.SalesOrderVo;
import javafx.beans.property.SimpleFloatProperty;
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 SalesOrderViewModel extends IdentityViewModel<SalesOrderVo> {

    SimpleIntegerProperty id = new SimpleIntegerProperty();
    SimpleObjectProperty<Integer> contract = new SimpleObjectProperty<>();
    SimpleObjectProperty<Integer> employee = new SimpleObjectProperty<>();
    SimpleStringProperty code = new SimpleStringProperty();
    SimpleObjectProperty<Integer> maker = new SimpleObjectProperty<>();
    SimpleObjectProperty<Integer> verifier = new SimpleObjectProperty<>();
    SimpleObjectProperty<LocalDate> makerDate = new SimpleObjectProperty<>();
    SimpleObjectProperty<LocalDate> verifierDate = new SimpleObjectProperty<>();
    SimpleStringProperty description = new SimpleStringProperty();

    // 添加缺失的字段
    SimpleIntegerProperty refId = new SimpleIntegerProperty();
    SimpleFloatProperty taxRate = new SimpleFloatProperty();
    SimpleObjectProperty<Integer> customer = new SimpleObjectProperty<>();
    SimpleStringProperty customerAddress = new SimpleStringProperty();
    SimpleObjectProperty<Integer> modifier = new SimpleObjectProperty<>();
    SimpleObjectProperty<LocalDate> modifierDate = new SimpleObjectProperty<>();
    SimpleObjectProperty<Integer> closer = new SimpleObjectProperty<>();
    SimpleObjectProperty<LocalDate> closerDate = new SimpleObjectProperty<>();

    @Override
    protected void updateFrom(SalesOrderVo v) {
        getId().set(v.getId());
        getContract().set(v.getContractId());
        getEmployee().set(v.getEmployeeId());
        getCode().set(v.getCode());
        getMaker().set(v.getMakerId());
        getVerifier().set(v.getVerifierId());
        getMakerDate().set(v.getMakerDate());
        getVerifierDate().set(v.getVerifierDate());
        getDescription().set(v.getDescription());

        // 更新添加的字段
        getRefId().set(v.getRefId());
        getTaxRate().set(v.getTaxRate());
        getCustomer().set(v.getCustomerId());
        getCustomerAddress().set(v.getCustomerAddress());
        getModifier().set(v.getModifierId());
        getModifierDate().set(v.getModifierDate());
        getCloser().set(v.getCloserId());
        getCloserDate().set(v.getCloserDate());

        super.updateFrom(v);
    }

    @Override
    public boolean copyTo(SalesOrderVo v) {
        boolean modified = super.copyTo(v);
        if (!Objects.equals(id.get(), v.getId())) {
            v.setId(id.get());
            modified = true;
        }

        if (!Objects.equals(contract.get(), v.getContractId())) {
            v.setContractId(contract.get());
            modified = true;
        }

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

        if (!Objects.equals(code.get(), v.getCode())) {
            v.setCode(code.get());
            modified = true;
        }
        if (!Objects.equals(maker.get(), v.getMakerId())) {
            v.setMakerId(maker.get());
            modified = true;
        }
        if (!Objects.equals(makerDate.get(), v.getMakerDate())) {
            v.setMakerDate(makerDate.get());
            modified = true;
        }
        if (!Objects.equals(verifier.get(), v.getVerifierId())) {
            v.setVerifierId(verifier.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;
        }

        // 复制添加的字段
        if (!Objects.equals(refId.get(), v.getRefId())) {
            v.setRefId(refId.get());
            modified = true;
        }

        if (!NumberUtils.equals(taxRate.get(), v.getTaxRate())) {
            v.setTaxRate(taxRate.get());
            modified = true;
        }

        if (!Objects.equals(customer.get(), v.getCustomerId())) {
            v.setCustomerId(customer.get());
            modified = true;
        }

        if (!Objects.equals(customerAddress.get(), v.getCustomerAddress())) {
            v.setCustomerAddress(customerAddress.get());
            modified = true;
        }

        if (!Objects.equals(modifier.get(), v.getModifierId())) {
            v.setModifierId(modifier.get());
            modified = true;
        }

        if (!Objects.equals(modifierDate.get(), v.getModifierDate())) {
            v.setModifierDate(modifierDate.get());
            modified = true;
        }

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

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

        return modified;
    }

    public static SalesOrderViewModel from(SalesOrderVo order) {
        SalesOrderViewModel model = new SalesOrderViewModel();
        model.update(order);
        return model;
    }
}
