package com.ecep.contract.vm;

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

import com.ecep.contract.ContractPayWay;
import com.ecep.contract.model.ContractPayPlan;
import com.ecep.contract.model.Project;
import com.ecep.contract.model.ProjectFundPlan;
import com.ecep.contract.util.NumberUtils;

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

/**
 * 项目资金计划视图模型
 */
@Data
@EqualsAndHashCode(callSuper = false)
public class ProjectFundPlanViewModel extends IdentityViewModel<ProjectFundPlan> implements ProjectBasedViewModel {
    /**
     * 关联的项目
     */
    private SimpleObjectProperty<Project> project = new SimpleObjectProperty<>();

    /**
     * 付款日期
     */
    private SimpleObjectProperty<LocalDate> payDate = new SimpleObjectProperty<>();

    /**
     * 付款比例
     */
    private SimpleFloatProperty payRatio = new SimpleFloatProperty();

    /**
     * 付款金额
     */
    private SimpleDoubleProperty payCurrency = new SimpleDoubleProperty();
    /**
     * 余额
     */
    private SimpleDoubleProperty balance = new SimpleDoubleProperty();
    /**
     * 付款条件
     */
    private SimpleStringProperty payTerm = new SimpleStringProperty();

    /**
     * 合同付款计划
     */
    private SimpleObjectProperty<ContractPayPlan> contractPayPlan = new SimpleObjectProperty<>();

    /**
     * 更新日期
     */
    private SimpleObjectProperty<LocalDateTime> updateDate = new SimpleObjectProperty<>();

    /**
     * 付款方式
     */
    private SimpleObjectProperty<ContractPayWay> payWay = new SimpleObjectProperty<>();

    /**
     * 从实体对象创建视图模型
     */
    public static ProjectFundPlanViewModel from(ProjectFundPlan entity) {
        ProjectFundPlanViewModel model = new ProjectFundPlanViewModel();
        model.update(entity);
        return model;
    }

    @Override
    protected void updateFrom(ProjectFundPlan entity) {
        super.updateFrom(entity);

        // 设置项目关联
        getProject().set(entity.getProject());

        // 设置付款相关信息
        getPayDate().set(entity.getPayDate());
        getPayRatio().set(entity.getPayRatio());
        getPayCurrency().set(entity.getPayCurrency());
        getPayTerm().set(entity.getPayTerm());

        // 设置合同付款计划关联
        getContractPayPlan().set(entity.getContractPayPlan());

        // 设置更新日期
        getUpdateDate().set(entity.getUpdateDate());

        // 设置付款方式
        getPayWay().set(entity.getPayWay());
    }

    @Override
    public boolean copyTo(ProjectFundPlan entity) {
        boolean modified = super.copyTo(entity);

        // 复制项目关联
        if (!Objects.equals(getProject().get(), entity.getProject())) {
            entity.setProject(getProject().get());
            modified = true;
        }

        // 复制付款相关信息
        if (!Objects.equals(getPayDate().get(), entity.getPayDate())) {
            entity.setPayDate(getPayDate().get());
            modified = true;
        }

        if (!NumberUtils.equals(getPayRatio().get(), entity.getPayRatio())) {
            entity.setPayRatio(getPayRatio().get());
            modified = true;
        }

        if (!NumberUtils.equals(getPayCurrency().get(), entity.getPayCurrency())) {
            entity.setPayCurrency(getPayCurrency().get());
            modified = true;
        }

        if (!Objects.equals(getPayTerm().get(), entity.getPayTerm())) {
            entity.setPayTerm(getPayTerm().get());
            modified = true;
        }

        // 复制合同付款计划关联
        if (!Objects.equals(getContractPayPlan().get(), entity.getContractPayPlan())) {
            entity.setContractPayPlan(getContractPayPlan().get());
            modified = true;
        }

        // 复制更新日期
        if (!Objects.equals(getUpdateDate().get(), entity.getUpdateDate())) {
            entity.setUpdateDate(getUpdateDate().get());
            modified = true;
        }

        // 复制付款方式
        if (!Objects.equals(getPayWay().get(), entity.getPayWay())) {
            entity.setPayWay(getPayWay().get());
            modified = true;
        }

        return modified;
    }

}
