package com.ecep.contract.vm;

import java.time.LocalDate;
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 EmployeeViewModel extends IdentityViewModel<Employee> {
    private SimpleStringProperty account = new SimpleStringProperty();
    private SimpleStringProperty name = new SimpleStringProperty();
    private SimpleObjectProperty<Department> department = new SimpleObjectProperty<>();
    private SimpleStringProperty alias = new SimpleStringProperty();
    private SimpleStringProperty code = new SimpleStringProperty();
    private SimpleStringProperty email = new SimpleStringProperty();
    private SimpleStringProperty phone = new SimpleStringProperty();
    private SimpleObjectProperty<LocalDate> created = new SimpleObjectProperty<>();
    private SimpleObjectProperty<LocalDate> entryDate = new SimpleObjectProperty<>();
    private SimpleObjectProperty<LocalDate> leaveDate = new SimpleObjectProperty<>();
    private SimpleBooleanProperty isActive = new SimpleBooleanProperty();

    public static EmployeeViewModel from(Employee v) {
        EmployeeViewModel model = new EmployeeViewModel();
        model.update(v);
        return model;
    }

    @Override
    protected void updateFrom(Employee v) {
        super.updateFrom(v);
        getAccount().set(v.getAccount());
        getName().set(v.getName());
        getDepartment().set(v.getDepartment());
        // getRoles().set(FXCollections.observableArrayList(v.getRoles()));
        getAlias().set(v.getAlias());
        getCode().set(v.getCode());
        getEmail().set(v.getEmail());
        getPhone().set(v.getPhone());
        getCreated().set(v.getCreated());
        getEntryDate().set(v.getEntryDate());
        getLeaveDate().set(v.getLeaveDate());
        getIsActive().set(v.isActive());
    }

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

        boolean modified = super.copyTo(v);
        if (!Objects.equals(account.get(), v.getAccount())) {
            v.setAccount(account.get());
            modified = true;
        }
        if (!Objects.equals(name.get(), v.getName())) {
            v.setName(name.get());
            modified = true;
        }
        if (!Objects.equals(department.get(), v.getDepartment())) {
            v.setDepartment(department.get());
            modified = true;
        }
        // if (!Objects.equals(roles.get(), v.getRoles())) {
        // v.setRoles(roles.get());
        // modified = true;
        // }
        if (!Objects.equals(alias.get(), v.getAlias())) {
            v.setAlias(alias.get());
            modified = true;
        }
        if (!Objects.equals(code.get(), v.getCode())) {
            v.setCode(code.get());
            modified = true;
        }
        if (!Objects.equals(email.get(), v.getEmail())) {
            v.setEmail(email.get());
            modified = true;
        }
        if (!Objects.equals(phone.get(), v.getPhone())) {
            v.setPhone(phone.get());
            modified = true;
        }
        if (!Objects.equals(created.get(), v.getCreated())) {
            v.setCreated(created.get());
            modified = true;
        }
        if (!Objects.equals(entryDate.get(), v.getEntryDate())) {
            v.setEntryDate(entryDate.get());
            modified = true;
        }
        if (!Objects.equals(leaveDate.get(), v.getLeaveDate())) {
            v.setLeaveDate(leaveDate.get());
            modified = true;
        }
        if (!Objects.equals(isActive.get(), v.isActive())) {
            v.setActive(isActive.get());
            modified = true;
        }
        return modified;
    }
}
