package com.ruoyi.web.controller.system;

import java.util.List;
import java.util.Set;

import com.ruoyi.common.core.domain.entity.tymh.wwToken.WwTokenResult;
import com.ruoyi.common.core.domain.entity.tymh.wwToken.WwUserLogin;
import com.ruoyi.common.core.domain.model.RegisterBody;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.service.OauthLoginHlwService;
import com.ruoyi.framework.web.service.OauthLoginService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysMenu;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginBody;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.service.SysLoginService;
import com.ruoyi.framework.web.service.SysPermissionService;
import com.ruoyi.system.service.ISysMenuService;


/**
 * 登录验证
 * 
 * @author ruoyi
 */
@RestController
public class SysLoginController
{
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;
    @Autowired
    private OauthLoginService oauthLoginService;
    @Autowired
    private OauthLoginHlwService oauthLoginHlwService;

    /**
     * 登录方法
     * 
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody)
    {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }
    @PostMapping("/loginoss")
    public AjaxResult loginoss(@RequestBody LoginBody loginBody)
    {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.loginsso(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),loginBody.getUuid());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }
    @PostMapping("/app/login")
    public AjaxResult loginApp(@RequestBody LoginBody loginBody)
    {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.loginApp("admin", "admin123");
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    /**
     * 微信小程序登录
     * @param loginBody
     * @return
     */
    @PostMapping("/app/appLogin")
    public AjaxResult appLogin(@RequestBody LoginBody loginBody)
    {
        AjaxResult ajax = AjaxResult.success();
        //ajax=loginService.appLogin(loginBody);
        ajax=loginService.appLoginNew(loginBody);
        return ajax;
    }

    /**
     * 一体机身份证登录
     * @param loginBody
     * @return
     */
    @PostMapping("/app/idCardLogin")
    public AjaxResult idCardLogin(@RequestBody LoginBody loginBody)
    {
        if(loginBody==null||StringUtils.isBlank(loginBody.getIdCard())){
            return AjaxResult.error("请输入有效的身份证号!");
        }
        AjaxResult ajax = AjaxResult.success();
        ajax=loginService.idCardLogin(loginBody);
        return ajax;
    }

    /**
     * 一体机手机号/密码登录
     * @param loginBody
     * @return
     */
    @PostMapping("/app/phoneLogin")
    public AjaxResult phoneLogin(@RequestBody LoginBody loginBody)
    {
        if(loginBody == null){
            return AjaxResult.error("登录参数不能为空!");
        }
        if(StringUtils.isBlank(loginBody.getUsername())){
            return AjaxResult.error("用户名为空,请输入用户名!");
        }
        if(StringUtils.isBlank(loginBody.getPassword())){
            return AjaxResult.error("密码为空,请输入密码!");
        }
        AjaxResult ajax = AjaxResult.success();
        ajax=loginService.phoneLogin(loginBody);
        return ajax;
    }

    /**
     * 获取用户信息
     * 
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public AjaxResult getInfo()
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        return ajax;
    }

    /**
     * 获取路由信息
     * 
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public AjaxResult getRouters()
    {
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
        return AjaxResult.success(menuService.buildMenus(menus));
    }
    @GetMapping("/sso/callback")
    public String ssoCallback(@RequestParam("ticket") String ticket) {
        String frontendIndexUrl = "http://domain.com";

        String ruoyiJwtToken = loginService.loginOss(ticket);

        String redirectUrl = frontendIndexUrl + "/index?token=" + ruoyiJwtToken;

        // 返回 "redirect:" 即可触发 302 重定向
        return "redirect:" + redirectUrl;
    }

    /**
     * 保存简历-重新返回token
     */
    @ApiOperation("保存注册信息")
    @PostMapping("/registerUser")
    public AjaxResult registerUser(@RequestBody RegisterBody registerBody)
    {
        String token=loginService.registerAppUser(registerBody);
        return AjaxResult.success().put("token",token);
    }

    /**
     * 获取统一门户token
     */
    @GetMapping("/getTjmhToken")
    public AjaxResult getTjmhToken(@RequestParam("code") String code){
        System.out.println("参数code==========================="+code);
        if(StringUtils.isBlank(code)){
            return AjaxResult.error("参数code为空，请传递code参数");
        }
        String token = oauthLoginService.oauthLogin(code);
        return AjaxResult.success("门户登录成功")
                .put("token", token)
                .put("accessUrl", "");
    }

    /**
     *互联网获取token
     * @param wwUserLogin
     * @return
     */
    @GetMapping("/getWwTjmhToken")
    public AjaxResult getWwTjmhToken(@RequestBody WwUserLogin wwUserLogin){
        if(wwUserLogin==null){
            return AjaxResult.error("参数wwUserLogin为空，请传递wwUserLogin实体参数");
        }
        String token = oauthLoginHlwService.getWwTjmhToken(wwUserLogin);
        return AjaxResult.success("门户登录成功")
                .put("token", token)
                .put("accessUrl", "");
    }

    /**
     *互联网获取token
     * @return
     */
    @GetMapping("/getWwTjmHlwToken")
    public AjaxResult getWwTjmHlwToken(@RequestParam("token") String token){
        System.out.println("互联网token================================"+token);
        if (StringUtils.isNotBlank(token)) {
            WwTokenResult wwTokenResult=new WwTokenResult();
            wwTokenResult.setAccessToken(token);
            String localToken = oauthLoginHlwService.getWwTjmHlwToken(wwTokenResult);
            return AjaxResult.success("门户登录成功")
                    .put("token", localToken)
                    .put("accessUrl", "");
        } else {
            return AjaxResult.error("token为空!");
        }
    }

}
