package com.bb.front;

import java.util.HashMap;

import jakarta.servlet.http.HttpServletRequest;

import org.codehaus.jettison.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.bb.jwt.JwtManager;
import com.bb.model.BombAuthVO;
import com.bb.model.BombReponseVo;
import com.bb.service.SiteService;
import com.bb.util.AesUtil;
import com.google.gson.Gson;

import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@RestController
@Slf4j
@RequestMapping("/api/cb/demopbomb")
@RequiredArgsConstructor
@SecurityRequirement(name = "Authorization")
public class CallBackBombDemoController {

	// https://oprvender.com/api/cb/live
	
	@Autowired
	private final JwtManager jwtManager;

	@Autowired
	SiteService siteService;
	
	
	
	
	@ResponseBody
	@RequestMapping(value="/loginAuth", method=RequestMethod.POST)
    public BombReponseVo minegameLoginAuth(@RequestBody BombAuthVO bombAuthVO, HttpServletRequest request) throws Exception {
		BombReponseVo bombReponseVo = new BombReponseVo();
		System.out.println("bombAuthVO" + bombAuthVO.toString());
		Gson gson = new Gson();
		bombAuthVO = gson.fromJson(AesUtil.getAES128_Decode(bombAuthVO.getEp()), BombAuthVO.class);
		System.out.println("dec bombAuthVO" + bombAuthVO.toString());
 

        HashMap memParam = new HashMap();
        memParam.put("bombAccId", bombAuthVO.getLoginToken());
        HashMap demomember =  siteService.getDemoByBombAccId(memParam);

        try {
            String accID = bombAuthVO.getAccID();       
            String token=  bombAuthVO.getLoginToken();

         

            if(demomember.get("bombAccId").toString().equals(token)) {
            	
            	
            	  System.out.println(1111111);
           
            	  
            	bombReponseVo.setAccId(accID);
            	bombReponseVo.setNickName( demomember.get("bombAccId").toString());
            	bombReponseVo.setCode(0);
      			bombReponseVo.setBalance(""+(Double.parseDouble(demomember.get("creditAmt").toString())));
      			bombReponseVo.setDemo(true);
      			 
			   
               
            } else {
            	bombReponseVo.fail();
            }
        }catch(Exception e) {
        	bombReponseVo.fail();
        }

        log.info("BOMB AUTH RESbody: {}", bombReponseVo);
        return bombReponseVo;
    }

	
	
	
	@ResponseBody
	 @RequestMapping(value="/bet", method=RequestMethod.POST)
	    public  BombReponseVo minegameEncBet(@RequestBody BombAuthVO bombAuthVO, HttpServletRequest request) throws Exception {
			BombReponseVo bombReponseVo = new BombReponseVo();

	    	Gson gson = new Gson();
	    	bombAuthVO = gson.fromJson(AesUtil.getAES128_Decode(bombAuthVO.getEp()), BombAuthVO.class);

	        JSONObject obj = new JSONObject();

	        
	        	        
	  	        
	        HashMap memParam = new HashMap();
	        memParam.put("bombAccId", bombAuthVO.getLoginToken());
	        HashMap demomember =  siteService.getDemoByBombAccId(memParam);
	        double balance = Double.parseDouble(demomember.get("creditAmt").toString());
	        double betAmount = (double)bombAuthVO.getAmount();
	        log.warn("DEMO BALANCE memINFO {} {} ", demomember.toString(), betAmount);
	        if(balance < balance - betAmount) {
                
                bombReponseVo.setCode(-99);
                return bombReponseVo;
            } else {
            	bombReponseVo.setBalance("" +( balance - betAmount));
           	 memParam.put("creditAmt", "" +( balance - betAmount));
            	siteService.updateDemoUSer(memParam); 
            }
	        log.warn("DEMO BALANCE memParam {}", memParam.toString());
	       
	        return bombReponseVo;
	    }
	
	
	@ResponseBody
	 @RequestMapping(value={"/payout", "/end"}, method=RequestMethod.POST)
	    public  BombReponseVo minegameEncCredit(@RequestBody BombAuthVO bombAuthVO, HttpServletRequest request) throws Exception {
		BombReponseVo bombReponseVo = new BombReponseVo();

    	Gson gson = new Gson();
    	bombAuthVO = gson.fromJson(AesUtil.getAES128_Decode(bombAuthVO.getEp()), BombAuthVO.class);

        JSONObject obj = new JSONObject();

        
        	        
  	        
        HashMap memParam = new HashMap();
        memParam.put("bombAccId", bombAuthVO.getLoginToken());
        HashMap demomember =  siteService.getDemoByBombAccId(memParam);
      
        double balance = Double.parseDouble(demomember.get("creditAmt").toString());
        double betAmount = (double)bombAuthVO.getAmount();
        log.warn("DEMO BALANCE memINFO {} {} ", demomember.toString(), betAmount);
    	bombReponseVo.setBalance("" +( balance + betAmount));
    	 memParam.put("creditAmt", "" +( balance + betAmount));
    	siteService.updateDemoUSer(memParam);
    	  log.warn("DEMO BALANCE memParam {}", memParam.toString());
        return bombReponseVo;


	       
	    }
	
	
	

}
