package cn.com.qmth.stmms.ms.accesscontrol.api;

import cn.com.qmth.stmms.ms.accesscontrol.config.LoginConfig;
import cn.com.qmth.stmms.ms.core.domain.user.MarkUser;
import cn.com.qmth.stmms.ms.core.domain.user.Role;
import cn.com.qmth.stmms.ms.core.repository.MarkUserRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Objects;

/**
 * Created by zhengmin on 2016/11/1.
 */
@RestController
@RequestMapping("/api")
public class AuthApi {

    @Autowired
    private LoginConfig loginConfig;

    @Autowired
    private MarkUserRepo markUserRepo;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public MarkUser login(MarkUser user, HttpServletRequest request) {
        MarkUser domain = null;


        if (loginConfig.adminLoginConfig().getLoginName().equals(user.getLoginName()) &&
                loginConfig.adminLoginConfig().getPassword().equals(user.getPassword())) {
            domain = new MarkUser(loginConfig.adminLoginConfig().getLoginName(), loginConfig.adminLoginConfig().getPassword(), null, null, "系统管理员", Role.SUPER_ADMIN, null);
        }
//        else if (loginConfig.clientLoginConfig().getLoginName().equals(user.getLoginName()) &&
//                loginConfig.clientLoginConfig().getPassword().equals(user.getPassword())) {
//            domain = new MarkUser(loginConfig.clientLoginConfig().getLoginName(), loginConfig.clientLoginConfig().getPassword(), null, null, "系统管理员", Role.ADMIN, null);
//        }
//        else if (loginConfig.inspectionLoginConfig().getLoginName().equals(user.getLoginName()) &&
//                loginConfig.inspectionLoginConfig().getPassword().equals(user.getPassword())) {
//            domain = new MarkUser(loginConfig.inspectionLoginConfig().getLoginName(), loginConfig.inspectionLoginConfig().getPassword(), null, null, "纪检员", Role.INSPECTION, null);
//        }
        else {
//            Work activeWork = workRepo.findByActiveTrue();
//            String loginName = activeWork.getId() + "-" + user.getLoginName();
//            domain = markUserRepo.findByLoginName(loginName);
            domain = markUserRepo.findByLoginName(user.getLoginName());
            if (domain == null) {
                if ((loginConfig.adminLoginConfig().getLoginName().equals(user.getLoginName()) && !loginConfig.adminLoginConfig().getPassword().equals(user.getPassword()))
//                        || (loginConfig.inspectionLoginConfig().getLoginName().equals(user.getLoginName()) && !loginConfig.inspectionLoginConfig().getPassword().equals(user.getPassword()))
                ) {
                    throw new RuntimeException("用户或密码错误");
                }
//                else if (loginConfig.clientLoginConfig().getLoginName().equals(user.getLoginName())) {
//                    throw new RuntimeException("该账户只能在采集端登录");
//                }
//                else if (loginConfig.uploadLoginConfig().getLoginName().equals(user.getLoginName())) {
//                    throw new RuntimeException("该账户只能在图片客户端登录");
//                }
                else {
                    throw new RuntimeException("用户不存在");
                }
            }
            if (Objects.equals(Role.COLLECTOR, domain.getRole())) {
                throw new RuntimeException("该账户只能在采集端登录");
            }
            if (!user.getPassword().equals(domain.getPassword())) {
                throw new RuntimeException("用户或密码错误");
            }
            if (!domain.isEnabled()) {
                throw new RuntimeException("用户被禁用");
            }
            domain.setSessionId(request.getSession().getId());
            markUserRepo.save(domain);
        }
        HttpSession session = request.getSession();
        String token = (String) request.getSession().getAttribute("token");
        if (!StringUtils.isEmpty(token)) {
            session.setAttribute("token", null);
        }
        //session过期时间
        session.setMaxInactiveInterval(-1);
        session.setAttribute("token", System.currentTimeMillis() + "#" + domain.getLoginName() + "#" + domain.getId());
        return domain;
    }

    @RequestMapping(value = "/logout", method = RequestMethod.DELETE)
    public void logout(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.setAttribute("token", null);
    }

    @RequestMapping(value = "/{domain}/password", method = RequestMethod.PATCH)
    public void pwd(@PathVariable MarkUser domain, @RequestBody MarkUser markUser) {
        if (markUser.getPassword() == null || markUser.getPassword().length() < 6) {
            throw new RuntimeException("密码长度不能小于6位");
        }
        domain.setPassword(markUser.getPassword());
        domain.setPwChangedCount(markUser.getPwChangedCount() + 1);
        markUserRepo.save(domain);
    }
}
