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

/**
 * <pre>
 * kr.co.swh.lecture.opensource.project.youtube 
 * Youtube.java
 *
 * 설명 :
 * </pre>
 * 
 * @since : 2021. 6. 6.
 * @author : tobby48
 * @version : v1.0
 */
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

import com.google.gson.Gson;




public class Youtube {
	public String search(String search) throws IOException {
		
		String apiurl = "https://www.googleapis.com/youtube/v3/search";
		apiurl += "?key=AIzaSyAI9M3FgT9emj5omLmbvm8rRDVLQvMMgE8";
		apiurl += "&part=snippet&type=video&maxResults=20&videoEmbeddable=true";
		apiurl += "&q="+URLEncoder.encode(search,"UTF-8");
		
		URL url = new URL(apiurl);
		HttpURLConnection con = (HttpURLConnection) url.openConnection();
		con.setRequestMethod("GET");
		
		BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(),"UTF-8"));
		String inputLine;
		StringBuffer response = new StringBuffer();
		while((inputLine = br.readLine()) != null) {
			response.append(inputLine);
		}
		br.close();
		
		return response.toString();
	}
	
	public ArrayList<String> audio(String audio) {
		
		Gson gsonObj = new Gson();
		Map<String, String> inputMap = new HashMap<String, String>();
		inputMap.put("name", "makesomething");
		inputMap.put("blog", "https://web-inf.tistory.com");
		
		Youtube y = new Youtube();
		ArrayList<String> alist = new ArrayList<String>();
		try {
			String s = y.search(""+ audio +"");
			Map map = gsonObj.fromJson(s, Map.class);	


			//			List list = gsonObj.fromJson(map.get("items"), List.class);	
			List list = (List)map.get("items");

			//			Map map1 = gsonObj.fromJson(list.get(0).toString(), Map.class);
			for(int a = 0; a < list.size(); a++) {

				Map id = (Map)list.get(a);
				Map videoId = (Map)id.get("id");
				//System.out.println(videoId.get("videoId"));

				Map snippet = (Map)list.get(a);
				Map title = (Map)id.get("snippet");
				//System.out.println(title.get("title"));
				String popo;
				HashMap<String,String> disc = new HashMap<String,String>();
				//disc.put("number",String.valueOf(a+1));
				//disc.put("title",(String) title.get("title")); //값 추가
				disc.put("address","https://www.youtube.com/watch?v="+((String) videoId.get("videoId")));
				System.out.println(disc);
				popo = "https://www.youtube.com/watch?v="+((String) videoId.get("videoId"));
				
				alist.add(popo);	
					if(a > 10)   
					{
						break;
					}
					else if(alist.toString().length() > 2000) 
					{
						alist.remove(a);
						break;
					}
				}

			//			System.out.println(map.get("items"));

			//			System.out.println(s);


		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return alist;
	}
	public static void main(String[] args) {
		String name;
		Gson gsonObj = new Gson();
		Map<String, String> inputMap = new HashMap<String, String>();
		inputMap.put("name", "makesomething");
		inputMap.put("blog", "https://web-inf.tistory.com");
		Scanner sc = new Scanner(System.in);
		name = sc.next();
		Youtube y = new Youtube();
		
		try {
			String s = y.search(""+ name +"");
			Map map = gsonObj.fromJson(s, Map.class);	


			//			List list = gsonObj.fromJson(map.get("items"), List.class);	
			List list = (List)map.get("items");

			//			Map map1 = gsonObj.fromJson(list.get(0).toString(), Map.class);
			for(int a = 0; a<15; a++) {

				Map id = (Map)list.get(a);
				Map videoId = (Map)id.get("id");
				//System.out.println(videoId.get("videoId"));

				Map snippet = (Map)list.get(a);
				Map title = (Map)id.get("snippet");
				//System.out.println(title.get("title"));

				HashMap<String,String> disc = new HashMap<String,String>();
				disc.put("number",String.valueOf(a+1));
				disc.put("title",(String) title.get("title")); //값 추가
				disc.put("address","https://www.youtube.com/watch?v="+((String) videoId.get("videoId")));
				System.out.println(disc);


			}

			//			System.out.println(map.get("items"));

			//			System.out.println(s);


		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
}
