package com.bb.service;

import java.util.Collections;
import java.util.HashMap;

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.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.ResourceAccessException;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.WebClientRequestException;

import io.netty.channel.ConnectTimeoutException;
import io.netty.handler.timeout.ReadTimeoutException;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class BetRadarServiceImpl implements BetRadarService {
	
	@Autowired
	WebClient webClient;
	
	@Override
	public String gameLaunch(String LOG_PREFIX, HashMap<String, String> apiInfo) {
		LOG_PREFIX = LOG_PREFIX + "BET_RADAR::GAME_LAUNCH:::";
		String luanchUrl = "";
		String apiBaseUrl = apiInfo.get("apiBaseUrl").toString();
		String apiKey = apiInfo.get("apiKey").toString();
		String apiUsername = apiInfo.get("apiUsername").toString();
		String apiNickname = apiInfo.get("apiNickname").toString();
		String apiDevice = apiInfo.get("apiDevice").toString();
		
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON);
		headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
		headers.set("Authorization", "Bearer "+apiKey);
		
		try {
			String launchUrl = apiBaseUrl + "/Game/GameLaunch";
			JSONObject launchBody = new JSONObject();
			launchBody.put("Owner", "SmCasino");
			launchBody.put("Provider", "SMSPORTS");
			launchBody.put("Account", apiUsername);
			launchBody.put("Name", apiNickname);
			launchBody.put("Device", apiDevice);
			launchBody.put("isLobby", true);
			launchBody.put("Gameid", "1");
			log.info(LOG_PREFIX+ "Url: " + launchUrl);
			log.info(LOG_PREFIX+ "Headers: " + headers.toString());
			log.info(LOG_PREFIX+ "Params: " + launchBody.toString());
			
			ResponseEntity<String> launchRes = webClient.post()
					.uri(launchUrl)
					.headers(h -> h.addAll(headers))
					.bodyValue(launchBody.toString())
					.retrieve()
					.toEntity(String.class)
					.block();

			JSONObject launchJson = null;
			
			if(launchRes != null && launchRes.getStatusCode() == HttpStatus.OK) {
				log.info(LOG_PREFIX+ "response : " + launchRes.getBody());
				launchJson = new JSONObject(launchRes.getBody());
				luanchUrl = launchJson.getString("url");
			}
			
		} catch (WebClientRequestException e) {
			Throwable cause = e.getCause();
			if (cause instanceof ConnectTimeoutException) {
				log.error(LOG_PREFIX+ "ConnectException e: {}", e.toString());
			} else if (cause instanceof ReadTimeoutException) {
				log.error(LOG_PREFIX+ "SocketTimeoutException e: {}", e.toString());
			} else {
				log.error(LOG_PREFIX+ "ResourceAccessException e: {}", e.toString());
			}
		} catch (Exception e) {
			log.error(LOG_PREFIX+ "Exception e: {}", e.toString());
		}
		
		return luanchUrl;
	}

	@Override
	public String getSportsSetting(String LOG_PREFIX, HashMap<String, String> apiInfo) {
		LOG_PREFIX = LOG_PREFIX + "BET_RADAR::GET_SPORTS_SETTING:::";
		String url = "";
		String apiBaseUrl = apiInfo.get("apiBaseUrl").toString();
		String apiKey = apiInfo.get("apiKey").toString();
		
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON);
		headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
		headers.set("Authorization", "Bearer "+apiKey);
		
		try {
			String launchUrl = apiBaseUrl + "/Agent/GetSportsSetting";
			log.info(LOG_PREFIX+ "Url: " + launchUrl);
			log.info(LOG_PREFIX+ "Headers: " + headers.toString());
			
			HttpEntity reqEntity = new HttpEntity(headers);
			ResponseEntity<String> launchRes = null;
			JSONObject launchJson = null;
			launchRes = webClient.get()
					.uri(launchUrl)
					.headers(h -> h.addAll(headers))
					.retrieve()
					.toEntity(String.class)
					.block();
			if(launchRes != null && launchRes.getStatusCode() == HttpStatus.OK) {
				log.info(LOG_PREFIX+ "response : " + launchRes.getBody());
				launchJson = new JSONObject(launchRes.getBody());
				url = launchJson.getString("url");
			}
			
		} catch (WebClientRequestException e) {
			Throwable cause = e.getCause();
			if (cause instanceof ConnectTimeoutException) {
				log.error(LOG_PREFIX+ "ConnectException e: {}", e.toString());
			} else if (cause instanceof ReadTimeoutException) {
				log.error(LOG_PREFIX+ "SocketTimeoutException e: {}", e.toString());
			} else {
				log.error(LOG_PREFIX+ "ResourceAccessException e: {}", e.toString());
			}
		} catch (Exception e) {
			log.error(LOG_PREFIX+ "Exception e: {}", e.toString());
		}
		
		return url;
	}
}
