package com.arrebol.taxiservicios.beans.system;

import com.arrebol.car.driver.security.CarDriverSecurity;
import com.arrebol.taxiservicios.beans.TaxiGenericBean;
import com.arrebol.taxiservicios.controller.catalog.LocationController;
import com.arrebol.taxiservicios.controller.system.UserHasPermissionController;
import com.arrebol.taxiservicios.model.catalog.Location;
import com.arrebol.taxiservicios.model.core.Permission;
import com.arrebol.taxiservicios.model.core.User;
import com.arrebol.taxiservicios.model.core.UserHasPermission;
import com.arrebol.taxiservicios.model.core.UserHasPermissionId;
import com.arrebol.taxiservicios.model.enums.UserStatusType;
import com.arrebol.taxiservicios.wrapper.UserWrapper;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.primefaces.model.DualListModel;

/**
 *
 * @author Carlos Janitzio Zavala Lopez
 */
@Named
@ViewScoped
public class UserManagerBean extends TaxiGenericBean implements Serializable {

    /**
     *
     */
    public void changePassword() {
        try {

            if (validatePasswordRules(getPassword(), getPasswordConfirmation())
                    && getController().updatePassword(getUserIdToChangePwd(),
                            new CarDriverSecurity(APP, getPassword()).getPassword())) {
                showMessage(FacesMessage.SEVERITY_INFO,
                        getBundlePropertyFile().getString("user.manager.title"),
                        getBundlePropertyFile().getString("privacy.password.confirmed.details"));
            } else {
                logger.error("changePassword: false");
                showMessage(FacesMessage.SEVERITY_ERROR,
                        getBundlePropertyFile().getString("user.manager.title"),
                        getBundlePropertyFile().getString("privacy.password.confirmed.details.error"));
            }
        } catch (Exception e) {
            logger.error("changePassword: " + e);
            showMessage(FacesMessage.SEVERITY_FATAL,
                    getBundlePropertyFile().getString("user.manager.title"),
                    getBundlePropertyFile().getString("fatal.access.details"));
        }
    }

    public void changeSuperAdmin() {
        try {

            if (getController().updateSuperAdmin(getUserIdToUpdateSuperAdmin(),
                    isSuperAdmin())) {

                initUpdateSuperAdmin();
                showMessage(FacesMessage.SEVERITY_INFO,
                        getBundlePropertyFile().getString("user.manager.title"),
                        "Se actualizaron los permisos de super administrador");
            } else {
                logger.error("changeLocation: false");
                showMessage(FacesMessage.SEVERITY_ERROR,
                        getBundlePropertyFile().getString("user.manager.title"),
                        "No se pudo actualizar los permisos de super administrador");
            }
        } catch (Exception e) {
            logger.error("changeSuperAdmin: " + e);
            showMessage(FacesMessage.SEVERITY_FATAL,
                    getBundlePropertyFile().getString("user.manager.title"),
                    getBundlePropertyFile().getString("fatal.access.details"));
        }
    }

    public void changeLocation() {
        try {

            if (getController().updateLocation(getUserIdToChangePwd(),
                    getLocationId())) {
                showMessage(FacesMessage.SEVERITY_INFO,
                        getBundlePropertyFile().getString("user.manager.title"),
                        "Se actualizó la localidad correctamente");
            } else {
                logger.error("changeLocation: false");
                showMessage(FacesMessage.SEVERITY_ERROR,
                        getBundlePropertyFile().getString("user.manager.title"),
                        "No se pudo actualizar la localidad");
            }
        } catch (Exception e) {
            logger.error("changePassword: " + e);
            showMessage(FacesMessage.SEVERITY_FATAL,
                    getBundlePropertyFile().getString("user.manager.title"),
                    getBundlePropertyFile().getString("fatal.access.details"));
        }
    }

