package com.izouma.awesomeadmin.web.admin;

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

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

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.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.izouma.awesomeadmin.constant.AppConstant;
import com.izouma.awesomeadmin.dto.Page;
import com.izouma.awesomeadmin.dto.Result;
import com.izouma.awesomeadmin.model.SuperUser;
import com.izouma.awesomeadmin.service.SuperUserService;
import com.izouma.awesomeadmin.util.CookieUtil;

@Controller
@RequestMapping("/super")
//url：/模块/资源/{id}/细分
public class SuperUserController {

    @Autowired
    private SuperUserService superUserService;


    @RequestMapping(value = "/getSuperUser", method = RequestMethod.GET)
    @ResponseBody
    public Result getSuperUser(@RequestParam(required = false, value = "userId") String userId) {

        SuperUser superUser = superUserService.getSuperUserById(userId);
        return new Result(true, superUser);

    }


    @RequestMapping(value = "/getSuperUserList", method = RequestMethod.GET)
    @ResponseBody
    public Result getSuperUserList() {

        Map<String, List<SuperUser>> result = new HashMap<>();
        List<SuperUser> superUserList = superUserService.getSuperUserList();
        result.put("superUserList", superUserList);
        return new Result(true, result);

    }


    @RequestMapping(value = "/all", method = RequestMethod.GET)
    @ResponseBody
    public List<SuperUser> getAllSuperUser() {

        List<SuperUser> superUserList = superUserService.getSuperUserList();

        return superUserList;
    }


    @RequestMapping(value = "/getSuperUserPage", method = RequestMethod.GET)
    @ResponseBody
    public Result getSuperUserPage(Page page) {

        // 创建分页对象

        Map<String, Object> result = new HashMap<>();
        List<SuperUser> superUserPage = superUserService.getSuperUserByPage(page);
        result.put(AppConstant.PAGE, page);
        result.put("superUserPage", superUserPage);
        return new Result(true, result);

    }


    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public Result updateSuperUser(HttpServletRequest request) {

        boolean isSuccess;

        isSuccess = superUserService.updateSuperUserInfo(request);

        if (isSuccess) {
            return new Result(true, "更新成功");
        } else {
            return new Result(false, "更新失败");
        }
    }


    @RequestMapping(value = "/updateUser", method = RequestMethod.POST)
    @ResponseBody
    public Result updateUser(SuperUser superUser) {

        boolean isSuccess;

        isSuccess = superUserService.updateUser(superUser);

        if (isSuccess) {
            return new Result(true, "更新成功");
        } else {
            return new Result(false, "更新失败");
        }
    }


    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public Result saveUserInfo(SuperUser superUser) {

        boolean num = superUserService.createUser(superUser);
        if (num) {
            return new Result(true, "保存成功");
        }
        return new Result(false, "保存异常");

    }


    /**
     * <p>删除用户。</p>
     *
     * @param productId
     * @return
     */
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    @ResponseBody
    public Result deleteUserInfoById(@RequestParam(required = true, value = "userId") String userId) {

        boolean num = superUserService.deleteUser(userId);
        if (num) {
            return new Result(true, "删除成功");
        }
        return new Result(false, "删除异常");

    }


    @ResponseBody
    @RequestMapping(value = "login", method = RequestMethod.POST)
    public Result Login(HttpServletRequest request, HttpServletResponse response, String userName, String password, boolean remember, String code) {

        if (null == userName || "".equals(userName)) {
            return new Result(false, "用户名或密码错误");
        }
        String authCode = (String) request.getSession(true).getAttribute("rand");
        if (!code.toLowerCase().equals(authCode)) {
            return new Result(false, "验证码错误");
        }
        SuperUser user = superUserService.UserLogin(userName, password);

        if (null != user) {

            String userId = user.getUserId() + "";

            request.getSession(true).setAttribute("userId", userId);
            request.getSession(true).setAttribute("superUserName", userName);

            if (remember) {

                int maxAge = 7 * 24 * 60 * 60;
                CookieUtil.addCookie(response, "userId", userId, maxAge);
                CookieUtil.addCookie(response, "superUserName", userName, maxAge);

                if (request.getParameter("remember") != null) {
                    if (request.getParameter("remember").equals("true")) {
                        HttpSession session = request.getSession();
                        CookieUtil.addCookie(response, "JSESSIONID", session.getId(), 7 * 24 * 60 * 60);
                    }
                }
            }
            return new Result(true, "登陆成功");
        } else {
            return new Result(false, "用户名或密码错误");
        }

    }


    @ResponseBody
    @RequestMapping(value = "logout", method = RequestMethod.POST)
    public Result logout(HttpServletRequest request, HttpServletResponse response) {

        //cookie生命周期设置为20小时
        int maxAge = -1;
        CookieUtil.addCookie(response, "userId", "-1", maxAge);
        CookieUtil.addCookie(response, "superUserName", "-1", maxAge);

        request.getSession().invalidate();
        //设置登陆状态
        return new Result(true, "退出成功");

    }

}
