package sgc.portal.com.com.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import sgc.portal.base.util.EasyHashMap;
import sgc.portal.base.websquare.GridRowStatus;
import sgc.portal.com.com.model.ComSendMsgPopVo;
import sgc.portal.com.com.persistence.ComSendMsgPopMapper;

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

@Service
public class ComSendMsgPopService {
    private final ComSendMsgPopMapper mapper;
    
    @Autowired
    private Environment environment;

    public ComSendMsgPopService(ComSendMsgPopMapper mapper) {
        this.mapper = mapper;
    }
    
    @Transactional
    public Map<String, String> sendLms(ComSendMsgPopVo comSendMsgPopVo) {
    	
    	Map<String,String> result = new HashMap<>();
    	String[] activeProfiles = environment.getActiveProfiles();
    	
    	//운영에서만 SMS전송 테이블에 데이터 INSERT
		if(activeProfiles[0].equals("local")) {			//local
			insertLms(comSendMsgPopVo);
			result.put("status", "OK");
	    	result.put("gubun", comSendMsgPopVo.getGubun());
	    	result.put("sconts", comSendMsgPopVo.getSconts());
		} else if(activeProfiles[0].equals("dev")) {	//개발
			result.put("status", "OK");
	    	result.put("gubun", comSendMsgPopVo.getGubun());
	    	result.put("sconts", comSendMsgPopVo.getSconts());
		} else {										//운영
			insertLms(comSendMsgPopVo);
			result.put("status", "OK");
	    	result.put("gubun", comSendMsgPopVo.getGubun());
	    	result.put("sconts", comSendMsgPopVo.getSconts());
		}
		
        return result;
    }
    
    @Transactional
    public Map<String, String> sendSms(ComSendMsgPopVo comSendMsgPopVo) {
    	
    	Map<String,String> result = new HashMap<>();
    	String[] activeProfiles = environment.getActiveProfiles();
    	
    	//운영에서만 SMS전송 테이블에 데이터 INSERT
		if(activeProfiles[0].equals("local")) {			//local
			insertSms(comSendMsgPopVo);
			result.put("status", "OK");
	    	result.put("gubun", comSendMsgPopVo.getGubun());
	    	result.put("sconts", comSendMsgPopVo.getSconts());
		} else if(activeProfiles[0].equals("dev")) {	//개발
			result.put("status", "OK");
	    	result.put("gubun", comSendMsgPopVo.getGubun());
	    	result.put("sconts", comSendMsgPopVo.getSconts());
		} else {										//운영
			insertSms(comSendMsgPopVo);
			result.put("status", "OK");
	    	result.put("gubun", comSendMsgPopVo.getGubun());
	    	result.put("sconts", comSendMsgPopVo.getSconts());
		}
		
        return result;
    }
    
    @Transactional
    public Map<String, String> sendSmsM(List<ComSendMsgPopVo> ComSendMsgPopVo) {
    	
    	Map<String,String> result = new HashMap<>();
    	String[] activeProfiles = environment.getActiveProfiles();
    	
    	for (ComSendMsgPopVo comSendMsgPopVo : ComSendMsgPopVo) {
    		//운영에서만 SMS전송 테이블에 데이터 INSERT
    		if(activeProfiles[0].equals("local")) {			//local
    			result.put("status", "OK");
    	    	result.put("gubun", comSendMsgPopVo.getGubun());
    	    	result.put("sconts", comSendMsgPopVo.getSconts());
    		} else if(activeProfiles[0].equals("dev")) {	//개발
    			result.put("status", "OK");
    	    	result.put("gubun", comSendMsgPopVo.getGubun());
    	    	result.put("sconts", comSendMsgPopVo.getSconts());
    		} else if(activeProfiles[0].equals("prod")) {	//운영
    			insertSms(comSendMsgPopVo);
    			result.put("status", "OK");
    	    	result.put("gubun", comSendMsgPopVo.getGubun());
    	    	result.put("sconts", comSendMsgPopVo.getSconts());
    		} else {
    			result.put("status", "NO");
    	    	result.put("gubun", "");
    	    	result.put("sconts", "");
    		}
    	}
        return result;
    }
    
    private void insertLms(ComSendMsgPopVo comSendMsgPopVo) {
        mapper.insertLms(comSendMsgPopVo);
    }
    
    private void insertSms(ComSendMsgPopVo comSendMsgPopVo) {
        mapper.insertSms(comSendMsgPopVo);
    }
}
