package com.ecep.contract.vm;

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

import com.ecep.contract.model.Bank;
import com.ecep.contract.model.Company;
import com.ecep.contract.model.CompanyBankAccount;

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 CompanyBankAccountViewModel extends IdentityViewModel<CompanyBankAccount> implements CompanyBasedViewModel {
    private SimpleIntegerProperty id = new SimpleIntegerProperty();

    private SimpleObjectProperty<Company> company = new SimpleObjectProperty<>();
    private SimpleObjectProperty<Bank> bank = new SimpleObjectProperty<>();

    private SimpleStringProperty openingBank = new SimpleStringProperty();
    private SimpleStringProperty account = new SimpleStringProperty();

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

    private SimpleIntegerProperty version = new SimpleIntegerProperty();

    @Override
    protected void updateFrom(CompanyBankAccount v) {
        getId().set(v.getId());
        getCompany().set(v.getCompany());
        getBank().set(v.getBank());
        getOpeningBank().set(v.getOpeningBank());
        getAccount().set(v.getAccount());
    }

    @Override
    public boolean copyTo(CompanyBankAccount v) {
        boolean modified = super.copyTo(v);
        if (!Objects.equals(id.get(), v.getId())) {
            v.setId(id.get());
            modified = true;
        }
        if (!Objects.equals(company.get(), v.getCompany())) {
            v.setCompany(company.get());
            modified = true;
        }
        if (!Objects.equals(bank.get(), v.getBank())) {
            v.setBank(bank.get());
            modified = true;
        }
        if (!Objects.equals(openingBank.get(), v.getOpeningBank())) {
            v.setOpeningBank(openingBank.get());
            modified = true;
        }
        if (!Objects.equals(account.get(), v.getAccount())) {
            v.setAccount(account.get());
            modified = true;
        }
        return modified;
    }

    public static CompanyBankAccountViewModel from(CompanyBankAccount v) {
        CompanyBankAccountViewModel vm = new CompanyBankAccountViewModel();
        vm.updateFrom(v);
        return vm;
    }
}
