package me.lenajenichen.voicebot.listener;

import me.lenajenichen.voicebot.LoggerInit;
import me.lenajenichen.voicebot.mysql.MySQL;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.events.user.update.UserUpdateAvatarEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;

import java.sql.ResultSet;
import java.sql.SQLException;

public class AvatarUpdate extends ListenerAdapter {

    @Override
    public void onUserUpdateAvatar(UserUpdateAvatarEvent e) {
        User user = e.getUser();
        ResultSet getUser = MySQL.queryMySQL("SELECT user_id FROM createvoice_users WHERE user_id = '" + user.getId() + "'");
        try {
            if(getUser.next()) {
                MySQL.updateQuery("UPDATE createvoice_users SET avatarURL = '" + user.getAvatarUrl() + "'");
            } else {
                MySQL.updateQuery("INSERT INTO createvoice_users(user_id, user_name, user_discriminator, avatarURL) VALUES('" + user.getId() + "', '" + user.getName() + "', '" + user.getDiscriminator() + "', '" + user.getAvatarUrl() + "')");
            }
        } catch (SQLException ex) {
            LoggerInit.logger.warning(ex.toString());
        }
    }

}
