package com.fjs.scenic.controller;

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

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fjs.scenic.service.user.UserService;
import com.fjs.scenic.utils.Cons;
import com.fjs.scenic.utils.ReturnResult;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@RestController
@RequestMapping("/api/personal")
public class PersonalCenterController {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation(value = "个人资料", notes = "个人资料")
    @GetMapping("/myDetail")
    public ReturnResult myDetail(HttpServletRequest request){
        String token = request.getHeader(Cons.TOKEN.WECHAT);
        Integer userId = (Integer) redisTemplate.opsForValue().get(token);
        if(userId==null){
        	return ReturnResult.build(500, "登录信息已过期");
        }
        return ReturnResult.ok(userService.getUserById(userId));
    }

    @ApiOperation(value = "手机绑定", notes = "手机绑定")
    @PostMapping("/bindingPhone")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", dataType = "string", value = "手机号", required = true, paramType = "query"),
            @ApiImplicitParam(name = "code", dataType = "string", value = "验证码", required = true, paramType = "query")
    })
    public ReturnResult bindingPhone(HttpServletRequest request,String phone,String code){
    	String token = request.getHeader(Cons.TOKEN.WECHAT);
        Integer userId = (Integer) redisTemplate.opsForValue().get(token);
        if(userId==null){
        	return ReturnResult.build(500, "登录信息已过期");
        }
        String c=(String)redisTemplate.opsForValue().get(phone);
        redisTemplate.delete(phone);
        if(c==null){
            return ReturnResult.build(500,"请重新获取验证码");
        }else {
            if(!c.equals(code)){
                return ReturnResult.build(500,"验证码错误");
            }
        }
        return userService.updatePhone(userId,phone);
    }

    @ApiOperation(value = "获取验证码", notes = "获取验证码")
    @GetMapping("/getCode/{phone}")
    public ReturnResult getCode(
            @ApiParam(required = true, name = "phone", value = "phone") @PathVariable("phone") String phone) {
        // 生成短信验证码
        String randomCode = RandomStringUtils.randomNumeric(4);
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("code", randomCode);
        /*ReturnResult b = MessageByAli.sendMsg(phone,"\u4f11\u5ea6\u79d1\u6280","SMS_162845048",paramMap);
        if(b.getStatus()==200){
            redisTemplate.opsForValue().set(phone, randomCode, 30 * 60 * 1000, TimeUnit.SECONDS);
            return ReturnResult.ok();
        }*/
        return ReturnResult.build(500,"发送失败");
    }
}
