package com.ecep.contract.vm;

import java.time.LocalDateTime;
import java.util.Objects;

import com.ecep.contract.model.Company;
import com.ecep.contract.model.Employee;
import com.ecep.contract.model.SalesBillVoucher;
import com.ecep.contract.model.SalesOrder;

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 SalesBillVoucherViewModel extends IdentityViewModel<SalesBillVoucher> {

    SimpleIntegerProperty id = new SimpleIntegerProperty();
    SimpleIntegerProperty refId = new SimpleIntegerProperty();
    SimpleStringProperty code = new SimpleStringProperty();
    SimpleObjectProperty<Company> company = new SimpleObjectProperty<>();
    SimpleObjectProperty<SalesOrder> order = new SimpleObjectProperty<>();
    SimpleObjectProperty<Employee> employee = new SimpleObjectProperty<>();
    SimpleObjectProperty<Employee> maker = new SimpleObjectProperty<>();
    SimpleObjectProperty<Employee> verifier = new SimpleObjectProperty<>();
    SimpleObjectProperty<LocalDateTime> makerDate = new SimpleObjectProperty<>();
    SimpleObjectProperty<LocalDateTime> modifyDate = new SimpleObjectProperty<>();
    SimpleObjectProperty<LocalDateTime> verifierDate = new SimpleObjectProperty<>();
    SimpleStringProperty description = new SimpleStringProperty();

    @Override
    protected void updateFrom(SalesBillVoucher v) {
        getId().set(v.getId());
        getRefId().set(v.getRefId());
        getCode().set(v.getCode());
        getCompany().set(v.getCompany());
        getOrder().set(v.getOrder());
        getEmployee().set(v.getEmployee());
        getMaker().set(v.getMaker());
        getVerifier().set(v.getVerifier());
        getMakerDate().set(v.getMakerDate());
        getModifyDate().set(v.getModifyDate());
        getVerifierDate().set(v.getVerifierDate());
        getDescription().set(v.getDescription());
        super.updateFrom(v);
    }

    @Override
    public boolean copyTo(SalesBillVoucher v) {
        boolean modified = super.copyTo(v);
        if (!Objects.equals(id.get(), v.getId())) {
            v.setId(id.get());
            modified = true;
        }
        if (!Objects.equals(refId.get(), v.getRefId())) {
            v.setRefId(refId.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(order.get(), v.getOrder())) {
            v.setOrder(order.get());
            modified = true;
        }
        if (!Objects.equals(employee.get(), v.getEmployee())) {
            v.setEmployee(employee.get());
            modified = true;
        }
        if (!Objects.equals(maker.get(), v.getMaker())) {
            v.setMaker(maker.get());
            modified = true;
        }
        if (!Objects.equals(verifier.get(), v.getVerifier())) {
            v.setVerifier(verifier.get());
            modified = true;
        }
        if (!Objects.equals(makerDate.get(), v.getMakerDate())) {
            v.setMakerDate(makerDate.get());
            modified = true;
        }
        if (!Objects.equals(modifyDate.get(), v.getModifyDate())) {
            v.setModifyDate(modifyDate.get());
            modified = true;
        }
        if (!Objects.equals(verifierDate.get(), v.getVerifierDate())) {
            v.setVerifierDate(verifierDate.get());
            modified = true;
        }
        if (!Objects.equals(description.get(), v.getDescription())) {
            v.setDescription(description.get());
            modified = true;
        }
        return modified;
    }

    public static SalesBillVoucherViewModel from(SalesBillVoucher order) {
        SalesBillVoucherViewModel model = new SalesBillVoucherViewModel();
        model.update(order);
        return model;
    }
}
