package com.ruoyi.oauth.wx.controller;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.auth.common.domain.OauthUser;
import com.ruoyi.auth.common.service.IOauthUserService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.service.SysLoginService;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.framework.web.service.UserDetailsServiceImpl;
import com.ruoyi.oauth.wx.service.Impl.WxMiniAppLoginServiceImpl;
import com.ruoyi.oauth.wx.service.Impl.WxPubLoginServiceImpl;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/oauth/wx")
public class WxLoginController extends BaseController {

    @Autowired
    private IOauthUserService oauthUserService;

    @Autowired
    private WxMiniAppLoginServiceImpl wxMiniAppLoginServiceImpl;

    @Autowired
    private WxPubLoginServiceImpl wxAppLoginServiceImpl;
    @Autowired

    private ISysUserService sysUserService;


    @Autowired
    private UserDetailsServiceImpl userDetailsServiceImpl;

    @Autowired
    private TokenService tokenService;
    @Autowired
    private SysLoginService sysLoginService;

    @Anonymous
    @PostMapping("/login/{source}/{code}")
    public AjaxResult loginMiniApp(@PathVariable("source") String source, @PathVariable("code") String code) {
        String token = null;
        AjaxResult ajax = AjaxResult.success();
        if ("miniapp".equals(source) ) {
            token = wxMiniAppLoginServiceImpl.doLogin(code, true);
        } else if ("weixin".equals(source)) {
            token = wxAppLoginServiceImpl.doLogin(code, false);
        } else {
            return error("错误的登录方式");
        }
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }


    @Anonymous
    @PostMapping("/loginApp/{code}")
    public AjaxResult loginApp( @PathVariable("authResult") Object authResult) {
        String token = null;
        AjaxResult ajax = AjaxResult.success();

//            token = wxAppLoginServiceImpl.doLogin(code, false);


        ajax.put(Constants.TOKEN, token);
        return ajax;
    }


    @PostMapping("/register/{source}/{code}")
    public AjaxResult register(@PathVariable("source") String source, @PathVariable("code") String code) {
        OauthUser oauthUser = oauthUserService.selectOauthUserByUserId(getUserId());
        if (oauthUser != null) {
            return error("不可以重复绑定");
        } else {
            String msg = "";
            oauthUser = new OauthUser();
            oauthUser.setUserId(getUserId());
            oauthUser.setCode(code);
            if ("miniapp".equals(source)) {
                msg = wxMiniAppLoginServiceImpl.doRegister(oauthUser);
            } else if ("weixin".equals(source)) {
                msg = wxAppLoginServiceImpl.doRegister(oauthUser);
            } else {
                return error("错误的注册方式");
            }
            return StringUtils.isEmpty(msg) ? success() : error(msg);
        }
    }

}
