package com.arrebol.taxiservicios.beans.system;

import com.arrebol.taxiservicios.beans.TaxiGenericBean;
import com.arrebol.taxiservicios.controller.system.PermissionController;
import com.arrebol.taxiservicios.model.core.Permission;
import com.arrebol.taxiservicios.model.core.User;
import com.arrebol.taxiservicios.wrapper.UserWrapper;
import java.io.Serializable;
import java.util.List;
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;

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

    /**
     * 
     */
    public void onUserChange() {
        logger.info("onUserChange");
        
        try {
            if("N/A".equals(getUserId())){
                user = new UserWrapper(
                    getBundlePropertyFile().getString("user.wrapper.role"),
                    getBundlePropertyFile().getString("user.wrapper.fullname"),
                    getBundlePropertyFile().getString("user.wrapper.avatar"));
            }else{
                setUser(new UserWrapper(getController().loadUserById(getUserId())));
            }
        } catch (Exception e) {
            logger.error("onItemSelect: " + e);
            showMessage(FacesMessage.SEVERITY_FATAL,
                    getBundlePropertyFile().getString("permission.title"),
                    getBundlePropertyFile().getString("permission.user.search.exception"));
        }
    }

    @PostConstruct
    public void init() {
        logger.info("init");
        try {
            loadBundlePropertyFile();
            setController(new PermissionController());
            setUsers(getController().getAllActiveUserAndNotRoot());
        } catch (Exception e) {
            logger.error("PermissionBean", e);
        }
    }

    private static final long serialVersionUID = -7642619296062533246L;

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

    private PermissionController controller;
    private List<Permission> permissions;
    private UserWrapper user;
    private String userName;
    private List<User> users;
    private String userId;

    public PermissionController getController() {
        return controller;
    }

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

    public List<Permission> getPermissions() {
        return permissions;
    }

    public void setPermissions(List<Permission> permissions) {
        this.permissions = permissions;
    }

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

    public void setUser(UserWrapper user) {
        this.user = user;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public List<User> getUsers() {
        return users;
    }

    public void setUsers(List<User> users) {
        this.users = users;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

}
