package com.ecep.contract.vm;

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

import com.ecep.contract.model.Company;

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 CompanyViewModel extends IdentityViewModel<Company> {
    private SimpleStringProperty name = new SimpleStringProperty();
    private SimpleStringProperty shortName = new SimpleStringProperty();
    private SimpleStringProperty uid = new SimpleStringProperty();
    private SimpleBooleanProperty pathExist = new SimpleBooleanProperty();
    private SimpleStringProperty path = new SimpleStringProperty();
    private SimpleObjectProperty<LocalDate> setupDate = new SimpleObjectProperty<>();
    private SimpleStringProperty entStatus = new SimpleStringProperty();
    private SimpleStringProperty entType = new SimpleStringProperty();
    private SimpleStringProperty district = new SimpleStringProperty();
    private SimpleStringProperty industry = new SimpleStringProperty();

    private SimpleStringProperty regAddress = new SimpleStringProperty();
    private SimpleStringProperty address = new SimpleStringProperty();

    private SimpleStringProperty telephone = new SimpleStringProperty();
    private SimpleObjectProperty<LocalDate> operationPeriodBegin = new SimpleObjectProperty<>();
    private SimpleObjectProperty<LocalDate> operationPeriodEnd = new SimpleObjectProperty<>();
    private SimpleStringProperty registeredCapital = new SimpleStringProperty();
    private SimpleStringProperty registeredCapitalCurrency = new SimpleStringProperty();
    private SimpleStringProperty legalRepresentative = new SimpleStringProperty();

    private SimpleStringProperty memo = new SimpleStringProperty();
    private SimpleObjectProperty<LocalDate> created = new SimpleObjectProperty<>();

    private SimpleIntegerProperty version = new SimpleIntegerProperty();

    public static CompanyViewModel from(Company company) {
        CompanyViewModel model = new CompanyViewModel();
        model.update(company);
        return model;
    }

    @Override
    protected void updateFrom(Company company) {
        super.updateFrom(company);
        name.set(company.getName());
        shortName.set(company.getShortName());
        uid.set(company.getUniscid());
        if (company.getPathExist() != null) {
            pathExist.set(company.getPathExist());
        }
        path.set(company.getPath());
        setupDate.set(company.getSetupDate());
        entStatus.set(company.getEntStatus());
        entType.set(company.getEntType());
        district.set(company.getDistrict());
        industry.set(company.getIndustry());

        regAddress.set(company.getRegAddr());
        address.set(company.getAddress());

        telephone.set(company.getTelephone());
        operationPeriodBegin.set(company.getOperationPeriodBegin());
        operationPeriodEnd.set(company.getOperationPeriodEnd());
        registeredCapital.set(company.getRegisteredCapital());
        registeredCapitalCurrency.set(company.getRegisteredCapitalCurrency());
        legalRepresentative.set(company.getLegalRepresentative());

        created.set(company.getCreated());
        memo.set(company.getMemo());
        version.set(company.getVersion());
    }

    public boolean copyTo(Company v) {
        boolean modified = super.copyTo(v);
        if (!Objects.equals(name.get(), v.getName())) {
            v.setName(name.get());
            modified = true;
        }
        if (!Objects.equals(shortName.get(), v.getShortName())) {
            v.setShortName(shortName.get());
            modified = true;
        }
        if (!Objects.equals(uid.get(), v.getUniscid())) {
            v.setUniscid(uid.get());
            modified = true;
        }

        if (!Objects.equals(pathExist.get(), v.getPathExist())) {
            v.setPathExist(pathExist.get());
            modified = true;
        }
        if (!Objects.equals(path.get(), v.getPath())) {
            v.setPath(path.get());
            modified = true;
        }

        if (!Objects.equals(setupDate.get(), v.getSetupDate())) {
            v.setSetupDate(setupDate.get());
            modified = true;
        }
        if (!Objects.equals(entStatus.get(), v.getEntStatus())) {
            v.setEntStatus(entStatus.get());
            modified = true;
        }
        if (!Objects.equals(entType.get(), v.getEntType())) {
            v.setEntType(entType.get());
            modified = true;
        }
        if (!Objects.equals(district.get(), v.getDistrict())) {
            v.setDistrict(district.get());
            modified = true;
        }
        if (!Objects.equals(industry.get(), v.getIndustry())) {
            v.setIndustry(industry.get());
            modified = true;
        }

        if (!Objects.equals(regAddress.get(), v.getRegAddr())) {
            v.setRegAddr(regAddress.get());
            modified = true;
        }
        if (!Objects.equals(address.get(), v.getAddress())) {
            v.setAddress(address.get());
            modified = true;
        }
        if (!Objects.equals(telephone.get(), v.getTelephone())) {
            v.setTelephone(telephone.get());
            modified = true;
        }

        if (!Objects.equals(operationPeriodBegin.get(), v.getOperationPeriodBegin())) {
            v.setOperationPeriodBegin(operationPeriodBegin.get());
            modified = true;
        }
        if (!Objects.equals(operationPeriodEnd.get(), v.getOperationPeriodEnd())) {
            v.setOperationPeriodEnd(operationPeriodEnd.get());
            modified = true;
        }
        if (!Objects.equals(registeredCapital.get(), v.getRegisteredCapital())) {
            v.setRegisteredCapital(registeredCapital.get());
            modified = true;
        }
        if (!Objects.equals(registeredCapitalCurrency.get(), v.getRegisteredCapitalCurrency())) {
            v.setRegisteredCapitalCurrency(registeredCapitalCurrency.get());
            modified = true;
        }
        if (!Objects.equals(legalRepresentative.get(), v.getLegalRepresentative())) {
            v.setLegalRepresentative(legalRepresentative.get());
            modified = true;
        }

        if (!Objects.equals(memo.get(), v.getMemo())) {
            v.setMemo(memo.get());
            modified = true;
        }

        if (!Objects.equals(created.get(), v.getCreated())) {
            v.setCreated(created.get());
            modified = true;
        }

        return modified;
    }
}
