package com.bb.service;

import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import org.apache.http.conn.ConnectTimeoutException;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.ResourceAccessException;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.WebClientRequestException;
import org.springframework.web.reactive.function.client.WebClientResponseException;

import com.bb.model.Game;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class TowerServiceImpl implements TowerService {
	
	@Autowired
	WebClient webClient;
	
	
	@Override
	public String getLaunchUrl(HashMap venderInfo) {
		String launchUrl="";
		try {
			String responseBody = webClient.get()
					.uri(venderInfo.get("gameApiUrl").toString())
					.header("Authorization", venderInfo.get("vendorApiKey").toString())
					.retrieve()
					.bodyToMono(String.class)
					.block();

			log.info(responseBody);
			JSONObject resJson = new JSONObject(responseBody);
			launchUrl = resJson.getString("url");
		} catch (WebClientRequestException wcre) {
			Throwable cause = wcre.getCause();
			if(cause instanceof org.apache.http.conn.ConnectTimeoutException) {
				log.error("TOWER_API::getLaunchUrl::[ConnectTimeoutException]"+wcre.getMessage());
			} else if(cause instanceof java.net.SocketTimeoutException) {
				log.error("TOWER_API::getLaunchUrl::[SocketTimeoutException]"+wcre.getMessage());
			} else if(cause instanceof InterruptedException) {
				log.error("TOWER_API::getLaunchUrl::[InterruptedException]"+wcre.getMessage());
			} else {
				log.error("TOWER_API::getLaunchUrl::WebClientRequestException::"+wcre.toString());
			}
		} catch (WebClientResponseException wre) {
			String str = wre.getResponseBodyAsString();
			log.error("TOWER_API::getLaunchUrl::WebClientResponseException::"+str);
		} catch (JSONException je) {
			je.printStackTrace();
		} catch (Exception e) {
			log.error("TOWER_API::getLaunchUrl::Exception::"+e.toString());
			e.printStackTrace();
		}
		return launchUrl;
	}


	@Override
	public List<Game> getGameList(HashMap venderInfo) {
		// https://to-api.com/v2/games?site_code=tripleopr01&vendorCode=pp
		List<Game> gameList = null;
		try {
		String url = venderInfo.get("vendorApiUrl").toString()+"/v2/games?site_code="+venderInfo.get("vendorApiId").toString()+"&vendorCode="+venderInfo.get("vendorGameCode").toString();

		String responseBody = webClient.get()
				.uri(url)
				.header("Authorization", venderInfo.get("vendorApiKey").toString())
				.retrieve()
				.bodyToMono(String.class)
				.block();

		JSONArray games = new JSONArray(responseBody);
		int length = games.length();
		log.info("TOWER_API::gameList::Game List length: " + length);

		if(length > 0) {
			gameList = new ArrayList<>();
			for(int i=0; i<length; i++) {
				Game game = new Game();
				JSONObject objGame = games.getJSONObject(i);
				game.setGameIdx(i);
				game.setGameKey(objGame.getString("gameCode"));
				game.setGameName(objGame.getString("name"));
				game.setGameNameEn(objGame.getString("name"));
				game.setGameImg(objGame.getString("img"));
				gameList.add(game);
			}
		} else {
			log.error("TOWER_API::gameList::Game List Empty");
		}

		} catch (WebClientRequestException wcre) {
			Throwable cause = wcre.getCause();
			if(cause instanceof org.apache.http.conn.ConnectTimeoutException) {
				log.error("TOWER_API::gameList::[ConnectTimeoutException]"+wcre.getMessage());
			}

			if(cause instanceof java.net.SocketTimeoutException) {
				log.error("TOWER_API::gameList::[SocketTimeoutException]"+wcre.getMessage());
			}

			if(cause instanceof InterruptedException) {
				log.error("TOWER_API::gameList::[InterruptedException]"+wcre.getMessage());
			}
		} catch(WebClientResponseException wre) {
			String str = wre.getResponseBodyAsString();
			log.error("TOWER_API::gameList::WebClientResponseException::"+str);
		} catch(HttpClientErrorException hee) {
			String str = hee.getResponseBodyAsString();
			log.error("TOWER_API::gameList::HttpClientErrorException::"+str);
		} catch (Exception e) {
			log.error("TOWER_API::gameList::Exception::"+e.toString());
			e.printStackTrace();
		}
		
		return gameList;
	}
}
