package com.bjtdba.userCenter.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.bjtdba.entity.userCenter.User;
import com.bjtdba.userCenter.dao.UserDao;
import com.bjtdba.userCenter.service.WechatLogOnService;
import com.bjtdba.userCenter.system.SystemConfig;
import com.bjtdba.utils.HttpURLConnectionUtils;
import com.bjtdba.utils.ResponseUtil;
import com.bjtdba.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

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

@Service
public class WechatLogOnServiceImpl implements WechatLogOnService {

    @Autowired
    private UserDao userDao;
    @Value("${WX.WECHAT_APPID}")
    private String appid;
    @Value("${WX.WECHAT_SECRET}")
    private String secret;
    @Value("${WX.WECHAT_URL}")
    private String url;
    @Value("${WX.WECHAT_TOKEN_URL}")
    private String tokenUrl;

    @Override
    public Object wechatBindPhone(String code) {
        Map<String, Object> data = new HashMap<>();
        String url1 = SystemConfig.WECHAT_URL.replace("APPID",SystemConfig.WECHAT_APPID).replace("SECRET",SystemConfig.WECHAT_SECRET).replace("CODE",code);
        String a = HttpURLConnectionUtils.doGet(url1);
        System.out.println("微信返回数据"+a);
        String openid =(String)JSONObject.parseObject(a).get("openid");
        String unionid =(String)JSONObject.parseObject(a).get("unionid");
        String access_token =(String)JSONObject.parseObject(a).get("access_token");
        String url2 = SystemConfig.WECHAT_USER_INFO_URL.replace("ACCESS_TOKEN", access_token).replace("OPENID", openid);
        String b = HttpURLConnectionUtils.doGet(url2);
        System.out.println("微信返回数据"+b);
        String nickname =(String)JSONObject.parseObject(b).get("nickname");
        String headimgurl =(String)JSONObject.parseObject(b).get("headimgurl");
        /**
         * 拿openid 查询数据库   判断用户是否绑定    绑定成功直接登录进入系统，绑定不成功返回标示 提示用户进行绑定
         */
//        User user = userDao.selectUserByOpenid(openid);
        User user = userDao.selectUserByOpenid(unionid);
        data.put("nickname",nickname);
        data.put("headimgurl",headimgurl);
        data.put("openid",openid);
        data.put("unionid",unionid);
        if(user==null){
            data.put("code",SystemConfig.WECHAT_CODE);
            data.put("msg",SystemConfig.WECHAT_MSG);

        }else{
            data.put("user",user);
            data.put("code",SystemConfig.WECHAT_SUCCESS_CODE);
        }
        return ResponseUtil.ok(data);
    }

    @Override
    public Object wechatUserId(String code) {
        Map<String, Object> data = new HashMap<>();
        String url1 = url.replace("APPID",appid).replace("SECRET",secret).replace("CODE",code);
        String a = HttpURLConnectionUtils.doGet(url1);
        System.out.println("微信返回数据"+a);
        String openid =(String)JSONObject.parseObject(a).get("openid");
        String unionid =(String)JSONObject.parseObject(a).get("unionid");
        /**
         * 拿openid 查询数据库   判断用户是否绑定    绑定成功直接登录进入系统，绑定不成功返回标示 提示用户进行绑定
         */
        User user = userDao.selectUserByOpenid(openid);
        data.put("openid",openid);
        data.put("unionid",unionid);
        if(user==null){
            data.put("code",SystemConfig.WECHAT_CODE);
            data.put("msg",SystemConfig.WECHAT_MSG);
        }else{
            //获取token 并且要把token放到redis中
            String thoken1 = TokenUtils.thoken(user.getUid(), user.getPhone());
            Object thoken = thoken1;
            //RedisUtil.set("java_"+thoken,thoken,3000 * 60 * 60);
            data.put("thoken",thoken);
            data.put("user",user);
            data.put("code",SystemConfig.WECHAT_SUCCESS_CODE);
        }
        return ResponseUtil.ok(data);
    }

    @Override
    public Object wechatPhone(String code) {
        Map<String, Object> data = new HashMap<>();
        /**
         *1、获取token https://api.weixin.qq.com/cgi-bin/token
         * grant_type：client_credential
         * appid：
         * secret：
         * https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=SECRET
         */
        //String url = tokenUrl.replace("APPID",appid).replace("SECRET",secret);
        //String doGet = HttpURLConnectionUtils.doGet(url);
        /**
         * 获取手机号
         * https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=ACCESS_TOKEN
         * 返回给前端手机号
         * 同时在redis中加入固定的验证码
         */

        String tokenUrl = "https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code";
        tokenUrl = tokenUrl.replace("APPID",appid).replace("SECRET",secret).replace("JSCODE", code);
        String resultToken = HttpURLConnectionUtils.doGet(tokenUrl);
        JSONObject jsonObject = JSONObject.parseObject(resultToken);
        Object session_key = jsonObject.get("session_key");
        Object openid = jsonObject.get("openid");
        data.put("session_key",session_key);
        data.put("openid",openid);
        return ResponseUtil.ok(data);
    }
}
