package com.jfinal.club._admin.login;

import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
import com.jfinal.club._admin.admin_interface.AdminSessionInterface;
import com.jfinal.club.common.controller.BaseController;
import com.jfinal.club.common.model.Admins;
import com.jfinal.club.util.CacheSessionUtil;
import com.jfinal.club.util.Cons;
import com.jfinal.club.util.SM4;
import com.jfinal.club.vo.UserSessionVo;
import com.jfinal.core.ActionKey;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.Ret;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;

import java.io.IOException;


/**
 * @descriptions: 登录控制
 * @author: lc
 * @date: 2020-05-09 20:02:53
 * @Version: 1.0
 **/
public class LoginAdminController extends BaseController {

    private CacheSessionUtil sessionUtil = CacheSessionUtil.getInstance( );

    /**
     * 登录界面
     */
    @Clear(AdminSessionInterface.class)
    public void index( ) {
        render("login.html");
    }

    /**
     * 后台登录 界面
     */
    @Clear(AdminSessionInterface.class)
    @Before(LoginValidator.class)
    public void login( ) {
        String username = get("username").toLowerCase( ).trim( );
        String password = get("password").trim( );

        Admins admins = Admins.dao.findFirst("select * from admins where username = ? and password = ?", username, password);
        if (admins != null) {
            saveSession(admins);
            renderJson(Ret.ok("msg", "登录成功!"));
        } else {
            renderJson(Ret.fail("msg", "用户名或密码输入有误!"));
        }
    }

    /**
     * 保存session
     * @param admins
     */
    private void saveSession(Admins admins){
        String sessionId = Cons.JSESSIONID_ADMIN+admins.getId();
        setCookie(Cons.JSESSIONID_ADMIN, sessionId, PropKit.getInt("cookie"), Cons.ContextPath+"/admin", null, true);
        UserSessionVo sessionVo = new UserSessionVo( );
        sessionVo.setAdmins(admins);
        sessionUtil.setSession(sessionId, sessionVo);
    }


    /**
     * 退出登录
     */
    @Clear(AdminSessionInterface.class)
    public void exit( ) {
        String sessionId = getCookie(Cons.JSESSIONID_ADMIN);
        //如果有session信息就清空
        if(StringUtils.isNotBlank(sessionId)){
            sessionUtil.delSession(sessionId);
        }
        index( );
    }

    @Clear(AdminSessionInterface.class)
    public void email() throws EmailException {
/*        Email email = new SimpleEmail();
        email.setHostName("smtp.qq.com");
        email.setSmtpPort(465);
        email.setAuthenticator(new DefaultAuthenticator("895296059@qq.com", "lc123..."));
        email.setSSL(true);
        email.setFrom("895296059@qq.com","lc123...","UTF-8");
        email.setSubject("TestMail");
        email.setMsg("This is a test mail ... :-)");
        email.addTo("lc895296059@163.com");
        email.send();
        System.out.println("发送成功");*/

        SimpleEmail simpleEmail = new SimpleEmail();
        simpleEmail.setHostName("smtp.qq.com");
        simpleEmail.setAuthentication("895296059@qq.com", "zjnourmdnappbdbb");
        simpleEmail.setSmtpPort(587);
        simpleEmail.setFrom("895296059@qq.com");
        simpleEmail.addTo("lc895296059@163.com");
        simpleEmail.setSubject("title");
        simpleEmail.setMsg("content");
        simpleEmail.send();

        System.out.println("发送邮件成功！");

    }

    public static void main(String[] args) throws IOException {
        SM4 sm4 = new SM4( );
        String decode = sm4.decode("xYnWFdVOHyVGhliDIIo0fQ==", "W5#");
        System.out.println(decode );
        
    }

}
