package com.ecep.contract.vm;

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

import com.ecep.contract.model.Contract;
import com.ecep.contract.model.ContractItem;
import com.ecep.contract.model.Employee;
import com.ecep.contract.model.Inventory;

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

/**
 * 合同内容
 */
@Data
@EqualsAndHashCode(callSuper = false)
public class ContractItemViewModel extends IdentityViewModel<ContractItem> {
    /**
     * 关联的合同对象
     */
    SimpleObjectProperty<Contract> contract = new SimpleObjectProperty<>();
    /**
     * 关联的用友系统中的数据ID
     */
    SimpleIntegerProperty refId = new SimpleIntegerProperty();
    SimpleObjectProperty<String> code = new SimpleObjectProperty<>();
    SimpleObjectProperty<String> title = new SimpleObjectProperty<>();
    SimpleObjectProperty<String> specification = new SimpleObjectProperty<>();
    SimpleObjectProperty<String> unit = new SimpleObjectProperty<>();

    /**
     * 存货
     */
    SimpleObjectProperty<Inventory> inventory = new SimpleObjectProperty<>();

    /**
     *
     */
    SimpleDoubleProperty taxRate = new SimpleDoubleProperty();
    SimpleDoubleProperty exclusiveTaxPrice = new SimpleDoubleProperty();
    SimpleDoubleProperty taxPrice = new SimpleDoubleProperty();
    SimpleDoubleProperty quantity = new SimpleDoubleProperty();
    SimpleDoubleProperty taxAmount = new SimpleDoubleProperty();
    SimpleDoubleProperty exclusiveTaxAmount = new SimpleDoubleProperty();


    SimpleObjectProperty<LocalDate> startDate = new SimpleObjectProperty<>();
    SimpleObjectProperty<LocalDate> endDate = new SimpleObjectProperty<>();

    SimpleObjectProperty<LocalDateTime> createDate = new SimpleObjectProperty<>();
    SimpleObjectProperty<LocalDateTime> updateDate = new SimpleObjectProperty<>();
    SimpleObjectProperty<Employee> creator = new SimpleObjectProperty<>();
    SimpleObjectProperty<Employee> updater = new SimpleObjectProperty<>();

    SimpleObjectProperty<String> remark = new SimpleObjectProperty<>();


    @Override
    protected void updateFrom(ContractItem v) {
        super.updateFrom(v);
        getContract().set(v.getContract());
        getRefId().set(v.getRefId());
        getCode().set(v.getItemCode());
        getTitle().set(v.getTitle());
        getSpecification().set(v.getSpecification());
        getUnit().set(v.getUnit());
        getInventory().set(v.getInventory());

        getExclusiveTaxPrice().set(v.getExclusiveTaxPrice());
        getTaxRate().set(v.getTaxRate());
        getTaxPrice().set(v.getTaxPrice());
        getQuantity().set(v.getQuantity());

        getStartDate().set(v.getStartDate());
        getEndDate().set(v.getEndDate());

        getCreateDate().set(v.getCreateDate());
        getUpdateDate().set(v.getUpdateDate());
        getCreator().set(v.getCreator());
        getUpdater().set(v.getUpdater());
        getRemark().set(v.getRemark());

        getTaxAmount().set(v.getTaxPrice() * v.getQuantity());
        getExclusiveTaxAmount().set(v.getExclusiveTaxPrice() * v.getQuantity());
    }


    @Override
    public boolean copyTo(ContractItem v) {
        boolean modified = super.copyTo(v);
        if (!Objects.equals(contract.get(), v.getContract())) {
            v.setContract(contract.get());
            modified = true;
        }
        if (!Objects.equals(refId.get(), v.getRefId())) {
            v.setRefId(refId.get());
            modified = true;
        }
        if (!Objects.equals(code.get(), v.getItemCode())) {
            v.setItemCode(code.get());
            modified = true;
        }
        if (!Objects.equals(title.get(), v.getTitle())) {
            v.setTitle(title.get());
            modified = true;
        }
        if (!Objects.equals(specification.get(), v.getSpecification())) {
            v.setSpecification(specification.get());
            modified = true;
        }
        if (!Objects.equals(unit.get(), v.getUnit())) {
            v.setUnit(unit.get());
            modified = true;
        }
        if (!Objects.equals(inventory.get(), v.getInventory())) {
            v.setInventory(inventory.get());
            modified = true;
        }
        if (!doubleEquals(exclusiveTaxPrice.get(), v.getExclusiveTaxPrice())) {
            v.setExclusiveTaxPrice(exclusiveTaxPrice.get());
            modified = true;
        }
        if (!doubleEquals(taxRate.get(), v.getTaxRate())) {
            v.setTaxRate(taxRate.get());
            modified = true;
        }
        if (!doubleEquals(taxPrice.get(), v.getTaxPrice())) {
            v.setTaxPrice(taxPrice.get());
            modified = true;
        }
        if (!doubleEquals(quantity.get(), v.getQuantity())) {
            v.setQuantity(quantity.get());
            modified = true;
        }

        if (!Objects.equals(startDate.get(), v.getStartDate())) {
            v.setStartDate(startDate.get());
            modified = true;
        }
        if (!Objects.equals(endDate.get(), v.getEndDate())) {
            v.setEndDate(endDate.get());
            modified = true;
        }

        if (!Objects.equals(createDate.get(), v.getCreateDate())) {
            v.setCreateDate(createDate.get());
            modified = true;
        }
        if (!Objects.equals(updateDate.get(), v.getUpdateDate())) {
            v.setUpdateDate(updateDate.get());
            modified = true;
        }
        if (!Objects.equals(creator.get(), v.getCreator())) {
            v.setCreator(creator.get());
            modified = true;
        }
        if (!Objects.equals(updater.get(), v.getUpdater())) {
            v.setUpdater(updater.get());
            modified = true;
        }
        if (!Objects.equals(remark.get(), v.getRemark())) {
            v.setRemark(remark.get());
            modified = true;
        }


        return modified;
    }

    /**
     * 判断两个 double 值是否相等
     *
     * @param a a
     * @param b b
     * @return 在误差范围内是否相同
     */
    private boolean doubleEquals(double a, double b) {
        return Math.abs(a - b) < 0.0001;
    }

    public static ContractItemViewModel from(ContractItem contractItem) {
        ContractItemViewModel viewModel = new ContractItemViewModel();
        viewModel.update(contractItem);
        return viewModel;
    }
}
