package com.infinite.focus.server.googleapis.services.youtube;

import java.io.IOException;

import org.springframework.stereotype.Service;

import com.infinite.focus.server.googleapis.GoogleApisConfiguration;

import retrofit2.Call;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

@Service
public class YouTubeService implements GoogleApisConfiguration {
	
	//YOUTUBE DATA API v3
    static final String PART_CONTENT_DETAILS = "contentDetails";
    static final String YOUTUBE_DATA_API_V3_API_KEY = "AIzaSyBQ8dFHzjnOSOTUEJqPWuvvHbgQG4D79Dg";

	private YoutubeRepositoryInterface service;

    public YouTubeService() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(GOOGLE_APIS_BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        service = retrofit.create(YoutubeRepositoryInterface.class);
    }

    public YouTubeVideoDetails getYouTubeVideoDetailsByVideoId(String video_id) throws IOException {
    	Call<YouTubeVideoDetails> retrofitCall = service.getYouTubeVideoDetailsByVideoId(PART_CONTENT_DETAILS, video_id, YOUTUBE_DATA_API_V3_API_KEY);

        Response<YouTubeVideoDetails> response = retrofitCall.execute();

        if (!response.isSuccessful()) {
            throw new IOException(response.errorBody() != null
                    ? response.errorBody().string() : "Unknown error");
        }

        return response.body();
    }
 
}
