package com.bb.service;

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

import jakarta.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import com.bb.dao.CreditDao;
import com.bb.dao.SiteDao;
import com.bb.model.CashParam;
import com.bb.model.Credit;
import com.bb.model.CreditPoint;
import com.bb.model.CreditPointSearch;
import com.bb.model.CreditSearchVO;
import com.bb.model.Insure;
import com.bb.model.SiteSearch;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class CreditServiceImpl implements  CreditService{

	@Autowired
	private CreditDao creditDao;

	@Autowired
	private SiteDao siteDao;


	@Autowired
	private  DataSourceTransactionManager transactionManager;


	@Override
	public int insertCredit(Credit credit) {
		TransactionStatus txStatus = transactionManager.getTransaction(new DefaultTransactionDefinition());
		int res = 0;

		long siteCredit = siteDao.getSiteCredit(credit.getTargetSiteId());
		log.info("siteCredit" + siteCredit);
		try {
			log.info("siteCredit" + siteCredit);
			if(credit.getCreditAmt() != 0) {
				credit.setPreCreditAmt(siteCredit);
				creditDao.insertCredit(credit);
				creditDao.insertCreditAiAo(credit);
			}
			if(credit.getSiteLevel() == 1
					&& ("AI".equals(credit.getCreditType()) || "AO".equals(credit.getCreditType()))
			) {
				creditDao.updateSubCredit(credit);
			}

			res = 0;
			transactionManager.commit(txStatus);
		} catch(Exception e) {
			System.out.println("ERROR");
			System.out.println(e.toString());
			res =-1;
			transactionManager.rollback(txStatus);
		} finally {
			return res;
		}
	}

	@Override
	public int insertCreditByCash(Credit credit) {
		TransactionStatus txStatus = transactionManager.getTransaction(new DefaultTransactionDefinition());
		int res = 0;

		long siteCredit = siteDao.getSiteCredit(credit.getSiteId());
		HashMap balanceMap = new HashMap<>();
		balanceMap.put("siteIdx", credit.getSiteIdx());
		balanceMap.put("memberIdx", credit.getTargetSiteIdx());
		int userBalance = siteDao.getUserBalance(balanceMap);
		try {
			log.info("siteCredit" + siteCredit);
			credit.setPreCreditAmt(siteCredit);
			credit.setPreBalance(userBalance);
			log.info("siteCredit" + siteCredit);
			creditDao.insertCreditByCash(credit);
			creditDao.insertCreditAiAo(credit);
			res = 0;
			transactionManager.commit(txStatus);
		} catch(Exception e) {
			System.out.println("ERROR");
			System.out.println(e.toString());
			res =-1;
			transactionManager.rollback(txStatus);
		} finally {
			return res;
		}
	}


	@Override
	public int getCreditListCnt(SiteSearch search) {
		// TODO Auto-generated method stub
		return creditDao.getCreditListCnt(search);
	}


	@Override
	public List<HashMap> getCreditList(SiteSearch search) {
		// TODO Auto-generated method stub
		return creditDao.getCreditList(search);
	}


	@Override
	public int getSiteTranListCnt(SiteSearch search) {
		// TODO Auto-generated method stub
		return creditDao.getSiteTranListCnt(search);
	}


	@Override
	public List<HashMap> getSiteTranList(SiteSearch search) {
		// TODO Auto-generated method stub
		return creditDao.getSiteTranList(search);
	}


	@Override
	public int updateStatus(Credit credit) {
		// TODO Auto-generated method stub
		int res = 0;
		try {
			creditDao.updateStatus(credit);
		}catch(Exception e) {

		}
		return res;
	}


	@Override
	public int getPointListCnt(SiteSearch search) {
		return creditDao.getPointListCnt(search);
	}


	@Override
	public List<HashMap> getPointList(SiteSearch search) {
		return creditDao.getPointList(search);
	}


	@Override
	public int getSiteBetListCnt(SiteSearch search) {
		return creditDao.getSiteBetListCnt(search);
	}


	@Override
	public List<HashMap> getSiteBetList(SiteSearch search) {
		return creditDao.getSiteBetList(search);
	}


	@Override
	public int insertInsure(Insure insure) {
		TransactionStatus txStatus = transactionManager.getTransaction(new DefaultTransactionDefinition());
		int res = 0;


		long siteInsure = siteDao.getInsureAmt(insure.getSiteId());

		log.info("siteInsure" + siteInsure);
		try {

			insure.setPreInsureAmt(siteInsure);

			creditDao.insertInsure(insure);


			res =0;
			transactionManager.commit(txStatus);

		}catch(Exception e) {
			System.out.println("ERROR");
			System.out.println(e.toString());
			res =-1;
			transactionManager.rollback(txStatus);


		}finally {
			return res;
		}
	}


	@Override
	public int updateInsureStatus(Insure insure) {

			int res = 0;
			try {
				creditDao.updateInsureStatus(insure);
			}catch(Exception e) {

			}
			return res;
	}


	@Override
	public HashMap getMyInsureInfo(int siteIdx) {
		// TODO Auto-generated method stub
		return 	creditDao.getMyInsureInfo(siteIdx);
	}


	@Override
	public int getInsureListCnt(SiteSearch search) {
		return creditDao.getInsureListCnt(search);
	}


	@Override
	public List<HashMap> getInsureList(SiteSearch search) {
		return creditDao.getInsureList(search);
	}


	@Override
	public HashMap getCreditSumToday(SiteSearch search) {
		// TODO Auto-generated method stub
		return creditDao.getCreditSumToday(search);
	}


	@Override
	public void updateSetStatus(Credit credit2) {
		// TODO Auto-generated method stub
		creditDao.updateSetStatus(credit2);
	}

	@Override
	public String getRoundIdByGsoft(String refId) {
		// TODO Auto-generated method stub
		return creditDao.getRoundIdByGsoft(refId);
	}


	@Override
	public int insertPoint(CreditPoint point) {
		// TODO Auto-generated method stub
		return creditDao.insertPoint(point);
	}


	@Override
	public int checkRequestTime(Credit credit) {
		// TODO Auto-generated method stub
		return creditDao.checkRequestTime(credit);
	}

	@Override
	public int getCashInOutListCnt(CashParam param) {
		// TODO Auto-generated method stub
		return creditDao.getCashInOutListCnt(param);
	}

	@Override
	public List<HashMap> getCashInOutList(CashParam param) {
		// TODO Auto-generated method stub
		return creditDao.getCashInOutList(param);
	}

	@Override
	public int getTransCreditListCnt(CreditSearchVO search) {
		// TODO Auto-generated method stub
		return creditDao.getTransCreditListCnt(search);
	}

	@Override
	public List<HashMap<String, Object>> getTransCreditList(CreditSearchVO search) {
		// TODO Auto-generated method stub
		return creditDao.getTransCreditList(search);
	}

	@Override
	public int getTotalTransactionListCnt(CreditSearchVO search) {
		// TODO Auto-generated method stub
		return creditDao.getTotalTransactionListCnt(search);
	}

	@Override
	public List<HashMap<String, Object>> getTotalTransactionList(CreditSearchVO search) {
		// TODO Auto-generated method stub
		return creditDao.getTotalTransactionList(search);
	}

	@Override
	public int getCreditPointListCnt(@Valid CreditPointSearch search) {
		// TODO Auto-generated method stub
		return creditDao.getCreditPointListCnt(search);
	}

	@Override
	public List<HashMap<String, String>> getCreditPointList(@Valid CreditPointSearch search) {
		// TODO Auto-generated method stub
		return creditDao.getCreditPointList(search);
	}

	@Override
	public int getCreditBetListCnt(@Valid CreditPointSearch search) {
		// TODO Auto-generated method stub
		return creditDao.getCreditBetListCnt(search);
	}

	@Override
	public List<HashMap<String, String>> getCreditBetList(@Valid CreditPointSearch search) {
		// TODO Auto-generated method stub
		return creditDao.getCreditBetList(search);
	}

	@Override
	public int getStatCreditListCnt(SiteSearch search) {
		// TODO Auto-generated method stub
		return creditDao.getStatCreditListCnt(search);
	}

	@Override
	public List<HashMap> getStatCreditList(SiteSearch search) {
		// TODO Auto-generated method stub
		return creditDao.getStatCreditList(search);
	}

	@Override
	public HashMap getStatCreditTotal(SiteSearch search) {
		// TODO Auto-generated method stub
		return creditDao.getStatCreditTotal(search);
	}





}
