package com.izouma.awesomeadmin.web;

import com.izouma.awesomeadmin.constant.RongConstant;
import com.izouma.awesomeadmin.dto.Result;
import com.izouma.awesomeadmin.service.RongYunTokenService;
import com.izouma.awesomeadmin.util.PropertiesFileLoader;
import io.rong.RongCloud;
import io.rong.models.SMSSendCodeResult;
import io.rong.models.SMSVerifyCodeResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;


@Controller
@RequestMapping("/rong")
public class RongCloudController  {

    @Autowired
    private RongYunTokenService rongYunTokenService;


    private RongCloud rongCloud = RongCloud.getInstance(PropertiesFileLoader.getProperties("rongyunappkey"), PropertiesFileLoader.getProperties("rongyunappsecret"));

    @RequestMapping(value = "/sendCode", method = RequestMethod.GET)
    @ResponseBody
    public Result sendCode(@RequestParam("phone") String mobile, HttpServletRequest request) {
        try {
            SMSSendCodeResult smsSendCodeResult = rongCloud.sms.sendCode(mobile, RongConstant.VERIFY_TEMPLATEID, "86", null, null);

            if (200 == smsSendCodeResult.getCode()) {

                String sessionId = smsSendCodeResult.getSessionId();

                return new Result(true, new StringBuilder(sessionId));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return new Result(false, "发送失败，请稍后重试");
    }


    @RequestMapping(value = "/verifyCode", method = RequestMethod.GET)
    @ResponseBody
    public Result verifyCode(@RequestParam("sessionId") String sessionId,
                             @RequestParam("code") String code) {
        try {
            SMSVerifyCodeResult sMSVerifyCodeResult = rongCloud.sms.verifyCode(sessionId, code);
            if (200 == sMSVerifyCodeResult.getCode()) {

                Boolean success = sMSVerifyCodeResult.getSuccess();
                if (success) {

                    return new Result(true, null);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return new Result(false, "验证失败");
    }


    @RequestMapping(value = "/sendNotify", method = RequestMethod.GET)
    @ResponseBody
    public Result sendNotify(@RequestParam(required = true, value = "mobile") String mobile) {

        String result;
        try {
            SMSSendCodeResult smsSendCodeResult = rongCloud.sms.sendNotify(mobile, RongConstant.NOTIFY_TEMPLATEID, "86", null, null, null);
            if (200 == smsSendCodeResult.getCode()) {

                String sessionId = smsSendCodeResult.getSessionId();
                return new Result(true, sessionId);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return new Result(false, "发送失败，请稍后重试");
    }
}
