package com.bb.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import org.springframework.web.reactive.function.client.WebClient;


import com.bb.dao.SiteDao;
import com.bb.model.Game;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class GrandServiceImpl implements GrandService {

	@Autowired
	private SiteDao siteDao;
	
	@Autowired
	WebClient webClient;
	
	@Override
	public String getLaunchUrl(HashMap venderInfo) {
		
		
		JSONObject params = new JSONObject();
		try {
		params.put("vendor_key", venderInfo.get("vendor_key").toString());
		params.put("game_key", venderInfo.get("game_key").toString());
		params.put("username", venderInfo.get("username").toString());
		params.put("nickname", venderInfo.get("username").toString());
		
		HttpHeaders header = new HttpHeaders(); 	
		header.set("Content-Type", "application/json");
		header.set("Accept", "application/json");
		header.set("Authorization", "Bearer " + venderInfo.get("vendorApiKey").toString());
		log.info(venderInfo.get("vendorApiUrl").toString()+"/game-url");
		log.info(params.toString());

		ResponseEntity<String> res = webClient.post()
				.uri(venderInfo.get("vendorApiUrl").toString()+"/game-url")
				.headers(h -> h.addAll(header))
				.bodyValue(params.toString())
				.retrieve()
				.toEntity(String.class)
				.block();
		
		log.info(res.getBody());
		JSONObject resJson = new JSONObject(res.getBody());
		JSONObject resData = resJson.getJSONObject("data");
		return resData.getString("url");
		}catch(Exception e) {
			log.info("[ERROR] Grand pragmatic getLaunchUrl : "+ venderInfo.get("username").toString()+" - " + e.toString());
			return null;
		}
		
	}

	@Override
	public List<Game> getGameList(HashMap venderInfo) {
		List<Game> gameList = null;
		try {
		
		
		HttpHeaders header = new HttpHeaders(); 	
		header.set("Content-Type", "application/json");
		header.set("Accept", "application/json");
		header.set("Authorization", "Bearer " + venderInfo.get("vendorApiKey").toString());

		ResponseEntity<String> res = webClient.get()
				.uri(venderInfo.get("vendorApiUrl").toString()+"/games?vendor_key="+venderInfo.get("vendor_key").toString())
				.headers(h -> h.addAll(header))
				.retrieve()
				.toEntity(String.class)
				.block();
		
		JSONObject resJson = new JSONObject(res.getBody());
		
		//log.info(resJson.toString());
		JSONArray games = resJson.getJSONArray("data");		
		int length = games.length();
		gameList = new ArrayList<>();
		
		for(int i=0; i<length; i++) {
			Game game = new Game();
			JSONObject objGame = games.getJSONObject(i);
			
			//log.info(objGame.toString());
			game.setGameIdx(i+1);
			game.setGameKey(objGame.getString("game"));
			
			JSONArray gameNames = objGame.getJSONArray("names");
			for(int n=0; n<gameNames.length(); n++) {
				JSONObject gameName = gameNames.getJSONObject(n);
				if("ko".equals(gameName.getString("language"))){
					game.setGameName(gameName.getString("name"));
				}
				if("en".equals(gameName.getString("language"))){
					game.setGameNameEn(gameName.getString("name"));
				}
			}			
			
			game.setGameImg(objGame.getString("image"));
			gameList.add(game);
		}
		
		}catch(Exception e) {
			log.info("[ERROR] Grand pragmatic getLaunchUrl : - " + e.toString());
			return null;
		}
		return gameList;
	}

	@Override
	public JSONObject getDetail(String LOG_PREFIX, HashMap<String, String> apiInfo) {
		// TODO Auto-generated method stub
		return null;
	}
	
	
	

}
