package com.ecep.contract.vm;

import java.util.Objects;

import com.ecep.contract.model.Department;
import com.ecep.contract.model.Employee;

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

@Data
@EqualsAndHashCode(callSuper = false)
public class DepartmentViewModel extends IdentityViewModel<Department> {
    private SimpleStringProperty name = new SimpleStringProperty();
    private SimpleObjectProperty<Employee> leader = new SimpleObjectProperty<>();
    private SimpleStringProperty code = new SimpleStringProperty();
    private SimpleBooleanProperty isActive = new SimpleBooleanProperty();

    public static DepartmentViewModel from(Department v) {
        DepartmentViewModel model = new DepartmentViewModel();
        model.update(v);
        return model;
    }

    @Override
    protected void updateFrom(Department v) {
        super.updateFrom(v);
        getName().set(v.getName());
        getCode().set(v.getCode());
        getLeader().set(v.getLeader());
        getIsActive().set(v.isActive());
    }

    @Override
    public boolean copyTo(Department v) {
        if (v == null) return false;

        boolean modified = super.copyTo(v);
        if (!Objects.equals(name.get(), v.getName())) {
            v.setName(name.get());
            modified = true;
        }
        if (!Objects.equals(code.get(), v.getCode())) {
            v.setCode(code.get());
            modified = true;
        }
        if (!Objects.equals(leader.get(), v.getLeader())) {
            v.setLeader(leader.get());
            modified = true;
        }
        if (!Objects.equals(isActive.get(), v.isActive())) {
            v.setActive(isActive.get());
            modified = true;
        }

        return modified;
    }
}