    /**
     * After inactive selected user the process will update pwdUserList,
     * userReactiveLst, userDeleteLst and It self.
     */
    public void changeUserToInactiveStatus() {
        logger.info("changeUserToInactiveStatus");
        try {
            if (getController().changeUserStatusByStatus(getUserIdToInactive(),
                    getLoggedUser().getId(), UserStatusType.DISABLED)) {
                initUserToChangePwdAndToInactiveStatusList();
                initUserToReactiveStatusList();
                initUserToDeleteStatusList();
                initUpdatePermissions();

                showMessage(FacesMessage.SEVERITY_INFO,
                        getBundlePropertyFile().getString("user.manager.title"),
                        getBundlePropertyFile().getString("user.manager.success.change.status.to.inactive"));
            } else {
                logger.error("changeUserToInactiveStatus" + UserStatusType.DISABLED);
                showMessage(FacesMessage.SEVERITY_ERROR,
                        getBundlePropertyFile().getString("user.manager.title"),
                        getBundlePropertyFile().getString("user.manager.fail.change.status.to.inactive"));
            }
        } catch (Exception e) {
            logger.error("changeUserToInactiveStatus: " + e);
            showMessage(FacesMessage.SEVERITY_FATAL,
                    getBundlePropertyFile().getString("user.manager.title"),
                    getBundlePropertyFile().getString("fatal.access.details"));
        }

    }

    /**
     * After reactive selected user the process will update pwdUserList,
     * userInactiveLst, userDeleteLst and It self.
     */
    public void changeUserToReactiveStatus() {
        logger.info("changeUserToReactiveStatus");
        try {
            if (getController().changeUserStatusByStatus(getUserIdToReactive(),
                    getLoggedUser().getId(), UserStatusType.ENABLED)) {
                initUserToChangePwdAndToInactiveStatusList();
                initUserToReactiveStatusList();
                initUserToDeleteStatusList();
                initUpdatePermissions();

                showMessage(FacesMessage.SEVERITY_INFO,
                        getBundlePropertyFile().getString("user.manager.title"),
                        getBundlePropertyFile().getString("user.manager.success.change.status.to.reactive"));
            } else {
                logger.error("changeUserToReactiveStatus" + UserStatusType.ENABLED);
                showMessage(FacesMessage.SEVERITY_ERROR,
                        getBundlePropertyFile().getString("user.manager.title"),
                        getBundlePropertyFile().getString("user.manager.fail.change.status.to.reactive"));
            }
        } catch (Exception e) {
            logger.error("changeUserToReactiveStatus: " + e);
            showMessage(FacesMessage.SEVERITY_FATAL,
                    getBundlePropertyFile().getString("user.manager.title"),
                    getBundlePropertyFile().getString("fatal.access.details"));
        }
    }

    /**
     * After remove selected user the process will update pwdUserList,
     * userReactiveLst, userInactiveLst and It self.
     */
    public void changeUserToDeleteStatus() {
        logger.info("changeUserToDeleteStatus");
        try {
            if (getController().changeUserStatusByStatus(getUserIdToDelete(),
                    getLoggedUser().getId(), UserStatusType.DELETED)) {
                initUserToChangePwdAndToInactiveStatusList();
                initUserToReactiveStatusList();
                initUserToDeleteStatusList();
                initUpdatePermissions();

                showMessage(FacesMessage.SEVERITY_INFO,
                        getBundlePropertyFile().getString("user.manager.title"),
                        getBundlePropertyFile().getString("user.manager.success.change.status.to.delete"));
            } else {
                logger.error("changeUserToDeleteStatus" + UserStatusType.DELETED);
                showMessage(FacesMessage.SEVERITY_ERROR,
                        getBundlePropertyFile().getString("user.manager.title"),
                        getBundlePropertyFile().getString("user.manager.fail.change.status.to.delete"));
            }
        } catch (Exception e) {
            logger.error("changeUserToDeleteStatus: " + e);
            showMessage(FacesMessage.SEVERITY_FATAL,
                    getBundlePropertyFile().getString("user.manager.title"),
                    getBundlePropertyFile().getString("fatal.access.details"));
        }
    }

    public void loadPermissionsByUser() {
        logger.info("loadPermissionsByUser");
        try {
            setUserPermissions(getController().findPermissionByUserId(getUserIdToUpdatePermissions(), Boolean.TRUE));
            setUserMissingPermissions(getController().findPermissionByUserId(getUserIdToUpdatePermissions(), Boolean.FALSE));

            setDualPermissionLst(new DualListModel<>(getUserMissingPermissions(), getUserPermissions()));
            setActivateUpdatePermissionBtn(Boolean.TRUE);
        } catch (Exception e) {
            logger.error("loadPermissionsByUser: " + e);
            showMessage(FacesMessage.SEVERITY_FATAL,
                    getBundlePropertyFile().getString("user.manager.title"),
                    getBundlePropertyFile().getString("fatal.access.details"));
        }
    }

