package com.dgtis.dorm.manager.admin.web;

import com.dgtis.dorm.manager.admin.annotation.LoginAdmin;
import com.dgtis.dorm.manager.admin.dao.AdminToken;
import com.dgtis.dorm.manager.admin.service.AdminTokenManager;
import com.dgtis.dorm.manager.admin.util.bcrypt.BCryptPasswordEncoder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.dgtis.dorm.manager.db.domain.Admin;
import com.dgtis.dorm.manager.db.service.AdminService;
import com.dgtis.dorm.manager.db.util.JacksonUtil;
import com.dgtis.dorm.manager.db.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/admin/login")
public class AuthController {
    private final Log logger = LogFactory.getLog(AuthController.class);

    @Autowired
    private AdminService adminService;

    /*
     *  { username : value, password : value }
     */
    @PostMapping("/login")
    public Object login(@RequestBody String body) {
        String username = JacksonUtil.parseString(body, "username");
        //String password = JacksonUtil.parseString(body, "password");

        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(JacksonUtil.parseString(body, "password"))) {
            return ResponseUtil.badArgument();
        }

        List<Admin> adminList = adminService.findAdmin(username);
        Assert.state(adminList.size() < 2, "同一个用户名存在两个账户");
        if (adminList.size() == 0) {
            return ResponseUtil.badArgumentValue();
        }
        Admin admin = adminList.get(0);

        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        if (!encoder.matches(JacksonUtil.parseString(body, "password"), admin.getPassword())) {
            return ResponseUtil.fail(403, "账号密码不对");
        }

        Integer adminId = admin.getId();
        // token
        AdminToken adminToken = AdminTokenManager.generateToken(adminId);

        return ResponseUtil.ok(adminToken.getToken());
    }

    /**
     * @descption: 根据工号查询用户信息
     * @param: [body]
     * @return: java.lang.Object
     * @auther: qxm
     * @date: 2019/6/5 17:21
     */
    @PostMapping("/loginByEno")
    public Object loginByEno(@RequestBody String body) {
        String eno = JacksonUtil.parseString(body, "employeNo");

        List<Admin> adminList = adminService.findByEno(eno);
        Admin admin = null;
        if (adminList.size() > 0) {
            admin = adminList.get(0);
        }

        Integer adminId = admin.getId();
        // token
        AdminToken adminToken = AdminTokenManager.generateToken(adminId);

        return ResponseUtil.ok(adminToken.getToken());
    }

    /*
     *
     */
    @PostMapping("/logout")
    public Object login(@LoginAdmin Integer adminId) {
        if (adminId == null) {
            return ResponseUtil.unlogin();
        }

        return ResponseUtil.ok();
    }
}
