package com.example.myshiro.controller;

import com.example.myshiro.domain.Users;
import com.example.myshiro.service.UsersService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.realm.jdbc.JdbcRealm;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
//import javax.naming.AuthenticationException;
import javax.sql.DataSource;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.ConcurrentAccessException;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.authc.ExpiredCredentialsException;
import org.apache.shiro.authc.ExcessiveAttemptsException;

@Controller
public class LoginController {
    @Autowired
    private UsersService usersService;
    @Autowired
    private DataSource dataSource;

    @RequestMapping("/user/login")
    public String login(String username, String password) {
        // 1.获取SecurityManager工厂，读取配置文件
        IniSecurityManagerFactory factory = new IniSecurityManagerFactory("classpath:shiro.ini");
        // 2.获取SecurityManager对象
        SecurityManager securityManager = factory.getInstance();
        // 3.将SecurityManager对象设置到运行环境中
        SecurityUtils.setSecurityManager(securityManager);
        // 4.获取Subject对象
        Subject subject = SecurityUtils.getSubject();
        // 5.将前端传来的用户名密码封装为Shiro提供的身份对象
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        try {
            // 6.shiro认证
            subject.login(token);
            // 7.认证通过跳转到主页面
            return "main";
        } catch (AuthenticationException e) {
            // 8.认证不通过跳转到失败页面
            return "fail";
        }
    }

    @RequestMapping("/user/login2")
    public String login2(String username, String password) {
        // 1.获取SecurityManager对象
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        // 2.为SecurityManager对象设置Realm
        JdbcRealm jdbcRealm = new JdbcRealm();
        jdbcRealm.setDataSource(dataSource);
        securityManager.setRealm(jdbcRealm);
        // 3.将SecurityManager对象设置到运行环境中
        SecurityUtils.setSecurityManager(securityManager);
        // 4.获取Subject对象
        Subject subject = SecurityUtils.getSubject();
        // 5.将前端传来的用户名密码封装为Shiro提供的身份对象
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        try {
            // 6.shiro认证
            subject.login(token);
            // 7.认证通过跳转到主页面
            return "main";
        } catch (AuthenticationException e) {
            // 8.认证不通过跳转到失败页面
            return "fail";
        }
    }

    @RequestMapping("/user/login3")
    public String login3(String username, String password) {
        try {
            // 6.shiro认证
            usersService.usersLogin(username, password);
            // 7.认证通过跳转到主页面
            return "main";
        } catch ( AuthenticationException e) {
            // 8.认证不通过跳转到失败页面
            return "fail";
        }
    }

    @RequestMapping("/user/login4")
    public String login4(String username, String password) {
        try {
            usersService.usersLogin(username, password);
            return "main";
        } catch (DisabledAccountException e) {
            System.out.println("账户失效");
            return "fail";
        } catch (ConcurrentAccessException e) {
            System.out.println("竞争次数过多");
            return "fail";
        } catch (ExcessiveAttemptsException e) {
            System.out.println("尝试次数过多");
            return "fail";
        } catch (UnknownAccountException e) {
            System.out.println("用户名不正确");
            return "fail";
        } catch (IncorrectCredentialsException e) {
            System.out.println("密码不正确");
            return "fail";
        } catch (ExpiredCredentialsException e) {
            System.out.println("凭证过期");
            return "fail";
//    } catch (AuthenticationException e) {
//        return "fail";
        }
    }

//    @RequestMapping("/user/login2")
//    public String login2(String username,String password,String rememberMe){
//        try {
//            usersService.userLogin(username,password,rememberMe);
//            return "main";
//        }catch (DisabledAccountException e){
//            System.out.println("账户失效");
//            return "fail";
//        }catch (ConcurrentAccessException e){
//            System.out.println("竞争次数过多");
//            return "fail";
//        }catch (ExcessiveAttemptsException e){
//            System.out.println("尝试次数过多");
//            return "fail";
//        }catch (UnknownAccountException e){
//            System.out.println("用户名不正确");
//            return "fail";
//        }catch (IncorrectCredentialsException e){
//            System.out.println("密码不正确");
//            return "fail";
//        }catch (ExpiredCredentialsException e){
//            System.out.println("凭证过期");
//            return "fail";
//        }
//    }
//

//@RequestMapping("/user/getUsername")
//@ResponseBody
//public String getUsername(){
//    Subject subject = SecurityUtils.getSubject();
//    Users users = (Users) subject.getPrincipal();
//    return users.getUsername();
//}
//
//    // 退出登录
//    @RequestMapping("/user/logout")
//    public String logout(){
//        Subject subject = SecurityUtils.getSubject();
//        subject.logout();
//        // 退出后重定向到登录页
//        return "redirect:/login";
//    }
//
//    // 支付
//    @RequestMapping("/user/pay")
//    @ResponseBody
//    public String pay(){
//        return "支付功能";
//    }
}
