package com.ecep.contract.vm;

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

import com.ecep.contract.model.Company;
import com.ecep.contract.model.Invoice;

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 InvoiceViewModel extends IdentityViewModel<Invoice> {
    SimpleIntegerProperty id = new SimpleIntegerProperty();
    SimpleStringProperty code = new SimpleStringProperty();
    SimpleObjectProperty<Company> company = new SimpleObjectProperty<>();
    SimpleObjectProperty<LocalDate> invoiceDate = new SimpleObjectProperty<>();
    SimpleStringProperty description = new SimpleStringProperty();

    @Override
    protected void updateFrom(Invoice v) {
        super.updateFrom(v);
        getId().set(v.getId());
        getCode().set(v.getCode());
        getCompany().set(v.getCompany());
        getInvoiceDate().set(v.getInvoiceDate());
        getDescription().set(v.getDescription());
    }

    @Override
    public boolean copyTo(Invoice v) {
        boolean modified = super.copyTo(v);
        if (!Objects.equals(id.get(), v.getId())) {
            v.setId(id.get());
            modified = true;
        }
        if (!Objects.equals(code.get(), v.getCode())) {
            v.setCode(code.get());
            modified = true;
        }
        if (!Objects.equals(company.get(), v.getCompany())) {
            v.setCompany(company.get());
            modified = true;
        }
        if (!Objects.equals(invoiceDate.get(), v.getInvoiceDate())) {
            v.setInvoiceDate(invoiceDate.get());
            modified = true;
        }
        if (!Objects.equals(description.get(), v.getDescription())) {
            v.setDescription(description.get());
            modified = true;
        }
        return modified;
    }

    public static InvoiceViewModel from(Invoice invoice) {
        InvoiceViewModel vm = new InvoiceViewModel();
        vm.updateFrom(invoice);
        return vm;
    }
}
