package io.renren.modules.lianlian.controller;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
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.RestController;

import io.renren.common.utils.Result;
import io.renren.common.vo.account.AcctInfoQueryResp;
import io.renren.common.vo.user.UserInfoQueryResp;
import io.renren.modules.lianlian.service.AccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;

@Log4j2
@RestController
@RequestMapping("/api/account")
@Api(tags="账户管理")
public class AccountController {
	
	@Autowired
	private AccountService accountService;

    @GetMapping("/openAccount")
    @ApiOperation("页面开户")
    public Result<String> openAccount(@RequestParam("userId") String userId) throws Exception{
    	return new Result<String>().ok(accountService.openAccount(userId));
    }

    @GetMapping("/queryAccount")
    @ApiOperation("账户信息查询")
    public Result<AcctInfoQueryResp> queryAccount(@RequestParam("userId") String userId) throws Exception{
    	return new Result<AcctInfoQueryResp>().ok(accountService.queryAccount(userId));
    }
    
    @GetMapping("/queryUserinfo")
    @ApiOperation("用户信息查询")
    public Result<UserInfoQueryResp> queryUserinfo(@RequestParam("userId") String userId) throws Exception{
    	return new Result<UserInfoQueryResp>().ok(accountService.queryUserinfo(userId));
    }
    
    @RequestMapping(method = RequestMethod.POST, value = "/notify")
    @ApiOperation("开户异步通知")
    public String llianPayMessageNotify(HttpServletRequest request) {
        // 从请求头中获取签名值
        String signature = request.getHeader("Signature-Data");
        BufferedReader reader = null;
        try {
            // 从请求体中获取源串
            reader = new BufferedReader(new InputStreamReader(request.getInputStream(), StandardCharsets.UTF_8));
            String line;
            StringBuilder stringBuilder = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                stringBuilder.append(line);
            }
            log.info("[接收来自连连下发的异步通知] 签名值为：" + signature);
            log.info("[接收来自连连下发的异步通知] 签名源串为：" + stringBuilder.toString());

            return accountService.openNotify(stringBuilder.toString(), signature);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }
            }
        }
    }
}