package com.icloud.server.web.shiro.controller;

import com.icloud.server.scala.shiro.User;
import com.icloud.server.scala.shiro.UserService;
import com.icloud.server.web.json.JsonResult;
import com.icloud.server.web.shiro.entity.UserQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Api( tags = {"登陆,注册"} , value = "API" )
@RestController
@RequestMapping("pub")
@CrossOrigin
public class PublicController {


    @Autowired
    private UserService us;

    @PostMapping("need_login")
    public JsonResult needLogin() {
        JsonResult jr = new JsonResult("温馨提示：请使用对应的账号登录,-2");
        jr.setState(-2);
        return jr;

    }


    @PostMapping("not_permit")
    public JsonResult notPermit() {

        return new JsonResult("温馨提示：拒绝访问，没权限,-3");
    }

    @ApiOperation( value = "index", notes = "",hidden = true )
    @PostMapping("index")
    public JsonResult index() {

        List<String> videoList = new ArrayList<>();


        return new JsonResult(videoList);

    }


    /**
     * 登录接口
     *
     * @param userQuery
     * @param request
     * @param response
     * @return
     */
    @PostMapping("login")
    public JsonResult login(@RequestBody UserQuery userQuery, HttpServletRequest request, HttpServletResponse response) {

        Subject subject = SecurityUtils.getSubject();
        Map<String, Object> info = new HashMap<>();
        try {
            UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(userQuery.getName(), userQuery.getPassword());

            subject.login(usernamePasswordToken);

            info.put("msg", "登录成功");
            info.put("uid", us.getid(userQuery.getName()).getId());
            info.put("session_id", subject.getSession().getId());


            return new JsonResult(info);

        } catch (Exception e) {
            e.printStackTrace();
            JsonResult jr = new JsonResult("账号或者密码错误");
            jr.setState(-2);
            return jr;

        }


    }

    @PostMapping("login1")
    public JsonResult login1(String name, String pwd, String token) {

        Subject subject = SecurityUtils.getSubject();
        Map<String, Object> info = new HashMap<>();
        try {
            UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(name, pwd);
            subject.login(usernamePasswordToken);

            info.put("msg", "登录成功");
            info.put("session_id", subject.getSession().getId());

            info.put("id", us.getid(name).getId());
            boolean b = false;
            String nick = "";
            User user = us.getid(name);

                nick = user.getUsername();

            System.out.println("nick:"+nick);
            info.put("nick", nick);
            info.put("checked", b);
            System.out.println("token:"+token);

            return new JsonResult(info);

        } catch (Exception e) {
            e.printStackTrace();
            e.printStackTrace();
            JsonResult jr = new JsonResult("账号或者密码错误");
            jr.setState(-2);
            return jr;

        }


    }
    @PostMapping("register")
    public JsonResult register(String name, String pwd, String token) {
        if(token=="1"){

        }
        Subject subject = SecurityUtils.getSubject();
        Map<String, Object> info = new HashMap<>();
        try {
            UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(name, pwd);
            subject.login(usernamePasswordToken);

            info.put("msg", "登录成功");
            info.put("session_id", subject.getSession().getId());

            info.put("id", us.getid(name).getId());
            boolean b = false;
            String nick = "";
            User user = us.getid(name);

            nick = user.getUsername();

            System.out.println("nick:"+nick);
            info.put("nick", nick);
            info.put("checked", b);
            System.out.println("token:"+token);

            return new JsonResult(info);

        } catch (Exception e) {
            e.printStackTrace();
            e.printStackTrace();
            JsonResult jr = new JsonResult("账号或者密码错误");
            jr.setState(-2);
            return jr;

        }


    }

}