    public void updatePermissionsByUser() {
        logger.info("updatePermissionsByUser");
        try {
            if (!"N/A".equals(getUserIdToUpdatePermissions())) {

                if (getController().updatePermissionsByUser(getUserIdToUpdatePermissions(), getAllNewPermissions())) {
                    showMessage(FacesMessage.SEVERITY_INFO,
                            getBundlePropertyFile().getString("user.manager.title"),
                            getBundlePropertyFile().getString("user.manager.success.update.permisions.by.user"));
                } else {
                    logger.error("updatePermissionsByUser" + UserStatusType.DELETED);
                    showMessage(FacesMessage.SEVERITY_ERROR,
                            getBundlePropertyFile().getString("user.manager.title"),
                            getBundlePropertyFile().getString("user.manager.fail.update.permisions.by.user"));
                }
            } else {
                logger.error("updatePermissionsByUser" + UserStatusType.DELETED);
                showMessage(FacesMessage.SEVERITY_ERROR,
                        getBundlePropertyFile().getString("user.manager.title"),
                        getBundlePropertyFile().getString("user.manager.fail.update.permisions.by.user"));
            }
        } catch (Exception e) {
            logger.error("updatePermissionsByUser: " + e);
            showMessage(FacesMessage.SEVERITY_FATAL,
                    getBundlePropertyFile().getString("user.manager.title"),
                    getBundlePropertyFile().getString("fatal.access.details"));
        }
    }

    /**
     *
     */
    public void onSelectUserPassword() {
        logger.info("onSelectUserPassword");
        try {
            UserWrapper wrapper;
            if ("N/A".equals(getUserIdToChangePwd())) {
                wrapper = new UserWrapper(
                        getBundlePropertyFile().getString("user.wrapper.role"),
                        getBundlePropertyFile().getString("user.wrapper.fullname"),
                        getBundlePropertyFile().getString("user.wrapper.avatar"));
            } else {
                int positionToChangePwd = getUserLstToChangePwd().indexOf(new User(getUserIdToChangePwd(), ""));
                wrapper = new UserWrapper(getUserLstToChangePwd().get(positionToChangePwd));
                setLocationId(getUserLstToChangePwd().get(positionToChangePwd).getLocation().getId());
            }
            setUserWrapperToChangePwd(wrapper);

        } catch (Exception e) {
            logger.error("onSelectUserPassword: " + e);
            showMessage(FacesMessage.SEVERITY_ERROR,
                    getBundlePropertyFile().getString("user.manager.title"),
                    getBundlePropertyFile().getString("fatal.access.details"));
        }
    }

    public void onSelectUserSuperAdmin() {
        logger.info("onSelectUserSuperAdmin");
        try {
            UserWrapper wrapper;
            if ("N/A".equals(getUserIdToUpdateSuperAdmin())) {
                wrapper = new UserWrapper(getBundlePropertyFile().getString("user.wrapper.role"), getBundlePropertyFile().getString("user.wrapper.fullname"), getBundlePropertyFile().getString("user.wrapper.avatar"));
            } else {
                int positionToChangeSuperAdmin = getUserLstToUpdateSuperAdmin().indexOf(new User(getUserIdToUpdateSuperAdmin(), ""));
                wrapper = new UserWrapper(getUserLstToUpdateSuperAdmin().get(positionToChangeSuperAdmin));
                setSuperAdmin(getUserLstToUpdateSuperAdmin().get(positionToChangeSuperAdmin).isAjustador());
            }
            setUserWrapperToChangeSuperAdmin(wrapper);
        } catch (Exception e) {
            logger.error("onSelectUserSuperAdmin: " + e);
            showMessage(FacesMessage.SEVERITY_ERROR, getBundlePropertyFile().getString("user.manager.title"), getBundlePropertyFile().getString("fatal.access.details"));
        }
    }

