package com.ecep.contract.vm;

import java.util.Objects;

import com.ecep.contract.model.Function;
import com.ecep.contract.model.Permission;

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

@Data
@EqualsAndHashCode(callSuper = false)
public class PermissionViewModel extends IdentityViewModel<Permission> {
    private SimpleStringProperty name = new SimpleStringProperty();
    private SimpleObjectProperty<Function> function = new SimpleObjectProperty<>();
    private SimpleStringProperty key = new SimpleStringProperty();

    public static PermissionViewModel from(Permission v) {
        PermissionViewModel model = new PermissionViewModel();
        model.update(v);
        return model;
    }

    @Override
    protected void updateFrom(Permission v) {
        super.updateFrom(v);
        getName().set(v.getName());
        getFunction().set(v.getFunction());
        getKey().set(v.getKey());
    }

    @Override
    public boolean copyTo(Permission v) {
        boolean modified = super.copyTo(v);
        if (!Objects.equals(name.get(), v.getName())) {
            v.setName(name.get());
            modified = true;
        }
        if (!Objects.equals(function.get(), v.getFunction())) {
            v.setFunction(function.get());
            modified = true;
        }
        if (!Objects.equals(key.get(), v.getKey())) {
            v.setKey(key.get());
            modified = true;
        }
        return modified;
    }
}