package com.ecep.contract.vm;

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

import com.ecep.contract.ContractFileType;
import com.ecep.contract.model.Contract;
import com.ecep.contract.model.ContractFile;

import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;

@Data
@EqualsAndHashCode(callSuper = false)
public class ContractFileViewModel extends IdentityViewModel<ContractFile> {
    SimpleObjectProperty<Contract> contract = new SimpleObjectProperty<>();
    SimpleObjectProperty<ContractFileType> type = new SimpleObjectProperty<>();
    SimpleObjectProperty<LocalDate> applyDate = new SimpleObjectProperty<>();
    SimpleStringProperty fileName = new SimpleStringProperty();
    SimpleStringProperty description = new SimpleStringProperty();

    @Override
    protected void updateFrom(ContractFile v) {
        super.updateFrom(v);
        contract.set(v.getContract());
        type.set(v.getType());
        applyDate.set(v.getApplyDate());
        fileName.set(v.getFileName());
        description.set(v.getDescription());
        super.updateFrom(v);
    }

    @Override
    public boolean copyTo(ContractFile v) {
        boolean modified = super.copyTo(v);
        if (!Objects.equals(contract.get(), v.getContract())) {
            v.setContract(contract.get());
            modified = true;
        }

        if (!Objects.equals(type.get(), v.getType())) {
            v.setType(type.get());
            modified = true;
        }

        if (!Objects.equals(applyDate.get(), v.getApplyDate())) {
            v.setApplyDate(applyDate.get());
            modified = true;
        }

        if (!Objects.equals(fileName.get(), v.getFileName())) {
            v.setFileName(fileName.get());
            modified = true;
        }

        if (!Objects.equals(description.get(), v.getDescription())) {
            v.setDescription(description.get());
            modified = true;
        }
        return modified;
    }

    public static ContractFileViewModel from(ContractFile contractFile) {
        ContractFileViewModel model = new ContractFileViewModel();
        model.update(contractFile);
        return model;
    }
}