    /**
     *
     */
    public void onSelectUserToInactiveStatus() {
        logger.info("onSelectUserToInactiveStatus");
        try {
            UserWrapper wrapper;
            if ("N/A".equals(getUserIdToInactive())) {
                wrapper = new UserWrapper(
                        getBundlePropertyFile().getString("user.wrapper.role"),
                        getBundlePropertyFile().getString("user.wrapper.fullname"),
                        getBundlePropertyFile().getString("user.wrapper.avatar"));
            } else {
                int positionToInactive = getUserLstToInactive().indexOf(new User(getUserIdToInactive(), ""));
                wrapper = new UserWrapper(getUserLstToInactive().get(positionToInactive));
            }
            setUserWrapperToInactive(wrapper);
        } catch (Exception e) {
            logger.error("onSelectUserToInactiveStatus: " + e);
            showMessage(FacesMessage.SEVERITY_ERROR,
                    getBundlePropertyFile().getString("user.manager.title"),
                    getBundlePropertyFile().getString("fatal.access.details"));
        }
    }

    /**
     *
     */
    public void onSelectUserToReactiveStatus() {
        logger.info("onSelectUserToReactiveStatus");
        try {
            UserWrapper wrapper;
            if ("N/A".equals(getUserIdToReactive())) {
                wrapper = new UserWrapper(
                        getBundlePropertyFile().getString("user.wrapper.role"),
                        getBundlePropertyFile().getString("user.wrapper.fullname"),
                        getBundlePropertyFile().getString("user.wrapper.avatar"));
            } else {
                int positionToReactive = getUserLstToReactive().indexOf(new User(getUserIdToReactive(), ""));
                wrapper = new UserWrapper(getUserLstToReactive().get(positionToReactive));
            }
            setUserWrapperToReactive(wrapper);
        } catch (Exception e) {
            logger.error("onSelectUserToReactiveStatus: " + e);
            showMessage(FacesMessage.SEVERITY_ERROR,
                    getBundlePropertyFile().getString("user.manager.title"),
                    getBundlePropertyFile().getString("fatal.access.details"));
        }
    }

    /**
     *
     */
    public void onSelectUserToDeleteStatus() {
        logger.info("onSelectUserToDeleteStatus");
        try {
            UserWrapper wrapper;
            if ("N/A".equals(getUserIdToDelete())) {
                wrapper = new UserWrapper(
                        getBundlePropertyFile().getString("user.wrapper.role"),
                        getBundlePropertyFile().getString("user.wrapper.fullname"),
                        getBundlePropertyFile().getString("user.wrapper.avatar"));
            } else {
                int positionToDelete = getUserLstToDelete().indexOf(new User(getUserIdToDelete(), ""));
                wrapper = new UserWrapper(getUserLstToDelete().get(positionToDelete));
            }
            setUserWrapperToDelete(wrapper);
        } catch (Exception e) {
            logger.error("onSelectUserToDeleteStatus: " + e);
            showMessage(FacesMessage.SEVERITY_ERROR,
                    getBundlePropertyFile().getString("user.manager.title"),
                    getBundlePropertyFile().getString("fatal.access.details"));
        }
    }

    public void onSelectUserToUpdatePermissions() {
        logger.info("onSelectUserToUpdatePermissions");
        try {
            UserWrapper wrapper;
            if ("N/A".equals(getUserIdToUpdatePermissions())) {
                wrapper = new UserWrapper(
                        getBundlePropertyFile().getString("user.wrapper.role"),
                        getBundlePropertyFile().getString("user.wrapper.fullname"),
                        getBundlePropertyFile().getString("user.wrapper.avatar"));
            } else {
                int positionToUpdatePermission = getUserLstToUpdatePermissions().indexOf(new User(getUserIdToUpdatePermissions(), ""));
                wrapper = new UserWrapper(getUserLstToUpdatePermissions().get(positionToUpdatePermission));
            }
            setUserWrapperToUpdatePermission(wrapper);
        } catch (Exception e) {
            logger.error("onSelectUserToUpdatePermissions: " + e);
            showMessage(FacesMessage.SEVERITY_ERROR,
                    getBundlePropertyFile().getString("user.manager.title"),
                    getBundlePropertyFile().getString("fatal.access.details"));
        }
    }

