package com.bb.service;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bb.dao.SplusDao;

import lombok.extern.slf4j.Slf4j;
@Slf4j
@Service
public class SplusServiceImpl implements SplusService {
	
	@Autowired
	SplusDao splusDao;

	@Override
	public void insertSplus(HashMap tranParam, JSONObject paramjson) {
		
		Map splusBetParam = new HashMap();
		
		splusBetParam.put("refId", tranParam.get("refId").toString());
		splusBetParam.put("siteIdx", tranParam.get("siteIdx"));
		splusBetParam.put("memberIdx", tranParam.get("memberIdx"));
		splusBetParam.put("memberId", tranParam.get("memberId").toString());
		splusBetParam.put("betStatus", 0);
		
		
		
		try {
			//한글 치환
			org.json.JSONObject bets = paramjson.getJSONObject("Bets");
			org.json.JSONObject bet = bets.getJSONObject("Bet");
			splusBetParam.put("realAmount", bet.get("RealAmount").toString());
			splusBetParam.put("creationDate", bet.get("CreationDate").toString());
			splusBetParam.put("betOdds", bet.getDouble("OddsDec"));
			splusBetParam.put("gain", bet.get("Gain").toString());
			
			//싱글 베팅
			if(bet.has("Lines")) {
				
				splusBetParam.put("betType", "M");
				
				
				org.json.JSONArray lines = bet.getJSONArray("Lines");
				for(int i=0;i<lines.length();i++) {		
					org.json.JSONObject line = lines.getJSONObject(i);
					
					splusBetParam.put("branchName", line.getString("BranchName"));
					splusBetParam.put("leagueName", line.getString("LeagueName"));
					splusBetParam.put("leagueNameKor", line.getString("LeagueNameKor"));
					
					splusBetParam.put("leagueNameKor", line.getString("LeagueNameKor"));
					splusBetParam.put("leagueNameKor", line.getString("LeagueNameKor"));
					splusBetParam.put("homeTeam", line.getString("HomeTeam"));
					splusBetParam.put("homeTeamKor", line.getString("HomeTeamKor"));
					splusBetParam.put("awayTeam", line.getString("AwayTeam"));
					splusBetParam.put("awayTeamKor", line.getString("AwayTeam"));
					
					
					splusBetParam.put("lineTypeId", line.getInt("LineTypeID"));
					splusBetParam.put("yourBet", line.getString("YourBet"));
					splusBetParam.put("oddsDec", line.getDouble("OddsDec"));
					
					if(1!= line.getInt("LineTypeID")) {
						splusBetParam.put("points", line.getDouble("Points"));
					}else {
						splusBetParam.put("points", null);
					}
					splusBetParam.put("score", line.getString("Score"));	
					splusBetParam.put("status", line.getString("Status"));
					splusBetParam.put("eventDate", line.getString("EventDate"));
					splusBetParam.put("newEventId", line.getLong("NewEventID"));
					splusBetParam.put("newMasterEventId", line.getLong("NewMasterEventID"));
					
					splusDao.insertSplus(splusBetParam);
				
				}
				
			}else {
				
				splusBetParam.put("betType", "S");
				splusBetParam.put("branchName", bet.getString("BranchName"));
				splusBetParam.put("leagueName", bet.getString("LeagueName"));
				splusBetParam.put("leagueNameKor", bet.getString("LeagueNameKor"));
				splusBetParam.put("homeTeam", bet.getString("HomeTeam"));
				splusBetParam.put("homeTeamKor", bet.getString("HomeTeamKor"));
				splusBetParam.put("awayTeam", bet.getString("AwayTeam"));
				splusBetParam.put("awayTeamKor", bet.getString("AwayTeam"));
				
				splusBetParam.put("lineTypeId", bet.getInt("LineTypeID"));
				splusBetParam.put("yourBet", bet.getString("YourBet"));
				splusBetParam.put("oddsDec", bet.getDouble("OddsDec"));
				splusBetParam.put("betOdds", bet.getDouble("OddsDec"));
			
			
				if(1!= bet.getInt("LineTypeID")) {
					splusBetParam.put("points", bet.getDouble("Points"));
				}else {
					splusBetParam.put("points", null);
				}
				
				splusBetParam.put("score", bet.getString("Score"));	
				splusBetParam.put("status", bet.getString("Status"));
				splusBetParam.put("eventDate", bet.getString("EventDate"));
				splusBetParam.put("newEventId", bet.getLong("NewEventID"));
				splusBetParam.put("newMasterEventId", bet.getLong("NewMasterEventID"));
				
				splusDao.insertSplus(splusBetParam);
								
			}
			}catch(Exception e) {log.error("tranERR {}", e.toString());}
		
	}

