/*
 * This file is part of Helium.
 *
 * Helium is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Helium is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Helium.  If not, see <http://www.gnu.org/licenses/>.
 */

package net.ryanmorrison.helium.configuration.beans;

import net.ryanmorrison.helium.discord.listener.ConnectionManagementListener;
import net.ryanmorrison.helium.discord.listener.ElasticsearchIndexListener;
import net.ryanmorrison.helium.discord.listener.ElasticsearchSyncListener;
import net.ryanmorrison.helium.discord.listener.RegistrationListener;
import net.ryanmorrison.helium.message.service.MessageService;
import net.ryanmorrison.helium.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.password.PasswordEncoder;
import sx.blah.discord.api.ClientBuilder;
import sx.blah.discord.api.EventDispatcher;
import sx.blah.discord.api.IDiscordClient;

import javax.annotation.PostConstruct;

/**
 * @author Ryan Morrison
 * @since 1.0
 */
@Configuration
public class DiscordBeanConfiguration {

    @Value("${helium.discord.token}")
    private String token;

    @Value("${helium.discord.trigger}")
    private String trigger;

    @Value("${helium.discord.websocket.reconnectWait}")
    private int reconnectWait;

    @Value("${helium.discord.websocket.maxRetries}")
    private int maxRetries;

    @Autowired
    private ApplicationContext applicationContext;

    @Autowired
    private UserService userService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private MessageService messageService;

    private IDiscordClient discordClient;

    @Bean
    public IDiscordClient discordClient() throws Exception {
        return this.discordClient = new ClientBuilder()
                .withToken(this.token)
                .build();
    }

    @PostConstruct
    public void postConstruct() throws Exception {
        this.discordClient.login();

        ConnectionManagementListener connectionManagementListener = new ConnectionManagementListener();
        connectionManagementListener.setApplicationContext(this.applicationContext);
        connectionManagementListener.setReconnectWait(this.reconnectWait);
        connectionManagementListener.setMaxReconnectTries(this.maxRetries);

        RegistrationListener registrationListener = new RegistrationListener();
        registrationListener.setTrigger(this.trigger);
        registrationListener.setUserService(this.userService);
        registrationListener.setPasswordEncoder(this.passwordEncoder);

        ElasticsearchIndexListener elasticsearchIndexListener = new ElasticsearchIndexListener();
        elasticsearchIndexListener.setMessageService(this.messageService);

        ElasticsearchSyncListener elasticsearchSyncListener = new ElasticsearchSyncListener();
        elasticsearchSyncListener.setMessageService(this.messageService);

        EventDispatcher dispatcher = this.discordClient.getDispatcher();
        dispatcher.registerListener(connectionManagementListener);
        dispatcher.registerListener(registrationListener);
        dispatcher.registerListener(elasticsearchIndexListener);
        dispatcher.registerListener(elasticsearchSyncListener);
    }
}
