package com.bb.admin.controller;

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

import jakarta.servlet.http.HttpServletRequest;

import org.codehaus.jettison.json.JSONObject;
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.RequestMapping;

import com.bb.model.PageFormVO;
import com.bb.model.Site;
import com.bb.model.SiteSearch;
import com.bb.service.SiteService;
import com.bb.service.StatService;
import com.bb.util.DateTimeUtils;
import com.bb.util.PagingUtil;
import com.fasterxml.jackson.databind.ObjectMapper;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Controller
@RequestMapping(value = "/stat")
@PreAuthorize("hasRole('ADMIN')")
public class StatController {

	@Autowired
	StatService statService;

	@Autowired
	SiteService siteService;

	@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());
		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());

		System.out.println(search.toString());
		List<HashMap> list = statService.getSiteDailyReportList(search);
		model.put("list", list);

		System.out.println(list.size());

		return "admin/stat";
	}

	@GetMapping("/bottom")
	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);

		search.setUpperSiteIdx(loginSite.getSiteIdx());

		List<HashMap> list = statService.getBottomSiteDailyReportList(search);
		model.put("list", list);
		return "admin/statBottom";
	}

	@GetMapping("/pot")
	public String mypage(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);

		String currDate = DateTimeUtils.getYyyymmdd();
		String yyyy = currDate.substring(0, 4);
		search.setSiteIdx(loginSite.getSiteIdx());
		int mm = Integer.parseInt(currDate.substring(4, 6));
		if (search.getStartDate() == null) {

			String mmm = currDate.substring(4, 6);
			model.put("yyyy", yyyy);
			model.put("mmm", mmm);
			search.setStartDate(mmm);
		} else {

			String mmm = search.getStartDate();
			model.put("yyyy", yyyy);
			model.put("mmm", mmm);
			search.setStartDate(mmm);

		}

		// model.put("", yyyy);
		model.put("mm", mm);

		HashMap list = statService.getMonthCredit(search);
		model.put("list", list);

		return "admin/statPot";
	}

	@GetMapping("/casino")
	public String statCasino(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> list = statService.getVendorDailyReportList(search);
		model.put("list", list);

		return "admin/statCasino";
	}

	@GetMapping("/user")
	public String statUser(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());

		if (search.getStartDate() == null || "".equals(search.getStartDate())) {
			String currDate = DateTimeUtils.getYyyymm();
			search.setStartDate(currDate.substring(0, 4));
			search.setEndDate(currDate.substring(4, 6));
		}

		System.out.println(search.getStartDate());
		List<HashMap> list = statService.getUserDailyReportList(search);

		model.put("list", list);
		model.put("list", list);

		return "admin/statUser";
	}

	@GetMapping("/date")
	public String statDate(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());

		System.out.println(search.getStartDate());
		List<HashMap> list = statService.getUserReportSum(search);
		model.put("list", list);

		return "admin/statDate";
	}

	@GetMapping("/agentReport")
	public String agentReport(HttpServletRequest request, ModelMap model, @ModelAttribute("searchVO") SiteSearch search)
			throws Exception {
		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		Site loginSite = (Site) authentication.getDetails();
		
		final String LOG_PREFIX = "#-SUPER::agentReport::::";
		
		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());
		search.setSiteId(loginSite.getSiteId());
		
		if(search.getSearchSiteId() != null && !"".equals(search.getSearchSiteId())) {
			Site targetSite = siteService.getSiteSimpleDetail(search);
			if(targetSite != null) {
				search.setSearchSiteIdx(targetSite.getSiteIdx());
			} else {
				search.setSearchSiteIdx(0);
			}
		} else {
			search.setSearchSiteIdx(0);
		}
		
		String toDay = new SimpleDateFormat("yyyy-MM-dd").format(new Date(System.currentTimeMillis()));
		String startDay = "";
    	String endDay = "";
		if(search.getStartDate() == null || "".equals(search.getStartDate())) {
			startDay = toDay;
			search.setStartDate(startDay);
		} else {
			startDay = search.getStartDate().substring(0, 10);
		}
		
		if(search.getEndDate() == null || "".equals(search.getEndDate())) {
			endDay = toDay;
			search.setEndDate(endDay);
		} else {
			endDay = search.getEndDate().substring(0, 10);
		}
		
		log.info(LOG_PREFIX+ "startDay::"+startDay);
    	log.info(LOG_PREFIX+ "endDay::"+endDay);
    	log.info(LOG_PREFIX+ "toDay::"+toDay);
    	search.setSiteDispYn("N");
		if(startDay.equals(toDay) || endDay.equals(toDay)) {
			search.setSiteDispYn("Y");
		}
		
		ObjectMapper objectMapper = new ObjectMapper();
		String reqJsonStr = objectMapper.writeValueAsString(search);
		JSONObject reqObj = new JSONObject(reqJsonStr);
		log.info(LOG_PREFIX+ "Request {}", reqObj);
		
		PageFormVO pageVo= new PageFormVO();
		if(loginSite.getSiteLevel() > 0) {
			search.setUpperSiteId(loginSite.getSiteId());
		}
		
		int totalCount = statService.getAgentReportCnt(search);
		if (totalCount != 0) {
			PageFormVO commonForm = new PageFormVO();
			commonForm.setFunction_name("goPage");
			commonForm.setPage(search.getPage());
			commonForm.setCount_per_page(20);
			commonForm.setCount_per_list(30);
			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());
		}
		log.info(LOG_PREFIX+ "search.getStartDate {}", search.getStartDate());
		log.info(LOG_PREFIX+ "search.getEndDate {}", search.getEndDate());
		log.info(LOG_PREFIX+ "search.getSearchSiteIdx {}", search.getSearchSiteIdx());
		log.info(LOG_PREFIX+ "search.getPage {}", search.getPage());
		log.info(LOG_PREFIX+ "search.getLimit {}", search.getLimit());
		log.info(LOG_PREFIX+ "search.getOffset {}", search.getOffset());
		
		List<HashMap> siteList = statService.getAgentReportList(search);
		model.put("siteList", siteList);

		return "admin/agentReport";
	}

}
