package cn.com.countrygarden.bi.main.sms;

import cn.com.countrygarden.bi.main.exception.ServiceException;
import cn.com.countrygarden.bi.main.sms.client.AsyncApiClientSmsService;
import cn.com.countrygarden.bi.main.sms.client.SyncApiClientSmsService;

import com.alibaba.cloudapi.sdk.core.model.ApiResponse;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;

/**
 * package: com.alicom.dysms.api
 * describe: 短信基础管理
 * creat_user: lsl
 * creat_date: 2018/7/9
 * creat_time: 17:48
 **/
public class SmsBaseManager {

    private final static Logger logger = LoggerFactory.getLogger(SmsBaseManager.class);
    /**
     * 授权码
     */
    private String appkey;
    private String appSecret;
    private static String appUrl;
    private static String appPersonUrl;

    

    public SmsBaseManager(String appkey, String appSecret,String appUrl,String appPersonUrl) {
		super();
		if(StringUtils.isBlank(appkey)||StringUtils.isBlank(appSecret)){
            throw new ServiceException("短信初始化异常！缺少必要参数AppKey或appSecret");
        }
		this.appkey = appkey;
		this.appSecret = appSecret;
		this.appUrl=appUrl;
		this.appPersonUrl=appPersonUrl;
	}

	public SmsBaseManager(String appkey, String appSecret,String appUrl) {
        if(StringUtils.isBlank(appkey)||StringUtils.isBlank(appSecret)){
            throw new ServiceException("短信初始化异常！缺少必要参数AppKey或appSecret");
        }
        this.appkey = appkey;
        this.appSecret = appSecret;
        this.appUrl=appUrl;
    }

    public SyncApiClientSmsService getSyncClientInstance(){
        return SyncApiClientSmsService.newBuilder()
                .appKey(this.appkey)
                .appSecret(this.appSecret)
                .build();
    }

    public AsyncApiClientSmsService getASyncClientInstance(){
        return AsyncApiClientSmsService.newBuilder()
                .appKey(this.appkey)
                .appSecret(this.appSecret)
                .build();
    }

    /**
     * 短信异常打印
     * @param response
     */
    public static void printResponse(ApiResponse response){
    	try {
			logger.info(new String(response.getBody(), "utf-8"));
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
        if(response==null){
            logger.error("短信服务器无任何响应信息！");
        }
        if(response.getStatusCode()!=0||response.getStatusCode()!=200){
            logger.error("===============发送异常===============");
            logger.error("code = "+response.getStatusCode());
            try {
                logger.error("result content = "+new String(response.getBody(), "utf-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            logger.error("=====================================");
        }
    }

    
    
	public static String getAppUrl() {
		return appUrl;
	}

	

	public static String getAppPersonUrl() {
		return appPersonUrl;
	}

	public static void setAppPersonUrl(String appPersonUrl) {
		SmsBaseManager.appPersonUrl = appPersonUrl;
	}

	/**
	 * 
	 * 短信类型。
	 * verify 验证码；
	 * marketing 营销，默认 verify
	 * 
		 验证码通道: iCRaxDorqWxlylft
		营销短信通道: fr4SJVclw4d0jb1W
	 * @param args
	 */
    public static void main(String[] args) {
        SmsBaseManager smsBaseManager=new SmsBaseManager("24953301","caf3cb74b38bd7d12f017fb22cb3783c","");
        SyncApiClientSmsService clientInstance = smsBaseManager.getSyncClientInstance();
//        ApiResponse response = clientInstance.postSms(null, null, null, null, null, null,
//                "13077403326",
//                "亲爱的李四，您有一份关于张三的调查问卷，请点击链接参与评价：http://t.cn/Rggc16u！",
//                "marketing", "fr4SJVclw4d0jb1W");
//        
//        printResponse(response);
        ApiResponse apiResponse = clientInstance.getSms(null, null, null, null,
                null, null, 490);
        printResponse(apiResponse);
//        
//        AsyncApiClientSmsService aclientInstance = smsBaseManager.getASyncClientInstance();
//        aclientInstance.postSms(null, null, null, null, null, null, "13077403326",
//                "亲爱的李四，您有一份关于张三的调查问卷，请点击链接参与评价：http://t.cn/Rggc16u！",
//                "verify", "fr4SJVclw4d0jb1W", new ApiCallBack() {
//					
//					@Override
//					public void onResponse(ApiRequest arg0, ApiResponse arg1) {
//						// TODO Auto-generated method stub
//						printResponse(arg1);
//					}
//					
//					@Override
//					public void onFailure(ApiRequest arg0, Exception arg1) {
//						// TODO Auto-generated method stub
//						arg1.printStackTrace();
//					}
//				});
//        
//        aclientInstance.getSms(null, null, null, null,
//                null, null, 323,new ApiCallBack() {
//
//					@Override
//					public void onFailure(ApiRequest arg0, Exception arg1) {
//						// TODO Auto-generated method stub
//						arg1.printStackTrace();
//					}
//					@Override
//					public void onResponse(ApiRequest arg0, ApiResponse arg1) {
//						// TODO Auto-generated method stub
//						printResponse(arg1);
//					}
//        	
//        });

    }


}
