package com.ecep.contract.vm;

import java.time.LocalDateTime;
import java.util.Objects;

import com.ecep.contract.vo.EmployeeLoginHistoryVo;

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

@Data
@EqualsAndHashCode(callSuper = false)
public class EmployeeLoginHistoryViewModel extends IdentityViewModel<EmployeeLoginHistoryVo> implements EmployeeBasedViewModel {
    private SimpleStringProperty ip = new SimpleStringProperty();
    private SimpleStringProperty mac = new SimpleStringProperty();

    private SimpleObjectProperty<Integer> employee = new SimpleObjectProperty<>();

    private SimpleObjectProperty<LocalDateTime> loginTime = new SimpleObjectProperty<>();
    private SimpleObjectProperty<LocalDateTime> activeTime = new SimpleObjectProperty<>();


    /**
     * 从Vo创建ViewModel
     */
    public static EmployeeLoginHistoryViewModel from(EmployeeLoginHistoryVo v) {
        EmployeeLoginHistoryViewModel vm = new EmployeeLoginHistoryViewModel();
        vm.updateFrom(v);
        return vm;
    }


    @Override
    protected void updateFrom(EmployeeLoginHistoryVo v) {
        super.updateFrom(v);
        getIp().set(v.getIp());
        getMac().set(v.getMac());
        getEmployee().set(v.getEmployeeId());
        getLoginTime().set(v.getLoginTime());
        getActiveTime().set(v.getActiveTime());
    }

    @Override
    public boolean copyTo(EmployeeLoginHistoryVo v) {
        boolean modified = super.copyTo(v);
        if (!Objects.equals(getIp().get(), v.getIp())) {
            v.setIp(getIp().get());
            modified = true;
        }
        if (!Objects.equals(getMac().get(), v.getMac())) {
            v.setMac(getMac().get());
            modified = true;
        }
        if (!Objects.equals(getEmployee().get(), v.getEmployeeId())) {
            v.setEmployeeId(getEmployee().get());
            modified = true;
        }
        if (!Objects.equals(getLoginTime().get(), v.getLoginTime())) {
            v.setLoginTime(getLoginTime().get());
            modified = true;
        }
        if (!Objects.equals(getActiveTime().get(), v.getActiveTime())) {
            v.setActiveTime(getActiveTime().get());
            modified = true;
        }
        return modified;
    }
}
