package com.arrebol.taxiservicios.beans.system;

import com.arrebol.taxiservicios.beans.TaxiGenericBean;
import com.arrebol.taxiservicios.controller.notification.NotificationController;
import com.arrebol.taxiservicios.model.core.User;
import com.arrebol.taxiservicios.model.enums.StopNotificationType;
import com.arrebol.taxiservicios.model.notification.StopNotification;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
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 UserNotificationBean extends TaxiGenericBean implements Serializable {

    public void updateStopNotification() {
        try {
            List<StopNotification> notifications = new ArrayList<>();

            getDualListModel().getTarget().forEach((usr) -> {
                notifications.add(new StopNotification(UUID.randomUUID().toString(), new User(usr.getId(), ""), StopNotificationType.SERVICE_ORDER));
            });

            if (getController().updateStopNotificationData(notifications)) {
                showMessage(FacesMessage.SEVERITY_INFO,
                        getBundlePropertyFile().getString("user.manager.notification.title"),
                        getBundlePropertyFile().getString("user.manager.notification.update.success.desc"));
            } else {
                showMessage(FacesMessage.SEVERITY_ERROR,
                        getBundlePropertyFile().getString("user.manager.notification.title"),
                        getBundlePropertyFile().getString("user.manager.notification.update.fail.desc"));
            }
        } catch (Exception e) {
            showMessage(FacesMessage.SEVERITY_ERROR,
                    getBundlePropertyFile().getString("user.manager.notification.title"),
                    getBundlePropertyFile().getString("user.manager.notification.update.fail.desc"));
        }
    }

    private static final long serialVersionUID = 5265606129322331928L;

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

    private NotificationController controller;
    private DualListModel<User> dualListModel;

    @PostConstruct
    public void init() {
        loadBundlePropertyFile();
        try {
            setController(new NotificationController());
            List<User> sourceUsers = getController().findNotificationUsers(false);
            List<User> targetUsers = getController().findNotificationUsers(true);
            setDualListModel(new DualListModel<>(sourceUsers, targetUsers));
        } catch (Exception e) {
            showMessage(FacesMessage.SEVERITY_ERROR,
                    getBundlePropertyFile().getString("user.manager.notification.title"),
                    getBundlePropertyFile().getString("user.manager.notification.fail.desc"));
        }
    }

    public NotificationController getController() {
        return controller;
    }

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

    public DualListModel<User> getDualListModel() {
        return dualListModel;
    }

    public void setDualListModel(DualListModel<User> dualListModel) {
        this.dualListModel = dualListModel;
    }

}
