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.JDA;
import net.dv8tion.jda.api.JDABuilder;


public class SendJDA {
	public static void main(String[] args) throws LoginException, InterruptedException {
		// TODO Auto-generated method stub
		List<SentimentalDictionary> dicList = initDatabase();
		// Note: It is important to register your ReadyListener before building
        JDA jda = JDABuilder.createDefault("NjYzMjgzODYxMTUyNTk1OTkz.XhGROw.Ps3JJIKlEXoE1eah_5OKHwViThY")
            .addEventListeners(new TListener(dicList))
            .build();

        // optionally block until JDA is ready
        jda.awaitReady();
//        
//        이전버전
//		List<SentimentalDictionary> dicList = initDatabase();
//		JDABuilder jb = new JDABuilder(AccountType.BOT);
//		jb.setAutoReconnect(true);
//		jb.setStatus(OnlineStatus.DO_NOT_DISTURB);
//		jb.setToken("NjYzMjgzODYxMTUyNTk1OTkz.XhGROw.Ps3JJIKlEXoE1eah_5OKHwViThY");
//		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.cj.jdbc.Driver");
			connection = DriverManager.getConnection("jdbc:mysql://183.99.87.90:3306/market?serverTimezone=UTC","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;
	}
}
