package com.ecep.contract.vm;

import com.ecep.contract.vo.ProjectVo;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;

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

@Data
@EqualsAndHashCode(callSuper = false)
public class ProjectViewModel extends IdentityViewModel<ProjectVo> {
    private SimpleObjectProperty<Integer> customer = new SimpleObjectProperty<>();
    /**
     * 银行账户（公司可能有多个账户，此项目关联其中一个账户）
     */
    private SimpleObjectProperty<Integer> bankAccount = new SimpleObjectProperty<>();
    /**
     * 开票信息（公司可能有多个开票信息，此项目关联其中一个开票信息）
     */
    private SimpleObjectProperty<Integer> invoiceInfo = new SimpleObjectProperty<>();
    /**
     * 主联系人（公司可能有多个联系人，此项目关联其中一个为主联系人）
     */
    private SimpleObjectProperty<Integer> mainContact = new SimpleObjectProperty<>();
    /**
     * 备用联系人（公司可能有多个联系人，此项目关联其中一个为备用联系人）
     */
    private SimpleObjectProperty<Integer> subContact = new SimpleObjectProperty<>();

    /**
     * 项目名称
     */
    private SimpleStringProperty name = new SimpleStringProperty();
    private SimpleStringProperty code = new SimpleStringProperty();
    private SimpleIntegerProperty codeYear = new SimpleIntegerProperty();
    private SimpleIntegerProperty codeSequenceNumber = new SimpleIntegerProperty();

    /**
     * 项目行业, Industry
     */
    private SimpleObjectProperty<Integer> industry = new SimpleObjectProperty<>();
    private SimpleObjectProperty<Integer> saleType = new SimpleObjectProperty<>();
    private SimpleObjectProperty<Integer> projectType = new SimpleObjectProperty<>();
    private SimpleObjectProperty<Integer> productType = new SimpleObjectProperty<>();
    private SimpleObjectProperty<Integer> deliverySignMethod = new SimpleObjectProperty<>();
    private SimpleObjectProperty<Integer> productUsage = new SimpleObjectProperty<>();

    /**
     * 项目地址
     */
    private SimpleStringProperty address = new SimpleStringProperty();
    private SimpleBooleanProperty useBid = new SimpleBooleanProperty();
    private SimpleBooleanProperty useOffer = new SimpleBooleanProperty();
    private SimpleIntegerProperty amount = new SimpleIntegerProperty();
    private SimpleBooleanProperty standardPayWay = new SimpleBooleanProperty();

    private SimpleStringProperty path = new SimpleStringProperty();
    private SimpleStringProperty description = new SimpleStringProperty();
    private SimpleObjectProperty<Integer> applicant = new SimpleObjectProperty<>();
    private SimpleObjectProperty<Integer> authorizer = new SimpleObjectProperty<>();
    private SimpleObjectProperty<LocalDate> created = new SimpleObjectProperty<>();
    private SimpleObjectProperty<LocalDate> plannedStartTime = new SimpleObjectProperty<>();
    private SimpleObjectProperty<LocalDate> plannedCompletionTime = new SimpleObjectProperty<>();

    private SimpleIntegerProperty version = new SimpleIntegerProperty();

    public static ProjectViewModel from(ProjectVo project) {
        ProjectViewModel model = new ProjectViewModel();
        model.update(project);
        return model;
    }

    @Override
    protected void updateFrom(ProjectVo v) {
        super.updateFrom(v);
        customer.set(v.getCustomerId());
        bankAccount.set(v.getBankAccountId());
        invoiceInfo.set(v.getInvoiceInfoId());
        mainContact.set(v.getMainContactId());
        subContact.set(v.getSubContactId());

        name.set(v.getName());
        code.set(v.getCode());
        codeYear.set(v.getCodeYear());
        codeSequenceNumber.set(v.getCodeSequenceNumber());
        address.set(v.getAddress());
        useBid.set(v.isUseBid());
        useOffer.set(v.isUseOffer());
        amount.set(Objects.requireNonNullElse(v.getAmount(), 0));
        standardPayWay.set(v.isStandardPayWay());
        industry.set(v.getIndustryId());
        saleType.set(v.getSaleTypeId());
        projectType.set(v.getProjectTypeId());
        productType.set(v.getProductTypeId());
        getDeliverySignMethod().set(v.getDeliverySignMethodId());
        getProductUsage().set(v.getProductUsageId());
        getPath().set(v.getPath());
        getDescription().set(v.getDescription());
        getApplicant().set(v.getApplicantId());
        getAuthorizer().set(v.getAuthorizerId());
        getCreated().set(v.getCreated());
        getPlannedStartTime().set(v.getPlannedStartTime());
        getPlannedCompletionTime().set(v.getPlannedCompletionTime());
        getVersion().set(v.getVersion());
    }

