package com.bb.admin.controller;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;

import jakarta.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bb.model.PageFormVO;
import com.bb.model.Site;
import com.bb.model.SiteSearch;
import com.bb.model.WithdrewRequest;
import com.bb.service.CreditService;
import com.bb.service.GsoftService;
import com.bb.service.SiteService;
import com.bb.service.TplusService;
import com.bb.util.PagingUtil;

import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;



@Slf4j
@Controller
@RequestMapping(value = "user")
@PreAuthorize("hasRole('ADMIN')")
public class UserController {
	
	@Autowired
	SiteService siteService;
	
	@Autowired
	TplusService tplusService;
	
	@Autowired
	GsoftService gsoftService;
	
	@Autowired
	CreditService creditService;
	
	  @GetMapping("")
	    public String loginForm( HttpServletRequest request, ModelMap model , @ModelAttribute("searchVO") SiteSearch search) throws Exception {
			Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
			Site loginSite = (Site)authentication.getDetails();
	    	model.put("loginSite", loginSite);
	    	long siteCredit = siteService.getSiteCredit(loginSite.getSiteId());
	    	model.put("underSiteCredit", 0);	
	    	model.put("siteCredit",siteCredit);
	    	model.put("sitePoint", 0);	
	    	model.put("creditWait", 0);	 
	    	model.put("insureAmt", 0);	  
	    	model.put("insurePointAmt", 0);	  
	    	
	    	PageFormVO pageVo= new PageFormVO();
	    	log.info("#-SUPER::USER_DETAIL::Search : "+search.toString());
	    	
			int totalCount = siteService.getSiteUserListCnt(search);
			if (totalCount != 0) {
				PageFormVO commonForm = new PageFormVO();
				commonForm.setFunction_name("goPage");
				commonForm.setPage(search.getPage());
				commonForm.setCount_per_page(20);
				if(search.getCount_per_list()==0) {
					commonForm.setCount_per_list(100);
				} else {
					commonForm.setCount_per_list(search.getCount_per_list());
				}
				commonForm.setTatal_list_count(totalCount);
				pageVo = PagingUtil.setPageUtil(commonForm);
				search.setLimit(pageVo.getLimit());
				search.setOffset(pageVo.getOffset());
				search.setTatal_list_count(totalCount);
				
				model.put("pageInfo", pageVo.getPagination());
			}
		
			List<HashMap> list = siteService.getSiteUserList(search);
			model.put("list", list);
	    	
	    	return "admin/user";        
	    }
		
		
	@GetMapping("/parseOnOff")
	public @ResponseBody JSONObject parseOnOff(HttpServletRequest request, ModelMap model, @RequestParam("memberIdx") String memberIdx, @RequestParam("pStatus") String pStatus) throws Exception {
		JSONObject obj = new JSONObject();
		
		final String LOG_PREFIX = "#-Proc::Evo::parseOnOff:::";
		HashMap param = new HashMap<>();
		param.put("memberIdx", memberIdx);
		param.put("parseEvoYn", pStatus);
		log.info(LOG_PREFIX + "전체 사이트 파싱설정::" + param.toString() + "::START");
		
		int result = siteService.updateParseEvoYn(param);
		log.info(LOG_PREFIX + "updateParseEvoYn result::" + result + "::END");
		
		obj.put("RES", "SUCCESS");
		
		return obj;
	}
	  
	  
	  @GetMapping("/betlist")
	    public String index( HttpServletRequest request, ModelMap model , @ModelAttribute("searchVO") SiteSearch search) throws Exception {
			Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
			Site loginSite = (Site)authentication.getDetails();
	    	model.put("loginSite", loginSite);
	    	long siteCredit = siteService.getSiteCredit(loginSite.getSiteId());
	    	long underSiteCredit = siteService.getUnderSiteCredit(loginSite.getSiteId());
	    	model.put("underSiteCredit", underSiteCredit);	
	    	model.put("siteCredit", siteCredit);
	    	long sitePoint = siteService.getSitePoint(loginSite.getSiteId());
	    	model.put("sitePoint", sitePoint);	
	    	HashMap creditWait = siteService.getCreditWait(loginSite.getSiteIdx());
	    	model.put("creditWait", creditWait);	  
	    	long insureAmt = siteService.getInsureAmt(loginSite.getSiteId());
	    	long insurePointAmt = siteService.getInsurePointAmt(loginSite.getSiteId());	
	    	model.put("insureAmt", insureAmt);	  
	    	model.put("insurePointAmt", insurePointAmt);	  
	    	 
	    	
	    	
	    	PageFormVO pageVo= new PageFormVO();	    	
	    	search.setSiteId(loginSite.getSiteId());
	    	search.setSiteIdx(loginSite.getSiteIdx());
	    	
						
			int totalCount = creditService.getSiteBetListCnt(search);
			if (totalCount != 0) {
				PageFormVO commonForm = new PageFormVO();
				commonForm.setFunction_name("goPage");
				commonForm.setPage(search.getPage());
				commonForm.setCount_per_page(20);
				if(search.getCount_per_list()==0) {
					commonForm.setCount_per_list(15);
				} else {
					commonForm.setCount_per_list(search.getCount_per_list());
				}
				commonForm.setTatal_list_count(totalCount);
				pageVo = PagingUtil.setPageUtil(commonForm);
				search.setLimit(pageVo.getLimit());
				search.setOffset(pageVo.getOffset());
				search.setTatal_list_count(totalCount);
				
				model.put("pageInfo", pageVo.getPagination());
			}
			
		
			List<HashMap> list = creditService.getSiteBetList(search);
			log.debug(""+list.size());
			model.put("list", list);
			
	    	
	    	return "admin/betlist";        
	    }
	  
	  
	  @GetMapping("/transaction")
	    public String mypage( HttpServletRequest request, ModelMap model ) throws Exception {
			Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
			Site loginSite = (Site)authentication.getDetails();
	    	model.put("loginSite", loginSite);
	    	long siteCredit = siteService.getSiteCredit(loginSite.getSiteId());
	    	long underSiteCredit = siteService.getUnderSiteCredit(loginSite.getSiteId());
	    	model.put("underSiteCredit", underSiteCredit);	
	    	model.put("siteCredit", siteCredit);
	    	long sitePoint = siteService.getSitePoint(loginSite.getSiteId());
	    	model.put("sitePoint", sitePoint);
	    	HashMap creditWait = siteService.getCreditWait(loginSite.getSiteIdx());
	    	model.put("creditWait", creditWait);	
	    	long insureAmt = siteService.getInsureAmt(loginSite.getSiteId());
	    	long insurePointAmt = siteService.getInsurePointAmt(loginSite.getSiteId());	
	    	model.put("insureAmt", insureAmt);	  
	    	model.put("insurePointAmt", insurePointAmt);	  
	    	 
			
	    	
	    	return "admin/transactionList";        
	    }
	  
	  
	  @GetMapping("/payments")
	    public String limit( HttpServletRequest request, ModelMap model ) throws Exception {
			Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
			Site loginSite = (Site)authentication.getDetails();
	    	model.put("loginSite", loginSite);
	    	long siteCredit = siteService.getSiteCredit(loginSite.getSiteId());
	    	long underSiteCredit = siteService.getUnderSiteCredit(loginSite.getSiteId());
	    	model.put("underSiteCredit", underSiteCredit);	
	    	model.put("siteCredit", siteCredit);
	    	long sitePoint = siteService.getSitePoint(loginSite.getSiteId());
	    	model.put("sitePoint", sitePoint);	
	    	HashMap creditWait = siteService.getCreditWait(loginSite.getSiteIdx());
	    	model.put("creditWait", creditWait);
	    	long insureAmt = siteService.getInsureAmt(loginSite.getSiteId());
	    	long insurePointAmt = siteService.getInsurePointAmt(loginSite.getSiteId());	
	    	model.put("insureAmt", insureAmt);	  
	    	model.put("insurePointAmt", insurePointAmt);	  
	    	 
			
	    	
	    	return "admin/payments";        
	    }
	  
	  
	  @GetMapping("/std")
	    public String std ( HttpServletRequest request, ModelMap model , @ModelAttribute("searchVO") SiteSearch search) throws Exception {
		  Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
			Site loginSite = (Site)authentication.getDetails();
	    	model.put("loginSite", loginSite);
	    	long siteCredit = siteService.getSiteCredit(loginSite.getSiteId());
	    	long underSiteCredit = siteService.getUnderSiteCredit(loginSite.getSiteId());
	    	model.put("underSiteCredit", underSiteCredit);	
	    	model.put("siteCredit", siteCredit);
	    	long sitePoint = siteService.getSitePoint(loginSite.getSiteId());
	    	model.put("sitePoint", sitePoint);	
	    	HashMap creditWait = siteService.getCreditWait(loginSite.getSiteIdx());
	    	model.put("creditWait", creditWait);	  
	    	long insureAmt = siteService.getInsureAmt(loginSite.getSiteId());
	    	long insurePointAmt = siteService.getInsurePointAmt(loginSite.getSiteId());	
	    	model.put("insureAmt", insureAmt);	  
	    	model.put("insurePointAmt", insurePointAmt);
	    	
	     	search.setSiteIdx(loginSite.getSiteIdx());	 
	     	
	        List<HashMap> venderAcountList = siteService.getTrasferVendorList();
	        
	        
	        for(HashMap vendor : venderAcountList) {
	        	String balance ="0";
	        	vendor.put("balance", balance);
	        }
	    	
	    	model.put("list", venderAcountList);
	    	
	    	return "admin/std";        
	    }
	  
	  
	  @PostMapping("/withdrawAll")
	    public synchronized @ResponseBody JSONObject withdrawAll(HttpServletRequest request, ModelMap model, WithdrewRequest withdrewRequest) throws Exception {
		  log.warn( " member withdrawAll 시작" + withdrewRequest.toString() );
			JSONObject obj = new JSONObject();      
			
			  List<HashMap> allMemberList = siteService.getAllMember2();
			  for(HashMap member:allMemberList) {
				  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					long reqAtL = System.currentTimeMillis();
					String reqAt = sdf.format(reqAtL);
			  }
			  
			  log.info( " member betCheck 끝" );
				obj.put("RES", "SUCCESS");
			return obj;
	    }
	  
	  
}
