package com.yzt.airconditionersystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sun.org.apache.bcel.internal.generic.LMUL;
import com.yzt.airconditionersystem.bean.*;
import com.yzt.airconditionersystem.enums.CompanyTypeEnum;
import com.yzt.airconditionersystem.result.Result;
import com.yzt.airconditionersystem.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @author TzZach
 * @since 2022-04-09 12:38
 */
@Controller
public class LoginController {

    @Autowired
    private UserService userService;
    @Autowired
    private PermissionService permissionService;
    @Autowired
    private RolePermissionService rolePermissionService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private CompanyService companyService;

    @GetMapping(value = {"/login", "/login.html"})
    public String login() {
        return "login";
    }

    @PostMapping("/login")
    @ResponseBody
    public Result login(@RequestBody User loginUser, HttpSession session) {
        session.setAttribute("loginUser", loginUser);

        User user = userService.getOneByUsernameAndPassword(loginUser);

        if(user != null) {
            if (loginUser.getUsername().equals(user.getUsername())
                    && loginUser.getPassword().equals(user.getPassword())) {
                List<Permission> permissions = getPermissionFromUserRoleId(user);
                userInfo(user);
                user.setPassword(null);
                session.setAttribute("loginUser", user);
                session.setAttribute("permissions", permissions);
                return Result.success(permissions);
            }
        }

        return Result.error("账户名或密码错误");
    }

    /**
     * 给用户加入角色名称和公司名称
     * @param user
     * @return
     */
    private void userInfo(User user) {
        Role role = roleService.getById(user.getRoleId());
        user.setRoleName(role.getName());
        if (user.getCompanyId().equals(String.valueOf(CompanyTypeEnum.HEADQUARTERS.getId()))) {
            user.setCompanyName("总部");
        } else {
            String name = companyService.getById(user.getCompanyId()).getName();
            user.setCompanyName(name);
        }
    }

    private List<Permission> getPermissionFromUserRoleId(User user) {
        LambdaQueryWrapper<RolePermission> rolePermissionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        rolePermissionLambdaQueryWrapper.eq(RolePermission::getRoleId, user.getRoleId());
        List<RolePermission> rolePermissionList = rolePermissionService.list(rolePermissionLambdaQueryWrapper);
        LambdaQueryWrapper<Permission> permissionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<Integer> permissionIds = new ArrayList<>();
        for (RolePermission rolePermission : rolePermissionList) {
            Integer permissionId = rolePermission.getPermissionId();
            permissionIds.add(permissionId);
        }
        permissionLambdaQueryWrapper.in(Permission::getId, permissionIds);
        List<Permission> permissionList = permissionService.list(permissionLambdaQueryWrapper);
        return permissionList;
    }


    @GetMapping("/index")
    public String index() {
        return "index";
    }

    @GetMapping("/logout")
    public String logout(HttpSession session) {
        session.removeAttribute("loginUser");
        session.removeAttribute("permissions");
        return "redirect:/login";
    }


}