    private void initUserToChangePwdAndToInactiveStatusList() {
        logger.info("initUserToChangePwdAndToInactiveStatusList");
        try {
            List<UserStatusType> activeUserStatus = new ArrayList<>();
            activeUserStatus.add(UserStatusType.ENABLED);

            List<User> userLstToShare = getController().findUsersNotRootByInStatus(getLoggedUser().getId(), activeUserStatus, getLoggedUser().getLocation());

            setUserLstToChangePwd(userLstToShare);
            setUserLstToInactive(userLstToShare);
            setUserIdToChangePwd("N/A");
            setUserWrapperToChangePwd(new UserWrapper(
                    getBundlePropertyFile().getString("user.wrapper.role"),
                    getBundlePropertyFile().getString("user.wrapper.fullname"),
                    getBundlePropertyFile().getString("user.wrapper.avatar")));
            setUserIdToInactive("N/A");
            setUserWrapperToInactive(new UserWrapper(
                    getBundlePropertyFile().getString("user.wrapper.role"),
                    getBundlePropertyFile().getString("user.wrapper.fullname"),
                    getBundlePropertyFile().getString("user.wrapper.avatar")));
        } catch (Exception e) {
            logger.error("initUserToChangePwdAndToInactiveStatusList: " + e);
        }
    }

    /**
     * Init selectOneMenu users list to reactive.
     */
    private void initUserToReactiveStatusList() {
        logger.info("initUserToReactiveStatusList");
        try {
            List<UserStatusType> userToReactiveStatusList = new ArrayList<>();

            userToReactiveStatusList.add(UserStatusType.DISABLED);

            setUserLstToReactive(getController().findUsersNotRootByInStatus(getLoggedUser().getId(), userToReactiveStatusList, getLoggedUser().getLocation()));
            setUserIdToReactive("N/A");
            setUserWrapperToReactive(new UserWrapper(
                    getBundlePropertyFile().getString("user.wrapper.role"),
                    getBundlePropertyFile().getString("user.wrapper.fullname"),
                    getBundlePropertyFile().getString("user.wrapper.avatar")));
        } catch (Exception e) {
            logger.error("initUserToReactiveStatusList: " + e);
        }
    }

    /**
     * Init selectOneMenu users list to delete.
     */
    private void initUserToDeleteStatusList() {
        logger.info("initUserToDeleteStatusList");
        try {
            List<UserStatusType> userToDeleteStatusList = new ArrayList<>();

            userToDeleteStatusList.add(UserStatusType.ENABLED);
            userToDeleteStatusList.add(UserStatusType.DISABLED);

            setUserLstToDelete(getController().findUsersNotRootByInStatus(getLoggedUser().getId(), userToDeleteStatusList, getLoggedUser().getLocation()));
            setUserIdToDelete("N/A");
            setUserWrapperToDelete(new UserWrapper(
                    getBundlePropertyFile().getString("user.wrapper.role"),
                    getBundlePropertyFile().getString("user.wrapper.fullname"),
                    getBundlePropertyFile().getString("user.wrapper.avatar")));
        } catch (Exception e) {
            logger.error("initUserToDeleteStatusList: " + e);
        }
    }

    private void initUpdatePermissions() {
        logger.info("initUpdatePermissions");
        try {
            List<UserStatusType> userToDeleteStatusList = new ArrayList<>();

            userToDeleteStatusList.add(UserStatusType.ENABLED);

            setUserLstToUpdatePermissions(getController().findUsersNotRootByInStatus(getLoggedUser().getId(), userToDeleteStatusList, getLoggedUser().getLocation()));
            setUserIdToUpdatePermissions("N/A");
            setUserWrapperToUpdatePermission(new UserWrapper(
                    getBundlePropertyFile().getString("user.wrapper.role"),
                    getBundlePropertyFile().getString("user.wrapper.fullname"),
                    getBundlePropertyFile().getString("user.wrapper.avatar")));
            setDualPermissionLst(new DualListModel<>());
        } catch (Exception e) {
            logger.error("initUpdatePermissions: " + e);
        }
    }

