package thyyxxk.webserver.controller;

import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;
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.RestController;
import thyyxxk.webserver.config.auth.PassToken;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.settings.notification.SendMessageParam;
import thyyxxk.webserver.service.PublicServer;
import thyyxxk.webserver.service.redislike.RedisLikeService;
import thyyxxk.webserver.utils.ResultVoUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Slf4j
@RestController
public class ShutDownController implements ApplicationContextAware {

    private final PublicServer publicServer;

    private final RedisLikeService redisLikeService;

    private static Boolean serverIsUpdating = false;

    private final String LOCALHOST = "0:0:0:0:0:0:0:1";

    public ShutDownController(PublicServer publicServer, RedisLikeService redisLikeService) {
        this.publicServer = publicServer;
        this.redisLikeService = redisLikeService;
    }

    class Showdown extends Thread {
        @Override
        public void run() {
            try {
                Showdown.sleep(1000 * 10);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            ConfigurableApplicationContext ctx = (ConfigurableApplicationContext) context;
            ctx.close();
        }
    }

    private ApplicationContext context;

    @PostMapping("/shutDownContext")
    @PassToken
    public ResultVo<String> shutDownContext(HttpServletRequest request) {
        if (LOCALHOST.equals(request.getRemoteAddr())) {
            serverIsUpdating = true;
            new Showdown().start();
            return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_EL_MESSAGE);
        } else {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "无法调用");
        }
    }

    public static Boolean getServerIsUpdating() {
        return serverIsUpdating;
    }


    @Override
    public void setApplicationContext(@NotNull ApplicationContext applicationContext) throws BeansException {
        context = applicationContext;
    }

    @GetMapping("sendSystemUpdatesMessage")
    public ResultVo<String> sendSystemUpdatesMessage(String message) {
        redisLikeService.setMessage(RedisLikeService.SYSTEM_UPDATES_MESSAGE, message);
        publicServer.sendSystemAnnouncement();
        return ResultVoUtil.success();
    }

    @GetMapping("sendScrollingMessages")
    public ResultVo<String> sendScrollingMessages(String message) {
        redisLikeService.setMessage(RedisLikeService.SCROLLING_MESSAGES, message);
        publicServer.sendSystemAnnouncement();
        return ResultVoUtil.success();
    }

    @GetMapping("/getSystemAnnouncement")
    public ResultVo<Map<String, String>> getSystemAnnouncement() {
        return ResultVoUtil.success(redisLikeService.getMessage());
    }


    @PostMapping("/updateCompleteMessage")
    @PassToken
    public ResultVo<String> updateCompleteMessage(HttpServletRequest request, @RequestBody SendMessageParam param) {
        if (!LOCALHOST.equals(request.getRemoteAddr())) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "无法访问");
        }
        publicServer.sendMessage(param);
        return ResultVoUtil.success();
    }


}
