package com.ecep.contract.vm;

import java.io.File;
import java.time.LocalDateTime;
import java.util.Objects;

import org.springframework.util.StringUtils;

import com.ecep.contract.model.CompanyCustomerEvaluationFormFile;
import com.ecep.contract.model.Employee;
import com.ecep.contract.model.Project;
import com.ecep.contract.model.ProjectQuotation;
import com.ecep.contract.util.NumberUtils;

import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleDoubleProperty;
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 ProjectQuotationViewModel extends IdentityViewModel<ProjectQuotation> {
    /**
     * 关联的项目
     */
    private SimpleObjectProperty<Project> project = new SimpleObjectProperty<>();
    /**
     * 客户咨询等级（级别）
     */
    private SimpleIntegerProperty level = new SimpleIntegerProperty();
    /**
     * 是否标准付款方式
     */
    private SimpleBooleanProperty standardPayWay = new SimpleBooleanProperty();
    /**
     * 非标准付款方式文本
     */

    private SimpleStringProperty noStandardPayWayText = new SimpleStringProperty();
    /**
     * 报价金额
     */
    private SimpleDoubleProperty amount = new SimpleDoubleProperty();
    /**
     * 客户咨询评估文件，根据资信资评估文件生成客户资信等级（级别）
     */
    private SimpleObjectProperty<CompanyCustomerEvaluationFormFile> evaluationFile = new SimpleObjectProperty<>();
    /**
     * 申请日期
     */
    private SimpleObjectProperty<LocalDateTime> applyTime = new SimpleObjectProperty<>();
    /**
     * 申请人
     */
    private SimpleObjectProperty<Employee> applicant = new SimpleObjectProperty<>();
    /**
     * 审核时间
     */
    private SimpleObjectProperty<LocalDateTime> authorizationTime = new SimpleObjectProperty<>();
    /**
     * 审核人
     */
    private SimpleObjectProperty<Employee> authorizer = new SimpleObjectProperty<>();
    /**
     * 审核文件
     */
    private SimpleObjectProperty<File> authorizationFile = new SimpleObjectProperty<>();
    /**
     * 说明、注释
     */
    private SimpleStringProperty description = new SimpleStringProperty();


    public static ProjectQuotationViewModel from(ProjectQuotation v) {
        ProjectQuotationViewModel model = new ProjectQuotationViewModel();
        model.update(v);
        return model;
    }

    @Override
    protected void updateFrom(ProjectQuotation v) {
        super.updateFrom(v);
        getLevel().set(v.getLevel());
        getStandardPayWay().set(v.isStandardPayWay());
        getNoStandardPayWayText().set(v.getNoStandardPayWayText());
        getAmount().set(v.getAmount());
        getEvaluationFile().set(v.getEvaluationFile());
        getProject().set(v.getProject());
        getApplicant().set(v.getApplicant());
        getApplyTime().set(v.getApplyTime());
        getAuthorizer().set(v.getAuthorizer());
        getAuthorizationTime().set(v.getAuthorizationTime());

        if (StringUtils.hasText(v.getAuthorizationFile())) {
            File file = new File(v.getAuthorizationFile());
            getAuthorizationFile().set(file);
        } else {
            getAuthorizationFile().set(null);
        }

        getDescription().set(v.getDescription());
    }

    @Override
    public boolean copyTo(ProjectQuotation v) {
        boolean modified = super.copyTo(v);
        if (!Objects.equals(getLevel().get(), v.getLevel())) {
            v.setLevel(getLevel().get());
            modified = true;
        }
        if (!Objects.equals(getStandardPayWay().get(), v.isStandardPayWay())) {
            v.setStandardPayWay(getStandardPayWay().get());
            modified = true;
        }
        if (!Objects.equals(getNoStandardPayWayText().get(), v.getNoStandardPayWayText())) {
            v.setNoStandardPayWayText(getNoStandardPayWayText().get());
            modified = true;
        }
        if (!NumberUtils.equals(getAmount().get(), v.getAmount())) {
            v.setAmount(getAmount().get());
            modified = true;
        }
        if (!Objects.equals(getEvaluationFile().get(), v.getEvaluationFile())) {
            v.setEvaluationFile(getEvaluationFile().get());
            modified = true;
        }
        if (!Objects.equals(getProject().get(), v.getProject())) {
            v.setProject(getProject().get());
            modified = true;
        }
        if (!Objects.equals(getApplicant().get(), v.getApplicant())) {
            v.setApplicant(getApplicant().get());
            modified = true;
        }
        if (!Objects.equals(getApplyTime().get(), v.getApplyTime())) {
            v.setApplyTime(getApplyTime().get());
            modified = true;
        }
        if (!Objects.equals(getAuthorizer().get(), v.getAuthorizer())) {
            v.setAuthorizer(getAuthorizer().get());
            modified = true;
        }
        if (!Objects.equals(getAuthorizationTime().get(), v.getAuthorizationTime())) {
            v.setAuthorizationTime(getAuthorizationTime().get());
            modified = true;
        }

        {

            File file = null;
            if (StringUtils.hasText(v.getAuthorizationFile())) {
                file = new File(v.getAuthorizationFile());
            }
            if (!Objects.equals(authorizationFile.get(), file)) {
                v.setAuthorizationFile(authorizationFile.get() == null ? null : authorizationFile.get().getAbsolutePath());
                modified = true;
            }
        }

        if (!Objects.equals(getDescription().get(), v.getDescription())) {
            v.setDescription(getDescription().get());
            modified = true;
        }
        return modified;
    }
}