    private void initUpdateSuperAdmin() {
        logger.info("initUpdatePermissions");
        try {
            List<UserStatusType> userToDeleteStatusList = new ArrayList<>();

            userToDeleteStatusList.add(UserStatusType.ENABLED);

            setUserLstToUpdateSuperAdmin(getController().findUsersNotRootByInStatus(getLoggedUser().getId(), userToDeleteStatusList, getLoggedUser().getLocation()));
            setUserIdToUpdateSuperAdmin("N/A");
            setUserWrapperToChangeSuperAdmin(new UserWrapper(
                    getBundlePropertyFile().getString("user.wrapper.role"),
                    getBundlePropertyFile().getString("user.wrapper.fullname"),
                    getBundlePropertyFile().getString("user.wrapper.avatar")));

        } catch (Exception e) {
            logger.error("initUpdatePermissions: " + e);
        }
    }

    public void findPermissionByUser() {
        logger.info("loadPermissionByUser");
        try {
            setUserPermissions(getController().findPermissionByUserId(getUserIdToUpdatePermissions(), Boolean.TRUE));
            setUserMissingPermissions(getController().findPermissionByUserId(getUserIdToUpdatePermissions(), Boolean.FALSE));
        } catch (Exception e) {
            logger.error("loadPermissionByUser", e);
        }
    }

    private synchronized Set<UserHasPermission> getAllNewPermissions() {
        logger.info("getAllNewPermissions");

        Set<UserHasPermission> allNewPermissions = new HashSet<UserHasPermission>();
        try {
            Set<Permission> uniquePermission = new HashSet<>();

            List<Permission> appMenuLst = findApplicationMenuPermissions();

            getDualPermissionLst().getTarget().forEach((permission) -> {
                if (!uniquePermission.contains(permission)) {
                    uniquePermission.add(permission);
                }

                Optional<Permission> optional = appMenuLst.stream()
                        .filter((appMenu) -> appMenu.getName().equals(permission.getParentName()))
                        .findAny();

                if (optional.isPresent()) {
                    if (!uniquePermission.contains(optional.get())) {
                        uniquePermission.add(optional.get());
                    }
                }
            });

            uniquePermission.forEach((unique) -> {
                allNewPermissions.add(
                        new UserHasPermission(
                                new UserHasPermissionId(getUserIdToUpdatePermissions(), unique.getId())
                        )
                );
            });
        } catch (Exception e) {
            logger.error("getAllNewPermissions", e);
            throw e;
        }

        return allNewPermissions;
    }

    private List<Permission> findApplicationMenuPermissions() {
        logger.info("findApplicationMenuPermissions");

        List<Permission> appMenus = new ArrayList<>();
        try {
            appMenus = getController().findApplicationMenuPermissions(Boolean.FALSE, Boolean.TRUE, Boolean.TRUE);
        } catch (Exception e) {
            logger.error("findApplicationMenuPermissions", e);
        }

        return appMenus;
    }

    private static final long serialVersionUID = 3276455861145411383L;

    final Logger logger = LogManager.getLogger(UserManagerBean.class);

    private UserHasPermissionController controller;

    private List<User> userLstToChangePwd;
    private List<User> userLstToInactive;
    private List<User> userLstToReactive;
    private List<User> userLstToDelete;
    private List<User> userLstToUpdatePermissions;
    private List<User> userLstToUpdateSuperAdmin;

    private String userIdToChangePwd;
    private String userIdToInactive;
    private String userIdToReactive;
    private String userIdToDelete;
    private String userIdToUpdatePermissions;
    private String userIdToUpdateSuperAdmin;

    private UserWrapper userWrapperToChangePwd;
    private UserWrapper userWrapperToChangeSuperAdmin;
    private UserWrapper userWrapperToInactive;
    private UserWrapper userWrapperToReactive;
    private UserWrapper userWrapperToDelete;
    private UserWrapper userWrapperToUpdatePermission;

    private String password;
    private String passwordConfirmation;

    private UserStatusType statusType;

    private List<Permission> userPermissions;
    private List<Permission> userMissingPermissions;

    private DualListModel<Permission> dualPermissionLst;

    private boolean activateUpdatePermissionBtn;
    private boolean superAdmin;

    private String locationId;
    private LocationController locationController;
    private List<Location> locations;

