package kr.co.swh.lecture.opensource.project.discode;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.security.auth.login.LoginException;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import com.google.gson.Gson;

import net.dv8tion.jda.api.AccountType;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.OnlineStatus;

public class SendJDA {

	public static JDA jda;
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		List<SentimentalDictionary> dicList = initDatabase();
		JDABuilder jb = new JDABuilder(AccountType.BOT);
		jb.setAutoReconnect(true);
		jb.setStatus(OnlineStatus.DO_NOT_DISTURB);
		jb.setToken("NjYzMjgzODYxMTUyNTk1OTkz.XhGR3g.GubjRZ-9njci3CUCoasIFj02mmU");
		jb.addEventListeners(new TListener(dicList));
		
		try {
			jda = jb.build();
		} catch (LoginException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static List<SentimentalDictionary> initDatas() {
		Gson gson = new Gson();
		List<SentimentalDictionary> dicList = new ArrayList<SentimentalDictionary>();
		CloseableHttpClient client = HttpClients.createDefault();
		HttpResponse response = null;
		String responseString = "";
		HttpGet requestGet = new HttpGet("https://raw.githubusercontent.com/park1200656/KnuSentiLex/master/data/SentiWord_info.json");
		try {
			response = client.execute(requestGet);
			responseString = EntityUtils.toString(response.getEntity());
			SentimentalDictionary[] value = gson.fromJson(responseString, SentimentalDictionary[].class);
			dicList.addAll(Arrays.asList(value));
		} catch (IOException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		return dicList;
	}
	
	public static List<SentimentalDictionary> initDatabase(){
		List<SentimentalDictionary> dicList = new ArrayList<SentimentalDictionary>();
		Connection connection = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			connection = DriverManager.getConnection("jdbc:mysql://dev-swh.ga:3306/market","root","swhacademy!");
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Statement statement = null;
		try {
			statement = connection.createStatement();
			ResultSet rs = statement.executeQuery("SELECT * FROM SENTIMENTAL_DIC;");
			while(rs.next()){
				SentimentalDictionary dic = new SentimentalDictionary();
				dic.setWord_root(rs.getString("word_root"));
				dic.setPolarity(String.valueOf(rs.getInt("polarity")));
				dicList.add(dic);
			}
			rs.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return dicList;
	}
}
