/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package io.renren.modules.app.controller;

import io.renren.common.utils.R;
import io.renren.common.utils.AliyunSmsUtils;
import io.renren.modules.app.annotation.Login;
import io.renren.modules.app.annotation.LoginUser;
import io.renren.modules.app.entity.UserEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.renren.modules.app.service.SmsCodeService;

/**
 * APP测试接口
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("/app")
@Api("APP测试接口")
public class AppTestController {
    @Autowired
    private SmsCodeService smsCodeService;

    @Login
    @GetMapping("userInfo")
    @ApiOperation("获取用户信息")
    public R userInfo(@LoginUser UserEntity user) {
        return R.ok().put("user", user);
    }

    @Login
    @GetMapping("userId")
    @ApiOperation("获取用户ID")
    public R userInfo(@RequestAttribute("userId") Integer userId) {
        return R.ok().put("userId", userId);
    }

    @GetMapping("notToken")
    @ApiOperation("忽略Token验证测试")
    public R notToken() {
        return R.ok().put("msg", "无需token也能访问。。。");
    }

    @PostMapping("sendSmsTest")
    public R sendTest() {
        String[] params = new String[3];

        params[0] = "17611467373"; //
        params[1] = "中国腐蚀与防护学会"; // SignName (签名名称)
        params[2] = "SMS_196640221"; // TemplateCode(模板code)

        AliyunSmsUtils.sendSms(params);
        return R.ok();
    }

    @PostMapping("mobileCodeTest")
    public R mobileCodeTest() {
        String phone = "17611467373";
        String uuid = UUID.randomUUID().toString();

        String res = "";
        try {
            res = smsCodeService.getCode(uuid, phone);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok(res);
    }

    @PostMapping("valmobileCodeTest")
    public R valMobileCodeTest() {
        String phone = "17611467373";
        String uuid = "2aada6ef-8cf8-4932-bc76-198cff8c8809";
        String code = "437964";

        try {
            smsCodeService.validate(uuid, code, phone);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }
}