    @PostConstruct
    public void init() {
        loadBundlePropertyFile();
        try {
            setController(new UserHasPermissionController());
            setLocationController(new LocationController());

            setLocations(getLocationController().fillLocationDatatable());
            initUserToChangePwdAndToInactiveStatusList();
            initUserToReactiveStatusList();
            initUserToDeleteStatusList();
            initUpdatePermissions();
            initUpdateSuperAdmin();
        } catch (Exception e) {
            showMessage(FacesMessage.SEVERITY_ERROR,
                    getBundlePropertyFile().getString("user.manager.title"),
                    getBundlePropertyFile().getString("user.manager.fail.desc"));
        }
    }

    public LocationController getLocationController() {
        return locationController;
    }

    public void setLocationController(LocationController locationController) {
        this.locationController = locationController;
    }

    public List<Location> getLocations() {
        return locations;
    }

    public void setLocations(List<Location> locations) {
        this.locations = locations;
    }

    public String getLocationId() {
        return locationId;
    }

    public void setLocationId(String locationId) {
        this.locationId = locationId;
    }

    public UserHasPermissionController getController() {
        return controller;
    }

    public void setController(UserHasPermissionController controller) {
        this.controller = controller;
    }

    public List<User> getUserLstToChangePwd() {
        return userLstToChangePwd;
    }

    public void setUserLstToChangePwd(List<User> userLstToChangePwd) {
        this.userLstToChangePwd = userLstToChangePwd;
    }

    public List<User> getUserLstToInactive() {
        return userLstToInactive;
    }

    public void setUserLstToInactive(List<User> userLstToInactive) {
        this.userLstToInactive = userLstToInactive;
    }

    public List<User> getUserLstToReactive() {
        return userLstToReactive;
    }

    public void setUserLstToReactive(List<User> userLstToReactive) {
        this.userLstToReactive = userLstToReactive;
    }

    public List<User> getUserLstToDelete() {
        return userLstToDelete;
    }

    public void setUserLstToDelete(List<User> userLstToDelete) {
        this.userLstToDelete = userLstToDelete;
    }

    public List<User> getUserLstToUpdatePermissions() {
        return userLstToUpdatePermissions;
    }

    public void setUserLstToUpdatePermissions(List<User> userLstToUpdatePermissions) {
        this.userLstToUpdatePermissions = userLstToUpdatePermissions;
    }

    public String getUserIdToChangePwd() {
        return userIdToChangePwd;
    }

    public void setUserIdToChangePwd(String userIdToChangePwd) {
        this.userIdToChangePwd = userIdToChangePwd;
    }

    public String getUserIdToInactive() {
        return userIdToInactive;
    }

    public void setUserIdToInactive(String userIdToInactive) {
        this.userIdToInactive = userIdToInactive;
    }

    public String getUserIdToReactive() {
        return userIdToReactive;
    }

    public void setUserIdToReactive(String userIdToReactive) {
        this.userIdToReactive = userIdToReactive;
    }

    public String getUserIdToDelete() {
        return userIdToDelete;
    }

    public void setUserIdToDelete(String userIdToDelete) {
        this.userIdToDelete = userIdToDelete;
    }

    public String getUserIdToUpdatePermissions() {
        return userIdToUpdatePermissions;
    }

    public void setUserIdToUpdatePermissions(String userIdToUpdatePermissions) {
        this.userIdToUpdatePermissions = userIdToUpdatePermissions;
    }

    public UserWrapper getUserWrapperToChangePwd() {
        if (null == userWrapperToChangePwd) {
            userWrapperToChangePwd = new UserWrapper(
                    getBundlePropertyFile().getString("user.wrapper.role"),
                    getBundlePropertyFile().getString("user.wrapper.fullname"),
                    getBundlePropertyFile().getString("user.wrapper.avatar"));
        }
        return userWrapperToChangePwd;
    }

    public void setUserWrapperToChangePwd(UserWrapper userWrapperToChangePwd) {
        this.userWrapperToChangePwd = userWrapperToChangePwd;
    }

    public UserWrapper getUserWrapperToInactive() {
        if (null == userWrapperToInactive) {
            userWrapperToInactive = new UserWrapper(
                    getBundlePropertyFile().getString("user.wrapper.role"),
                    getBundlePropertyFile().getString("user.wrapper.fullname"),
                    getBundlePropertyFile().getString("user.wrapper.avatar"));
        }
        return userWrapperToInactive;
    }

    public void setUserWrapperToInactive(UserWrapper userWrapperToInactive) {
        this.userWrapperToInactive = userWrapperToInactive;
    }

