/**
 * Sample Java code for youtube.videos.list
 * See instructions for running these code samples locally:
 * https://developers.google.com/explorer-help/guides/code_samples#java
 */
package com.google.musicanalysis.site;
import com.google.musicanalysis.util.Secrets;
import com.google.musicanalysis.util.URLEncodedBuilder;

import com.google.gson.*;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpRequest.BodyPublishers;
import java.net.http.HttpResponse.BodyHandlers;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.annotation.WebServlet;


@WebServlet("/api/youtube")
public class YoutubeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse res) 
        throws ServletException, IOException {
        // retrieve Youtube API key and access token 
        String API_KEY = Secrets.getSecretString("YOUTUBE_API_KEY");
        HttpSession session = req.getSession();
        var accessToken = session.getAttribute("oauth-access-token-youtube");
        if (session == null || accessToken == null) {
            res.setStatus(401);
            return;
        }
    
        // make http request to youtube API
        var youtubeParam = new URLEncodedBuilder()
            .add("part", "topicDetails")
            .add("myRating", "like")
            .add("key", API_KEY);
        URI youtubeUri = URI.create("https://www.googleapis.com/youtube/v3/videos?" + youtubeParam.build());

        var httpClient = HttpClient.newHttpClient();
        var youtubeReq = HttpRequest.newBuilder(youtubeUri)
                .header("Authorization", "Bearer " + accessToken.toString())
                .header("Accept", "application/json")
                .GET()
                .build();

        // get response to Youtube API 
        var youtubeRes = httpClient.sendAsync(youtubeReq, BodyHandlers.ofString()).join();
        var youtubeResBody = youtubeRes.body();
        System.out.println("DATA TYPE");
        System.out.println(youtubeResBody.getClass().getName());

        // parse JSON response for topic Categories
        JsonObject jObject = JsonParser.parseString(youtubeResBody).getAsJsonObject();
        JsonArray videos = jObject.getAsJsonArray("items");
        for (int i = 0; i < videos.size(); i++){
            JsonObject video = videos.get(i).getAsJsonObject();
            JsonObject topicDetails = video.getAsJsonObject("topicDetails");
            JsonArray topicCategories = topicDetails.getAsJsonArray("topicCategories");
            for (int j = 0; j < topicCategories.size(); j++) {
                // parse wikipedia links (json primitive) => music genres
                String link = topicCategories.get(j).toString();
                String topic = link.substring(link.lastIndexOf('/') + 1);
                topic = topic.replaceAll("\"", "");
                topic = topic.replaceAll("_", " ");
                System.out.println(topic);
            }
        }


        res.getWriter().write(youtubeResBody);
    }
}

@@ -812,16 +812,41 @@
 ervlet;%0A
+import java.util.HashMap;
 %0A%0A@WebSe
@@ -2316,24 +2316,103 @@
 etName());%0A%0A
+%0A        HashMap%3CString, Integer%3E genreCount = new HashMap%3CString, Integer%3E();%0A
         // p
@@ -3252,24 +3252,25 @@
 (%22_%22, %22 %22);%0A
+%0A
             
@@ -3277,59 +3277,248 @@
     
-System.
+// update genreCount hashmap%0A                int count = genreC
 ou
+n
 t.
-println(topic);%0A            %7D%0A        %7D%0A
+containsKey(topic) ? genreCount.get(topic) : 0;%0A                genreCount.put(topic, count + 1);%0A            %7D%0A        %7D%0A        System.out.println(genreCount.toString());
 %0A%0A  

update topic in hash map