package com.ecep.contract.vm;

import java.util.Objects;

import com.ecep.contract.vo.PermissionVo;

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<PermissionVo> {
    private SimpleStringProperty name = new SimpleStringProperty();
    private SimpleStringProperty key = new SimpleStringProperty();
    private SimpleObjectProperty<Integer> function = new SimpleObjectProperty<>();

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

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

    @Override
    public boolean copyTo(PermissionVo 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.getFunctionId())) {
            v.setFunctionId(function.get());
            modified = true;
        }
        if (!Objects.equals(key.get(), v.getKey())) {
            v.setKey(key.get());
            modified = true;
        }
        return modified;
    }
}