package com.ecep.contract.vm;

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

import com.ecep.contract.model.Company;
import com.ecep.contract.model.CompanyBankAccount;
import com.ecep.contract.model.CompanyContact;
import com.ecep.contract.model.CompanyInvoiceInfo;
import com.ecep.contract.model.DeliverySignMethod;
import com.ecep.contract.model.Employee;
import com.ecep.contract.model.ProductType;
import com.ecep.contract.model.ProductUsage;
import com.ecep.contract.model.Project;
import com.ecep.contract.model.ProjectIndustry;
import com.ecep.contract.model.ProjectSaleType;
import com.ecep.contract.model.ProjectType;

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;

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

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

    private SimpleObjectProperty<ProjectIndustry> industry = new SimpleObjectProperty<>();
    private SimpleObjectProperty<ProjectSaleType> saleType = new SimpleObjectProperty<>();
    private SimpleObjectProperty<ProjectType> projectType = new SimpleObjectProperty<>();
    private SimpleObjectProperty<ProductType> productType = new SimpleObjectProperty<>();
    private SimpleObjectProperty<DeliverySignMethod> deliverySignMethod = new SimpleObjectProperty<>();
    private SimpleObjectProperty<ProductUsage> 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<Employee> applicant = new SimpleObjectProperty<>();
    private SimpleObjectProperty<Employee> 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(Project project) {
        ProjectViewModel model = new ProjectViewModel();
        model.update(project);
        return model;
    }

    @Override
    protected void updateFrom(Project v) {
        super.updateFrom(v);
        customer.set(v.getCustomer());
        bankAccount.set(v.getBankAccount());
        invoiceInfo.set(v.getInvoiceInfo());
        mainContact.set(v.getMainContact());
        subContact.set(v.getSubContact());

        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.getIndustry());
        saleType.set(v.getSaleType());
        projectType.set(v.getProjectType());
        productType.set(v.getProductType());
        getDeliverySignMethod().set(v.getDeliverySignMethod());
        getProductUsage().set(v.getProductUsage());
        path.set(v.getPath());
        getDescription().set(v.getDescription());
        getApplicant().set(v.getApplicant());
        getAuthorizer().set(v.getAuthorizer());
        getCreated().set(v.getCreated());
        getPlannedStartTime().set(v.getPlannedStartTime());
        getPlannedCompletionTime().set(v.getPlannedCompletionTime());
        getVersion().set(v.getVersion());
    }

    public boolean copyTo(Project v) {
        boolean modified = super.copyTo(v);
        if (!Objects.equals(customer.get(), v.getCustomer())) {
            v.setCustomer(customer.get());
            modified = true;
        }

        if (!Objects.equals(bankAccount.get(), v.getBankAccount())) {
            v.setBankAccount(bankAccount.get());
            modified = true;
        }
        if (!Objects.equals(invoiceInfo.get(), v.getInvoiceInfo())) {
            v.setInvoiceInfo(invoiceInfo.get());
            modified = true;
        }
        if (!Objects.equals(mainContact.get(), v.getMainContact())) {
            v.setMainContact(mainContact.get());
            modified = true;
        }
        if (!Objects.equals(subContact.get(), v.getSubContact())) {
            v.setSubContact(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.getIndustry())) {
            v.setIndustry(industry.get());
            modified = true;
        }
        if (!Objects.equals(saleType.get(), v.getSaleType())) {
            v.setSaleType(saleType.get());
            modified = true;
        }
        if (!Objects.equals(projectType.get(), v.getProjectType())) {
            v.setProjectType(projectType.get());
            modified = true;
        }
        if (!Objects.equals(productType.get(), v.getProductType())) {
            v.setProductType(productType.get());
            modified = true;
        }
        if (!Objects.equals(deliverySignMethod.get(), v.getDeliverySignMethod())) {
            v.setDeliverySignMethod(deliverySignMethod.get());
            modified = true;
        }
        if (!Objects.equals(productUsage.get(), v.getProductUsage())) {
            v.setProductUsage(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.getApplicant())) {
            v.setApplicant(applicant.get());
            modified = true;
        }
        if (!Objects.equals(authorizer.get(), v.getAuthorizer())) {
            v.setAuthorizer(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;
    }

}
