package com.arrebol.taxiservicios.beans.stocktaking.service.order;

import com.arrebol.taxiservicios.controller.stocktaking.ServiceOrderSingletonController;
import com.arrebol.taxiservicios.model.core.User;
import com.arrebol.taxiservicios.model.enums.DeliveryStatusType;
import com.arrebol.taxiservicios.model.enums.StopNotificationType;
import com.arrebol.taxiservicios.model.stocktaking.ServiceOrderNotification;
import com.arrebol.taxiservicios.model.stocktaking.ServiceOrderNotificationDeliveryTo;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 *
 * @author Carlos Janitzio Zavala Lopez
 */
public final class ServiceOrderNotificationThread extends Thread {

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

    private ServiceOrderSingletonController singletonController;
    private ServiceOrderNotification serviceOrderNotification;
    private String createdBy;
    private List<String> permissionLst;
    private List<String> usrLst;

    @Override
    public void run() {
        notificationLogic();
    }

    public ServiceOrderNotificationThread() {
    }

    /**
     *
     * @param serviceOrderNotification
     * @param permissionLst
     * @param createdBy
     */
    public ServiceOrderNotificationThread(ServiceOrderNotification serviceOrderNotification,
            List<String> permissionLst, String createdBy) {
        setSingletonController(ServiceOrderSingletonController.getInstance());

        this.serviceOrderNotification = serviceOrderNotification;
        this.permissionLst = permissionLst;
        this.createdBy = createdBy;
    }

    private synchronized void notificationLogic() {
        try {
            logger.debug("Saving Notification");
            getSingletonController().saveServiceOrderNotification(getServiceOrderNotification());

            logger.debug("Getting users that will be notifed");
            setUsrLst(getSingletonController().
                    findAllUsersByPermissionHowWillReciveNotification(
                            getCreatedBy(), getPermissionLst(),
                            StopNotificationType.SERVICE_ORDER)
            );

            logger.debug("Prepared notifications recipients");
            List<ServiceOrderNotificationDeliveryTo> deliveryToLst = new ArrayList<>();
            getUsrLst().forEach((usrId) -> {
                ServiceOrderNotificationDeliveryTo delivery
                        = new ServiceOrderNotificationDeliveryTo(UUID.randomUUID().toString(),
                                new ServiceOrderNotification(getServiceOrderNotification().getId()), new User(usrId, ""), DeliveryStatusType.ENABLED);
                deliveryToLst.add(delivery);
            });
            logger.debug("Sending notifications");
            getSingletonController().sendNotifications(deliveryToLst);
            logger.debug("Notifications Sent");
        } catch (Exception e) {
            logger.error("notificationLogic", e);
        }
    }

    public String getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(String createdBy) {
        this.createdBy = createdBy;
    }

    public ServiceOrderSingletonController getSingletonController() {
        return singletonController;
    }

    public void setSingletonController(ServiceOrderSingletonController singletonController) {
        this.singletonController = singletonController;
    }

    public ServiceOrderNotification getServiceOrderNotification() {
        return serviceOrderNotification;
    }

    public void setServiceOrderNotification(ServiceOrderNotification serviceOrderNotification) {
        this.serviceOrderNotification = serviceOrderNotification;
    }

    public List<String> getPermissionLst() {
        return permissionLst;
    }

    public void setPermissionLst(List<String> permissionLst) {
        this.permissionLst = permissionLst;
    }

    public List<String> getUsrLst() {
        return usrLst;
    }

    public void setUsrLst(List<String> usrLst) {
        this.usrLst = usrLst;
    }

}
