package edb_monitor

import (
	"encoding/json"
	"eta/eta_api/controllers"
	"eta/eta_api/models"
	"eta/eta_api/models/edb_monitor/request"
	edbmonitor "eta/eta_api/services/edb_monitor"
	"eta/eta_api/utils"
	"net/http"
	"strconv"
	"time"

	"github.com/gorilla/websocket"
)

type EdbMonitorMessageController struct {
	controllers.BaseAuthController
}

var upgrader = websocket.Upgrader{
	ReadBufferSize:  1024,
	WriteBufferSize: 1024,
	CheckOrigin: func(r *http.Request) bool {
		return true
	},
}

// GetMonitorLevel
// @Title 预警管理消息
// @Description 预警管理消息
// @Param   request body request.EdbMonitorSaveRequest  true  "每页数据条数"
// @Success 200 {object} models.EnglishReportEmailPageListResp
// @router /message/connect [get]
func (m *EdbMonitorMessageController) Connect() {
	br := new(models.BaseResponse).Init()
	defer func() {
		m.Data["json"] = br
		m.ServeJSON()
	}()
	sysUser := m.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录,SysUser Is Empty"
		br.Ret = 408
		return
	}

	var conn *websocket.Conn
	connKey := edbmonitor.EDB_MONITOR_MESSAGE_CONNECT_CACHE + strconv.Itoa(sysUser.AdminId)
	ok := utils.Rc.IsExist(connKey)
	if !ok {
		conn = edbmonitor.MonitorMessageConn[sysUser.AdminId]
		if conn != nil {
			conn.Close()
		}
	}
	err := utils.Rc.Put(connKey, "1", time.Minute*1)
	if err != nil {
		br.Msg = "系统错误"
		br.ErrMsg = "连接失败,err:" + err.Error()
		return
	}
	conn, err = upgrader.Upgrade(m.Ctx.ResponseWriter, m.Ctx.Request, nil)
	if err != nil {
		br.Msg = "连接失败"
		br.ErrMsg = "连接失败,err:" + err.Error()
		return
	}
	defer conn.Close()

	edbmonitor.MonitorMessageConn[sysUser.AdminId] = conn
	conn.SetCloseHandler(func(code int, text string) error {
		delete(edbmonitor.MonitorMessageConn, sysUser.AdminId)
		utils.Rc.Delete(connKey)
		return nil
	})

	go func() {
		// 心跳检测
		for {
			isClose, err := edbmonitor.EdbMonitorMessageHealth(sysUser.AdminId)
			if err != nil {
				utils.FileLog.Error("指标预警信息健康检查失败,err:%s, adminId:%d", err.Error(), sysUser.AdminId)
				return
			}
			if isClose {
				return
			}
		}
	}()

	messageList, err := edbmonitor.GetHistoryMessages(sysUser.AdminId)
	if err != nil {
		utils.FileLog.Error("获取指标预警信息历史失败,err:%s, adminId:%d", err.Error(), sysUser.AdminId)
	}
	success := make(chan int, 10)
	go func() {
		defer close(success)
		for i, msg := range messageList {
			if i == 0 {
				// 多条消息仅发送最新一条
				err = edbmonitor.SendMessages(sysUser.AdminId, msg.EdbInfoId, msg.EdbInfoType, msg.EdbClassifyId, msg.EdbUniqueCode, msg.Message, msg.TriggerTime)
				if err != nil {
					utils.FileLog.Error("指标预警信息发送失败,err:%s, adminId:%d", err.Error(), sysUser.AdminId)
				} else {
					success <- msg.EdbMonitorMessageId
				}
			} else {
				success <- msg.EdbMonitorMessageId
			}
		}
	}()
	go func() {
		readList := make([]int, 0)
		for {
			msgId, ok := <-success
			if !ok {
				break
			}
			readList = append(readList, msgId)
		}
		_, err = edbmonitor.ReadEdbMonitorMessageList(readList, sysUser.AdminId)
		if err != nil {
			utils.FileLog.Error("指标预警信息已读失败,err:%s, adminId:%d", err.Error(), sysUser.AdminId)
		}
	}()

	for {
		ok = utils.Rc.IsExist(connKey)
		if !ok {
			br.Msg = "连接已断开"
			br.Ret = 200
			br.Success = true
			return
		}
		time.Sleep(10 * time.Second)
	}
}

// Close
// @Title 预警管理消息
// @Description 预警管理消息
// @Param   request body request.EdbMonitorSaveRequest  true  "每页数据条数"
// @Success 200 {object} models.EnglishReportEmailPageListResp
// @router /message/close [post]
func (m *EdbMonitorMessageController) Close() {
	br := new(models.BaseResponse).Init()
	defer func() {
		m.Data["json"] = br
		m.ServeJSON()
	}()
	sysUser := m.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录,SysUser Is Empty"
		br.Ret = 408
		return
	}

	conn := edbmonitor.MonitorMessageConn[sysUser.AdminId]
	if conn != nil {
		conn.Close()
	}

	br.Msg = "关闭成功"
	br.Ret = 200
	br.Success = true
}

// List
// @Title 预警管理消息列表
// @Description 预警管理消息列表
// @Param   PageSize   query   int  true       "每页数据条数"
// @Param   CurrentIndex   query   int  true       "当前页页码,从1开始"
// @Success 200 {object} response.EdbMonitorClassifyTree
// @router /message/list [get]
func (c *EdbMonitorMessageController) List() {
	br := new(models.BaseResponse).Init()
	defer func() {
		c.Data["json"] = br
		c.ServeJSON()
	}()
	sysUser := c.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录,SysUser Is Empty"
		br.Ret = 408
		return
	}

	pageSize, _ := c.GetInt("PageSize")
	currentIndex, _ := c.GetInt("CurrentIndex")
	resp, err := edbmonitor.GetMessageList(sysUser.AdminId, currentIndex, pageSize)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = err.Error()
		return
	}

	br.Data = resp
	br.Msg = "获取成功"
	br.Ret = 200
	br.Success = true
}

// Read
// @Title 预警管理消息已读
// @Description 预警管理消息已读
// @Param   request body request.EdbMonitorSaveRequest  true  "每页数据条数"
// @Success 200 {object} models.EnglishReportEmailPageListResp
// @router /message/read [post]
func (m *EdbMonitorMessageController) Read() {
	br := new(models.BaseResponse).Init()
	defer func() {
		m.Data["json"] = br
		m.ServeJSON()
	}()

	sysUser := m.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录,SysUser Is Empty"
		br.Ret = 408
		return
	}
	var req request.EdbMonitorMessageReadReq
	if err := json.Unmarshal(m.Ctx.Input.RequestBody, &req); err != nil {
		br.Msg = "参数错误"
		br.ErrMsg = "参数错误,err:" + err.Error()
		return
	}
	if req.EdbMonitorMessageId <= 0 {
		br.Msg = "参数错误"
		return
	}

	msg, err := edbmonitor.ReadEdbMonitorMessage(req.EdbMonitorMessageId, sysUser.AdminId)
	if err != nil {
		if msg == "" {
			msg = "系统错误"
		}
		br.Msg = msg
		br.ErrMsg = "读取消息失败,err:" + err.Error()
		return
	}

	br.Msg = "已读成功"
	br.Ret = 200
	br.Success = true
}