package com.jay.monitor.data.server.controllers;

import com.alibaba.fastjson.JSONObject;
import com.cat.file.message.MessageManagerProcess;
import com.jay.monitor.data.core.model.response.ResponseCommonModel;
import com.jay.monitor.data.core.utils.ResponseUtils;
import com.jay.monitor.data.server.config.props.JayServerProperties;
import com.jay.monitor.data.server.costs.ServerConstants;
import com.jay.monitor.data.server.enums.CommonEnums;
import com.jay.monitor.data.server.models.request.login.LoginRequest;
import com.jay.monitor.data.server.utils.CookiesUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

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

/**
 * @author ： liukx
 * @describe ：首页
 * @time ： 2019/3/1 - 16:31
 */
@Controller
@RequestMapping(value = "/")
public class IndexController {
    final static private String page = "/";
    @Autowired
    private JayServerProperties serverProperties;

    @Autowired
    private MessageManagerProcess messageManagerProcess;

    @RequestMapping(value = "/index.html", method = RequestMethod.GET)
    public String index() throws Exception {
        return page + "index";
    }

    @RequestMapping(value = "/login.html", method = RequestMethod.GET)
    public String login() throws Exception {
        return page + "login";
    }

    @RequestMapping(value = "/loginOut.html", method = RequestMethod.GET)
    public String loginOut(HttpServletRequest request, HttpServletResponse response) throws Exception {
        CookiesUtil.deleteCookieByName(request, response, ServerConstants.USER_SESSION_KEY);
        return "redirect:/login.html";
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public ResponseCommonModel loginService(HttpServletResponse response, @RequestBody LoginRequest loginRequest)
        throws Exception {
        String username = serverProperties.getUsername();
        String password = serverProperties.getPassword();
        String token = serverProperties.getToken();

        String un = loginRequest.getUsername();
        String pwd = loginRequest.getPassword();

        if (username.equals(un) && password.equals(pwd)) {
            CookiesUtil.setCookie(response, ServerConstants.USER_SESSION_KEY, token, -1);
            return ResponseUtils.trues();
        } else {
            return ResponseUtils.falses(CommonEnums.LOGIN_ERROR);
        }

    }

    @RequestMapping(value = "/test", method = {RequestMethod.DELETE,
        RequestMethod.PUT}, produces = "application/json; charset=utf-8")
    @ResponseBody
    public JSONObject test(@RequestBody JSONObject jsonObject) throws Exception {
        return jsonObject;
    }

    @RequestMapping(value = "/getLogId", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    @ResponseBody
    public String getLogId(@RequestParam("id") String logId) throws Exception {
        return messageManagerProcess.getMessage(logId);
    }

    @RequestMapping(value = "/storeDisk", method = RequestMethod.GET)
    @ResponseBody
    public String storeDisk() throws Exception {
        messageManagerProcess.closeAll();
        return "OK";
    }
}
