package com.madeu.service.web.webpopbillpoint.impl;

import com.madeu.constants.Constants;
import com.madeu.service.web.webpopbillpoint.WebPopbillPointService;
import com.popbill.api.PopbillException;
import com.popbill.api.TaxinvoiceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Slf4j
@Service("WebPopbillPointService")
public class WebPopbillPointServiceImpl implements WebPopbillPointService{

	@Value("${popbill.corp-num}")
	String corpNum;

	@Value("${popbill.user-id}")
	String userId;

	@Autowired
	private TaxinvoiceService taxinvoiceService;
	
	/**
	 * 잔여 포인트 조회
	 * 
	 * @param paramMap
	 * @return
	 * @throws Exception 
	 */
	@Override
	public HashMap<String, Object> selectPopbillPoint(HashMap<String, Object> paramMap) throws Exception {
		
		log.debug("WebPopbillPointServiceImpl selectPopbillPoint START");
		
		HashMap<String, Object> map = new HashMap<String, Object>();
		
		log.debug("loginMemberId       :"+paramMap.get("loginMemberId"));
		
		try {
			boolean check = true;
			
			if(check == true) {

				log.debug("corpNum :" + corpNum);

	            try {
	                double remainPoint = taxinvoiceService.getBalance(corpNum);
	                
	                log.debug("remainPoint : "+remainPoint);

	                map.put("msgCode", Constants.OK);
					map.put("success", "true");
					map.put("point", remainPoint);
					
	            } catch (PopbillException e) {
	            	log.debug("오류 코드   :" + e.getCode());
                    log.debug("오류 메시지 :" + e.getMessage());
	            }
				
			} else {
				
			}
			
		}catch(Exception e){
			e.printStackTrace();
            throw e;
		}
		
		log.debug("WebPopbillPointServiceImpl selectPopbillPoint END");
		
		return map;
	}
	
	/**
	 * 포인트 충전
	 * 
	 * @param paramMap
	 * @return
	 * @throws Exception 
	 */
	@Override
	public HashMap<String, Object> chargePopbillPointIntro(HashMap<String, Object> paramMap) throws Exception {
		
		log.debug("WebPopbillPointServiceImpl chargePopbillPointIntro START");
		
		HashMap<String, Object> map = new HashMap<String, Object>();
		
		log.debug("loginMemberId       :"+paramMap.get("loginMemberId"));
		
		try {
			boolean check = true;
			
			if(check == true) {

				log.debug("corpNum :" + corpNum);
				log.debug("userId  :" + userId);

	            try {
	            	String url = taxinvoiceService.getChargeURL(corpNum, userId);
	                
	                log.debug("url : "+url);

	                map.put("msgCode", Constants.OK);
					map.put("success", "true");
					map.put("url", url);
					
	            } catch (PopbillException e) {
	            	log.debug("오류 코드   :" + e.getCode());
                    log.debug("오류 메시지 :" + e.getMessage());
	            }
				
			} else {
				
			}
			
		}catch(Exception e){
			e.printStackTrace();
            throw e;
		}
		
		log.debug("WebPopbillPointServiceImpl chargePopbillPointIntro END");
		
		return map;
	}
}
