// Path: src/main/java/com/papercut/dust/ConfigurationHolder.java
// public final class ConfigurationHolder {
// 
//     private static DeviceUsageTrackerConfig config;
// 
//     private ConfigurationHolder() {
//     }
// 
//     static void set(final DeviceUsageTrackerConfig config) {
//         ConfigurationHolder.config = config;
//     }
// 
//     public static DeviceUsageTrackerConfig get() {
//         return config;
//     }
// }


import allbegray.slack.SlackClientFactory;
import allbegray.slack.rtm.Event;
import allbegray.slack.rtm.EventListener;
import allbegray.slack.rtm.SlackRealTimeMessagingClient;
import allbegray.slack.webapi.SlackWebApiClient;
import com.papercut.dust.ConfigurationHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.enterprise.context.Dependent;
/*
 * Copyright (c) 2017 PaperCut Software International Pty. Ltd.
 *
 * https://www.papercut.com
 *
 * Use of this source code is governed by an MIT license.
 * See the project's LICENSE file for more information.
 */
package com.papercut.dust.slack;



/**
 * Slack client integration.
 */
@Configured
@Dependent
public class SlackIntegration implements SlackClient {

    private static final Logger LOGGER = LoggerFactory.getLogger(SlackIntegration.class);

    private SlackWebApiClient webClient;
    private SlackRealTimeMessagingClient rtmClient;

    @PostConstruct
    public void init() {
        webClient = webClient();
        rtmClient = rtmClient();
        LOGGER.info("Web and RTM clients created.");
        startListening();
    }

    @PreDestroy
    public void destroy() {
        rtmClient.close();
        LOGGER.info("Closed RTM client.");
    }

    private void startListening() {
        rtmClient.connect();
        LOGGER.info("PIN_ADDED/PIN_REMOVED listeners added.");
    }

    /**
     * Convenience method to create an authorized web API client.
     *
     * @return Slack web client
     */
    private SlackWebApiClient webClient() {
        return SlackClientFactory.createWebApiClient(
                ConfigurationHolder.get().getSlackConfig().getBotToken());
    }

    /**
     * Convenience method to create an authorized RTM client.
     *
     * @return Slack RTM client
     */
    private SlackRealTimeMessagingClient rtmClient() {
        return SlackClientFactory.createSlackRealTimeMessagingClient(
// Path: src/main/java/com/papercut/dust/ConfigurationHolder.java
// public final class ConfigurationHolder {
// 
//     private static DeviceUsageTrackerConfig config;
// 
//     private ConfigurationHolder() {
//     }
// 
//     static void set(final DeviceUsageTrackerConfig config) {
//         ConfigurationHolder.config = config;
//     }
// 
//     public static DeviceUsageTrackerConfig get() {
//         return config;
//     }
// }


// Path: src/main/java/com/papercut/dust/slack/SlackIntegration.java
import allbegray.slack.SlackClientFactory;
import allbegray.slack.rtm.Event;
import allbegray.slack.rtm.EventListener;
import allbegray.slack.rtm.SlackRealTimeMessagingClient;
import allbegray.slack.webapi.SlackWebApiClient;
import com.papercut.dust.ConfigurationHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.enterprise.context.Dependent;

/*
 * Copyright (c) 2017 PaperCut Software International Pty. Ltd.
 *
 * https://www.papercut.com
 *
 * Use of this source code is governed by an MIT license.
 * See the project's LICENSE file for more information.
 */
package com.papercut.dust.slack;



/**
 * Slack client integration.
 */
@Configured
@Dependent
public class SlackIntegration implements SlackClient {

    private static final Logger LOGGER = LoggerFactory.getLogger(SlackIntegration.class);

    private SlackWebApiClient webClient;
    private SlackRealTimeMessagingClient rtmClient;

    @PostConstruct
    public void init() {
        webClient = webClient();
        rtmClient = rtmClient();
        LOGGER.info("Web and RTM clients created.");
        startListening();
    }

    @PreDestroy
    public void destroy() {
        rtmClient.close();
        LOGGER.info("Closed RTM client.");
    }

    private void startListening() {
        rtmClient.connect();
        LOGGER.info("PIN_ADDED/PIN_REMOVED listeners added.");
    }

    /**
     * Convenience method to create an authorized web API client.
     *
     * @return Slack web client
     */
    private SlackWebApiClient webClient() {
        return SlackClientFactory.createWebApiClient(
                ConfigurationHolder.get().getSlackConfig().getBotToken());
    }

    /**
     * Convenience method to create an authorized RTM client.
     *
     * @return Slack RTM client
     */
    private SlackRealTimeMessagingClient rtmClient() {
        return SlackClientFactory.createSlackRealTimeMessagingClient(
                ConfigurationHolder.get().getSlackConfig().getBotToken());