    public UserWrapper getUserWrapperToReactive() {
        if (null == userWrapperToReactive) {
            userWrapperToReactive = new UserWrapper(
                    getBundlePropertyFile().getString("user.wrapper.role"),
                    getBundlePropertyFile().getString("user.wrapper.fullname"),
                    getBundlePropertyFile().getString("user.wrapper.avatar"));
        }
        return userWrapperToReactive;
    }

    public void setUserWrapperToReactive(UserWrapper userWrapperToReactive) {
        this.userWrapperToReactive = userWrapperToReactive;
    }

    public UserWrapper getUserWrapperToDelete() {
        if (null == userWrapperToDelete) {
            userWrapperToDelete = new UserWrapper(
                    getBundlePropertyFile().getString("user.wrapper.role"),
                    getBundlePropertyFile().getString("user.wrapper.fullname"),
                    getBundlePropertyFile().getString("user.wrapper.avatar"));
        }
        return userWrapperToDelete;
    }

    public void setUserWrapperToDelete(UserWrapper userWrapperToDelete) {
        this.userWrapperToDelete = userWrapperToDelete;
    }

    public UserWrapper getUserWrapperToUpdatePermission() {
        if (null == userWrapperToUpdatePermission) {
            userWrapperToUpdatePermission = new UserWrapper(
                    getBundlePropertyFile().getString("user.wrapper.role"),
                    getBundlePropertyFile().getString("user.wrapper.fullname"),
                    getBundlePropertyFile().getString("user.wrapper.avatar"));
        }
        return userWrapperToUpdatePermission;
    }

    public void setUserWrapperToUpdatePermission(UserWrapper userWrapperToUpdatePermission) {
        this.userWrapperToUpdatePermission = userWrapperToUpdatePermission;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPasswordConfirmation() {
        return passwordConfirmation;
    }

    public void setPasswordConfirmation(String passwordConfirmation) {
        this.passwordConfirmation = passwordConfirmation;
    }

    public void setStatusType(UserStatusType statusType) {
        this.statusType = statusType;
    }

    public UserStatusType getStatusType() {
        return statusType;
    }

    public List<Permission> getUserPermissions() {
        return userPermissions;
    }

    public void setUserPermissions(List<Permission> userPermissions) {
        this.userPermissions = userPermissions;
    }

    public List<Permission> getUserMissingPermissions() {
        return userMissingPermissions;
    }

    public void setUserMissingPermissions(List<Permission> userMissingPermissions) {
        this.userMissingPermissions = userMissingPermissions;
    }

    public void setDualPermissionLst(DualListModel<Permission> dualPermissionLst) {
        this.dualPermissionLst = dualPermissionLst;
    }

    public DualListModel<Permission> getDualPermissionLst() {
        return dualPermissionLst;
    }

    public boolean isActivateUpdatePermissionBtn() {
        return activateUpdatePermissionBtn;
    }

    public void setActivateUpdatePermissionBtn(boolean activateUpdatePermissionBtn) {
        this.activateUpdatePermissionBtn = activateUpdatePermissionBtn;
    }

    public String getUserIdToUpdateSuperAdmin() {
        return userIdToUpdateSuperAdmin;
    }

    public void setUserIdToUpdateSuperAdmin(String userIdToUpdateSuperAdmin) {
        this.userIdToUpdateSuperAdmin = userIdToUpdateSuperAdmin;
    }

    public UserWrapper getUserWrapperToChangeSuperAdmin() {
        return userWrapperToChangeSuperAdmin;
    }

    public void setUserWrapperToChangeSuperAdmin(UserWrapper userWrapperToChangeSuperAdmin) {
        this.userWrapperToChangeSuperAdmin = userWrapperToChangeSuperAdmin;
    }

    public List<User> getUserLstToUpdateSuperAdmin() {
        return userLstToUpdateSuperAdmin;
    }

    public void setUserLstToUpdateSuperAdmin(List<User> userLstToUpdateSuperAdmin) {
        this.userLstToUpdateSuperAdmin = userLstToUpdateSuperAdmin;
    }

    public boolean isSuperAdmin() {
        return superAdmin;
    }

    public void setSuperAdmin(boolean superAdmin) {
        this.superAdmin = superAdmin;
    }

}