	@Override
	public void updateSplus(HashMap tranParam, JSONObject paramjson) {
		
		Map splusBetParam = new HashMap();
		try {
		splusBetParam.put("refId", tranParam.get("refId").toString());
	
		org.json.JSONObject credits = paramjson.getJSONObject("Credit");
		splusBetParam.put("gain",credits.getLong("Amount"));
		
		if(credits.getDouble("Amount")> 0) {
			splusBetParam.put("betStatus", 1);
		}else {
			splusBetParam.put("betStatus", -1);
		}
		org.json.JSONObject purchases = credits.getJSONObject("Purchases");
		org.json.JSONObject purchase = purchases.getJSONObject("Purchase");
		org.json.JSONObject selections = purchase.getJSONObject("Selections");
		//싱글 베팅
		
		org.json.JSONObject selection = selections.getJSONObject("Selection");
		if(selection ==null) { // 경기 결과	여러개		
			org.json.JSONArray selectionArray = selections.getJSONArray("Selection");
			for(int i=0;i<selectionArray.length();i++) {		
				org.json.JSONObject line = selectionArray.getJSONObject(i);
				
				Long newMasterEventId = line.getLong("NewMasterEventID");
				Long newEventId = line.getLong("NewEventID");
				String score = line.getString("Score");
				int lineTypeId = line.getInt("LineTypeID");
				
				org.json.JSONObject changes = line.getJSONObject("Changes");
				org.json.JSONObject change = changes.getJSONObject("Change");
				String status = change.getString("NewStatus");
				
				org.json.JSONObject bets = change.getJSONObject("Bets");
				org.json.JSONObject bet = bets.getJSONObject("Bet");
				
				Double resultOdds = bet.getDouble("Odds");
				
				
				Map eventParam = new HashMap();
				eventParam.put("newMasterEventId", newMasterEventId);
				eventParam.put("newEventId", newEventId);
				eventParam.put("score", score);
				eventParam.put("lineTypeId", lineTypeId);
				eventParam.put("status", status);
				eventParam.put("resultOdds", resultOdds);
				
				splusDao.udpateEvent(eventParam);
				
			
			}
			
		}else {  // 경기 결과 1개
			
			Long newMasterEventId = selection.getLong("NewMasterEventID");
			Long newEventId = selection.getLong("NewEventID");
			String score = selection.getString("Score");
			int lineTypeId = selection.getInt("LineTypeID");
			
			org.json.JSONObject changes = selection.getJSONObject("Changes");
			org.json.JSONObject change = changes.getJSONObject("Change");
			String status = change.getString("NewStatus");
			
			org.json.JSONObject bets = change.getJSONObject("Bets");
			org.json.JSONObject bet = bets.getJSONObject("Bet");
			
			Double resultOdds = bet.getDouble("Odds");
			
			
			Map eventParam = new HashMap();
			eventParam.put("newMasterEventId", newMasterEventId);
			eventParam.put("newEventId", newEventId);
			eventParam.put("score", score);
			eventParam.put("lineTypeId", lineTypeId);
			eventParam.put("status", status);
			eventParam.put("resultOdds", resultOdds);
			
			splusDao.udpateEvent(eventParam);
					
			
		}
		
		splusDao.udpateBet(splusBetParam);
		}catch(Exception e) {log.error("spls update {}", e.toString());}
	}

	@Override
	public void cancelSplus(HashMap tranParam) {
		try {
			Map splusBetParam = new HashMap();
			
			splusBetParam.put("refId", tranParam.get("refId").toString());
			
			splusDao.cancelBet(splusBetParam);
		}catch(Exception e) {log.error("spls update {}", e.toString());}
	}

	@Override
	public int getReserveCnt(Map trxParam) {
		// TODO Auto-generated method stub
		return splusDao.getReserveCnt(trxParam);
	}

	@Override
	public int getCommitCnt(Map trxParam) {
		// TODO Auto-generated method stub
		return splusDao.getCommitCnt(trxParam);
	}

	@Override
	public int getDeReverseCnt(Map trxParam) {
		// TODO Auto-generated method stub
		return splusDao.getDeReverseCnt(trxParam);
	}

	@Override
	public int getCustomerCnt(Map trxParam) {
		// TODO Auto-generated method stub
		return splusDao.getCustomerCnt(trxParam);
	}

}
