package com.ecep.contract.vm;

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

import com.ecep.contract.vo.ContractItemVo;

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

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

    /**
     * 存货,Inventory
     */
    private final SimpleObjectProperty<Integer> inventory = new SimpleObjectProperty<>();

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

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

    private final SimpleObjectProperty<LocalDateTime> createDate = new SimpleObjectProperty<>();
    private final SimpleObjectProperty<LocalDateTime> updateDate = new SimpleObjectProperty<>();
    /**
     * 关联的创建人,Employee
     */
    private final SimpleObjectProperty<Integer> creator = new SimpleObjectProperty<>();
    /**
     * 关联的更新人,Employee
     */
    private final SimpleObjectProperty<Integer> updater = new SimpleObjectProperty<>();

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

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

        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.getCreatorId());
        getUpdater().set(v.getUpdaterId());
        getRemark().set(v.getRemark());

        calculateAmounts();
    }

    @Override
    public boolean copyTo(ContractItemVo v) {
        boolean modified = super.copyTo(v);
        if (!Objects.equals(contract.get(), v.getContractId())) {
            v.setContractId(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.getInventoryId())) {
            v.setInventoryId(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.getCreatorId())) {
            v.setCreatorId(creator.get());
            modified = true;
        }
        if (!Objects.equals(updater.get(), v.getUpdaterId())) {
            v.setUpdaterId(updater.get());
            modified = true;
        }
        if (!Objects.equals(remark.get(), v.getRemark())) {
            v.setRemark(remark.get());
            modified = true;
        }

        return modified;
    }

    /**
     * 计算税额相关字段
     */
    public void calculateAmounts() {
        double price = getExclusiveTaxPrice().get();
        double rate = getTaxRate().get();
        double qty = getQuantity().get();
        
        // 计算含税单价
        double taxPrice = price * (1 + rate / 100);
        // 计算税额
        double taxAmount = price * rate / 100 * qty;
        // 计算不含税金额
        double exclusiveTaxAmount = price * qty;
        
        getTaxPrice().set(taxPrice);
        getTaxAmount().set(taxAmount);
        getExclusiveTaxAmount().set(exclusiveTaxAmount);
    }

    /**
     * 判断两个 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(ContractItemVo contractItem) {
        ContractItemViewModel viewModel = new ContractItemViewModel();
        viewModel.update(contractItem);
        return viewModel;
    }
}