    public boolean copyTo(ProjectVo v) {
        boolean modified = super.copyTo(v);
        if (!Objects.equals(customer.get(), v.getCustomerId())) {
            v.setCustomerId(customer.get());
            modified = true;
        }

        if (!Objects.equals(bankAccount.get(), v.getBankAccountId())) {
            v.setBankAccountId(bankAccount.get());
            modified = true;
        }
        if (!Objects.equals(invoiceInfo.get(), v.getInvoiceInfoId())) {
            v.setInvoiceInfoId(invoiceInfo.get());
            modified = true;
        }
        if (!Objects.equals(mainContact.get(), v.getMainContactId())) {
            v.setMainContactId(mainContact.get());
            modified = true;
        }
        if (!Objects.equals(subContact.get(), v.getSubContactId())) {
            v.setSubContactId(subContact.get());
            modified = true;
        }

        if (!Objects.equals(name.get(), v.getName())) {
            v.setName(name.get());
            modified = true;
        }
        if (!Objects.equals(code.get(), v.getCode())) {
            v.setCode(code.get());
            modified = true;
        }
        if (!Objects.equals(codeYear.get(), v.getCodeYear())) {
            v.setCodeYear(codeYear.get());
            modified = true;
        }
        if (!Objects.equals(codeSequenceNumber.get(), v.getCodeSequenceNumber())) {
            v.setCodeSequenceNumber(codeSequenceNumber.get());
            modified = true;
        }
        if (!Objects.equals(address.get(), v.getAddress())) {
            v.setAddress(address.get());
            modified = true;
        }
        if (!Objects.equals(useBid.get(), v.isUseBid())) {
            v.setUseBid(useBid.get());
            modified = true;
        }
        if (!Objects.equals(useOffer.get(), v.isUseOffer())) {
            v.setUseOffer(useOffer.get());
            modified = true;
        }
        if (!Objects.equals(amount.get(), v.getAmount())) {
            v.setAmount(amount.get());
            modified = true;
        }
        if (!Objects.equals(standardPayWay.get(), v.isStandardPayWay())) {
            v.setStandardPayWay(standardPayWay.get());
            modified = true;
        }
        if (!Objects.equals(industry.get(), v.getIndustryId())) {
            v.setIndustryId(industry.get());
            modified = true;
        }
        if (!Objects.equals(saleType.get(), v.getSaleTypeId())) {
            v.setSaleTypeId(saleType.get());
            modified = true;
        }
        if (!Objects.equals(projectType.get(), v.getProjectTypeId())) {
            v.setProjectTypeId(projectType.get());
            modified = true;
        }
        if (!Objects.equals(productType.get(), v.getProductTypeId())) {
            v.setProductTypeId(productType.get());
            modified = true;
        }
        if (!Objects.equals(deliverySignMethod.get(), v.getDeliverySignMethodId())) {
            v.setDeliverySignMethodId(deliverySignMethod.get());
            modified = true;
        }
        if (!Objects.equals(productUsage.get(), v.getProductUsageId())) {
            v.setProductUsageId(productUsage.get());
            modified = true;
        }
        if (!Objects.equals(path.get(), v.getPath())) {
            v.setPath(path.get());
            modified = true;
        }
        if (!Objects.equals(description.get(), v.getDescription())) {
            v.setDescription(description.get());
            modified = true;
        }
        if (!Objects.equals(applicant.get(), v.getApplicantId())) {
            v.setApplicantId(applicant.get());
            modified = true;
        }
        if (!Objects.equals(authorizer.get(), v.getAuthorizerId())) {
            v.setAuthorizerId(authorizer.get());
            modified = true;
        }
        if (!Objects.equals(created.get(), v.getCreated())) {
            v.setCreated(created.get());
            modified = true;
        }
        if (!Objects.equals(plannedStartTime.get(), v.getPlannedStartTime())) {
            v.setPlannedStartTime(plannedStartTime.get());
            modified = true;
        }
        if (!Objects.equals(plannedCompletionTime.get(), v.getPlannedCompletionTime())) {
            v.setPlannedCompletionTime(plannedCompletionTime.get());
            modified = true;
        }
        return modified;
    }

}
