package eu.dnetlib.data.claims.handler;

import eu.dnetlib.data.claims.entity.Notification;
import eu.dnetlib.data.claims.sql.SQLStoreException;
import eu.dnetlib.data.claims.sql.SqlDAO;
import eu.dnetlib.data.claims.utils.QueryGenerator;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class FetchNotificationHandler {
    private Logger log = LogManager.getLogger(this.getClass());

    SqlDAO sqlDAO = null;
    QueryGenerator queryGenerator = null;

    public FetchNotificationHandler() {}

    public Notification fetchNotification(String openaireId, String userMail) throws Exception, SQLStoreException {
        ArrayList<Object> params = new ArrayList<>();
        String query = queryGenerator.generateSelectNotificationQuery(openaireId, userMail, params);
        ResultSet rs = sqlDAO.executePreparedQuery(query, params);

        Notification notification = fetchNotificationByResultSet(rs);
        if(notification == null) {
            log.info("No notification for openaireId: "+ openaireId+" and userMail: "+userMail+"\n");
        }

        return notification;
    }

    public List<Notification> fetchTrueNotifications() throws Exception, SQLStoreException {
        String query = queryGenerator.generateSelectTrueNotificationsQuery();
        ResultSet rs = sqlDAO.executePreparedQuery(query);

        List<Notification> notifications = fetchNotificationsByResultSet(rs);

        return notifications;
    }

    public Notification fetchNotificationByResultSet(ResultSet rs) throws Exception {
        Notification notification = null;
        if(rs.next()) {
            notification = new Notification();
            notification.setOpenaireId(rs.getString("openaire_id"));
            notification.setUserMail(rs.getString("user_email"));
            notification.setDate(rs.getTimestamp("last_interaction_date"));
            notification.setFrequency(rs.getInt("frequency"));
            notification.setNotify(rs.getBoolean("notify"));
        }
        return notification;
    }

    public List<Notification> fetchNotificationsByResultSet(ResultSet rs) throws Exception {
        List<Notification> notifications = null;
        while(rs.next()) {
            if(notifications == null) {
                notifications = new ArrayList<>();
            }
            Notification notification;
            notification = new Notification();
            notification.setOpenaireId(rs.getString("openaire_id"));
            notification.setUserMail(rs.getString("user_email"));
            notification.setDate(rs.getTimestamp("last_interaction_date"));
            notification.setFrequency(rs.getInt("frequency"));
            notification.setNotify(rs.getBoolean("notify"));

            notifications.add(notification);
        }
        return notifications;
    }

    public SqlDAO getSqlDAO() {
        return sqlDAO;
    }

    public void setSqlDAO(SqlDAO sqlDAO) {
        this.sqlDAO = sqlDAO;
    }

    public QueryGenerator getQueryGenerator() {
        return queryGenerator;
    }

    public void setQueryGenerator(QueryGenerator queryGenerator) {
        this.queryGenerator = queryGenerator;
    }
}
