package com.ecep.contract.vm;

import com.ecep.contract.CompanyFileType;
import com.ecep.contract.vo.CompanyFileVo;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;

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

/**
 * View Model for {@link CompanyFileVo}
 */
@Data
@EqualsAndHashCode(callSuper = false)
public class CompanyFileViewModel extends IdentityViewModel<CompanyFileVo> {
    SimpleObjectProperty<Integer> company = new SimpleObjectProperty<>();
    SimpleObjectProperty<CompanyFileType> type = new SimpleObjectProperty<>();
    SimpleObjectProperty<LocalDate> applyDate = new SimpleObjectProperty<>();
    SimpleObjectProperty<LocalDate> expiringDate = new SimpleObjectProperty<>();
    SimpleStringProperty filePath = new SimpleStringProperty();

    @Override
    protected void updateFrom(CompanyFileVo v) {
        super.updateFrom(v);
        company.set(v.getCompanyId());
        type.set(v.getType());
        applyDate.set(v.getApplyDate());
        expiringDate.set(v.getExpiringDate());
        filePath.set(v.getFilePath());
    }

    public boolean copyTo(CompanyFileVo v) {
        boolean modified = super.copyTo(v);
        if (!Objects.equals(company.get(), v.getCompanyId())) {
            v.setCompanyId(company.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(expiringDate.get(), v.getExpiringDate())) {
            v.setExpiringDate(expiringDate.get());
            modified = true;
        }
        if (!Objects.equals(filePath.get(), v.getFilePath())) {
            v.setFilePath(filePath.get());
            modified = true;
        }
        return modified;
    }

    public static CompanyFileViewModel from(CompanyFileVo file) {
        CompanyFileViewModel model = new CompanyFileViewModel();
        model.update(file);
        return model;
    }
}