package controller

import (
	"fmt"
	"strings"

	"be.ems/src/framework/database/redis"
	"be.ems/src/framework/i18n"
	"be.ems/src/framework/reqctx"
	"be.ems/src/framework/resp"
	"be.ems/src/framework/ssh"
	"be.ems/src/framework/telnet"
	"be.ems/src/framework/utils/parse"
	"be.ems/src/modules/ne/model"
	neService "be.ems/src/modules/ne/service"

	"github.com/gin-gonic/gin"
)

// 实例化控制层 NeHostController 结构体
var NewNeHost = &NeHostController{
	neHostService: neService.NewNeHost,
}

// 网元主机连接请求
//
// PATH /host
type NeHostController struct {
	neHostService *neService.NeHost // 网元主机连接服务
}

// 网元主机列表
//
// GET /list
func (s NeHostController) List(c *gin.Context) {
	query := reqctx.QueryMap(c)
	rows, total := s.neHostService.FindByPage(query)

	arr := &rows
	for i := range *arr {
		(*arr)[i].Password = "-"
		(*arr)[i].PrivateKey = "-"
		(*arr)[i].PassPhrase = "-"
	}

	c.JSON(200, resp.OkData(map[string]any{"total": total, "rows": rows}))
}

// 网元主机信息
//
// GET /:id
func (s NeHostController) Info(c *gin.Context) {
	language := reqctx.AcceptLanguage(c)
	id := parse.Number(c.Param("id"))
	if id <= 0 {
		c.JSON(400, resp.CodeMsg(40010, "bind err: id is empty"))
		return
	}

	neHost := s.neHostService.FindById(id)
	if neHost.ID != id {
		// 没有可访问主机信息数据！
		c.JSON(200, resp.ErrMsg(i18n.TKey(language, "neHost.noData")))
		return
	}

	c.JSON(200, resp.OkData(neHost))
}

// 网元主机新增
//
// POST /
func (s NeHostController) Add(c *gin.Context) {
	language := reqctx.AcceptLanguage(c)
	var body model.NeHost
	if err := c.ShouldBindBodyWithJSON(&body); err != nil {
		errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
		c.JSON(422, resp.CodeMsg(resp.CODE_PARAM_PARSER, errMsgs))
		return
	}
	if body.ID != 0 {
		c.JSON(422, resp.CodeMsg(resp.CODE_PARAM_CHEACK, "bind err: id not is empty"))
		return
	}

	if body.GroupID == "1" {
		// 主机信息操作【%s】失败，禁止操作网元
		msg := i18n.TKey(language, "neHost.banNE")
		c.JSON(200, resp.ErrMsg(msg))
		return
	}

	// 检查属性值唯一
	uniqueHost := s.neHostService.CheckUniqueHostTitle(body.GroupID, body.Title, body.HostType, 0)
	if !uniqueHost {
		// 主机信息操作【%s】失败，同组内名称已存在
		msg := i18n.TTemplate(language, "neHost.errKeyExists", map[string]any{"name": body.Title})
		c.JSON(200, resp.ErrMsg(msg))
		return
	}

	body.CreateBy = reqctx.LoginUserToUserName(c)
	insertId := s.neHostService.Insert(body)
	if insertId > 0 {
		c.JSON(200, resp.Ok(nil))
		return
	}
	c.JSON(200, resp.Err(nil))
}

// 网元主机修改
//
// PUT /
func (s NeHostController) Edit(c *gin.Context) {
	language := reqctx.AcceptLanguage(c)
	var body model.NeHost
	if err := c.ShouldBindBodyWithJSON(&body); err != nil {
		errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
		c.JSON(422, resp.CodeMsg(resp.CODE_PARAM_PARSER, errMsgs))
		return
	}
	if body.ID <= 0 {
		c.JSON(422, resp.CodeMsg(resp.CODE_PARAM_CHEACK, "bind err: id is empty"))
		return
	}

	// 检查属性值唯一
	uniqueHost := s.neHostService.CheckUniqueHostTitle(body.GroupID, body.Title, body.HostType, body.ID)
	if !uniqueHost {
		// 主机信息操作【%s】失败，同组内名称已存在
		msg := i18n.TTemplate(language, "neHost.errKeyExists", map[string]any{"name": body.Title})
		c.JSON(200, resp.ErrMsg(msg))
		return
	}

	// 检查是否存在
	neHost := s.neHostService.FindById(body.ID)
	if neHost.ID != body.ID {
		// 没有可访问主机信息数据！
		c.JSON(200, resp.ErrMsg(i18n.TKey(language, "neHost.noData")))
		return
	}

	body.UpdateBy = reqctx.LoginUserToUserName(c)
	rows := s.neHostService.Update(body)
	if rows > 0 {
		c.JSON(200, resp.Ok(nil))
		return
	}
	c.JSON(200, resp.Err(nil))
}

// 网元主机删除
//
// DELETE /:id
func (s NeHostController) Remove(c *gin.Context) {
	language := reqctx.AcceptLanguage(c)
	id := c.Param("id")
	if id == "" {
		c.JSON(400, resp.CodeMsg(40010, "bind err: id is empty"))
		return
	}

	// 处理字符转id数组后去重
	uniqueIDs := parse.RemoveDuplicatesToArray(id, ",")
	// 转换成int64数组类型
	ids := make([]int64, 0)
	for _, v := range uniqueIDs {
		ids = append(ids, parse.Number(v))
	}

	rows, err := s.neHostService.DeleteByIds(ids, true)
	if err != nil {
		c.JSON(200, resp.ErrMsg(i18n.TKey(language, err.Error())))
		return
	}
	msg := i18n.TTemplate(language, "app.common.deleteSuccess", map[string]any{"num": rows})
	c.JSON(200, resp.OkMsg(msg))
}

// 网元主机测试连接
//
// POST /test
//
//	@Tags			ne
//	@Accept			json
//	@Produce		json
//	@Param			data	body		object	true	"Request Param"
//	@Success		200		{object}	object	"Response Results"
//	@Security		TokenAuth
//	@Summary		Network element host test connection
//	@Description	Network element host test connection
//	@Router			/ne/host/test [post]
func (s NeHostController) Test(c *gin.Context) {
	language := reqctx.AcceptLanguage(c)
	var body model.NeHost
	if err := c.ShouldBindBodyWithJSON(&body); err != nil {
		errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
		c.JSON(422, resp.CodeMsg(resp.CODE_PARAM_PARSER, errMsgs))
		return
	}

	if body.HostType == "ssh" {
		var connSSH ssh.ConnSSH
		body.CopyTo(&connSSH)
		var client *ssh.ConnSSH
		var err error
		if body.AuthMode == "2" {
			client, err = connSSH.NewClientByLocalPrivate()
		} else {
			client, err = connSSH.NewClient()
		}
		if err != nil {
			// 连接主机失败，请检查连接参数后重试
			c.JSON(200, resp.ErrMsg(i18n.TKey(language, "neHost.errByHostInfo")))
			return
		}
		defer client.Close()
		c.JSON(200, resp.Ok(nil))
		return
	}

	if body.HostType == "telnet" {
		var connTelnet telnet.ConnTelnet
		body.CopyTo(&connTelnet)

		client, err := connTelnet.NewClient()
		if err != nil {
			// 连接主机失败，请检查连接参数后重试
			c.JSON(200, resp.ErrMsg(i18n.TKey(language, "neHost.errByHostInfo")))
			return
		}
		defer client.Close()
		// 是否有终止符
		if strings.HasSuffix(client.LastResult, ">") || strings.HasSuffix(client.LastResult, "> ") || strings.HasSuffix(client.LastResult, "# ") {
			c.JSON(200, resp.Ok(nil))
		} else {
			// 连接主机失败，请检查连接参数后重试
			c.JSON(200, resp.ErrMsg(i18n.TKey(language, "neHost.errByHostInfo")))
		}
		return
	}

	if body.HostType == "redis" {
		var connRedis redis.ConnRedis
		body.CopyTo(&connRedis)

		client, err := connRedis.NewClient()
		if err != nil {
			// 连接主机失败，请检查连接参数后重试
			c.JSON(200, resp.ErrMsg(i18n.TKey(language, "neHost.errByHostInfo")))
			return
		}
		defer client.Close()
		c.JSON(200, resp.Ok(nil))
		return
	}
}

// 网元主机发送命令
//
// POST /cmd
//
//	@Tags			ne
//	@Accept			json
//	@Produce		json
//	@Param			data	body		object	true	"Request Param"
//	@Success		200		{object}	object	"Response Results"
//	@Security		TokenAuth
//	@Summary		The network element host sends the command
//	@Description	The network element host sends the command
//	@Router			/ne/host/cmd [post]
func (s NeHostController) Cmd(c *gin.Context) {
	language := reqctx.AcceptLanguage(c)
	var body struct {
		ID  int64  `json:"id" binding:"required"`  // 主机ID
		Cmd string `json:"cmd" binding:"required"` // 执行命令
	}
	if err := c.ShouldBindBodyWithJSON(&body); err != nil {
		errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
		c.JSON(422, resp.CodeMsg(resp.CODE_PARAM_PARSER, errMsgs))
		return
	}

	// 检查是否存在
	neHost := s.neHostService.FindById(body.ID)
	if neHost.ID != body.ID {
		// 没有可访问主机信息数据！
		c.JSON(200, resp.ErrMsg(i18n.TKey(language, "neHost.noData")))
		return
	}

	if neHost.HostType == "ssh" {
		var connSSH ssh.ConnSSH
		neHost.CopyTo(&connSSH)
		var client *ssh.ConnSSH
		var err error
		if neHost.AuthMode == "2" {
			client, err = connSSH.NewClientByLocalPrivate()
		} else {
			client, err = connSSH.NewClient()
		}
		if err != nil {
			// 连接主机失败，请检查连接参数后重试
			c.JSON(200, resp.ErrMsg(i18n.TKey(language, "neHost.errByHostInfo")))
			return
		}
		defer client.Close()

		// 执行命令
		output, err := client.RunCMD(body.Cmd)
		if err != nil {
			c.JSON(200, resp.ErrMsg(err.Error()))
			return
		}
		c.JSON(200, resp.OkData(output))
		return
	}

	if neHost.HostType == "telnet" {
		var connTelnet telnet.ConnTelnet
		neHost.CopyTo(&connTelnet)

		client, err := connTelnet.NewClient()
		if err != nil {
			// 连接主机失败，请检查连接参数后重试
			c.JSON(200, resp.ErrMsg(i18n.TKey(language, "neHost.errByHostInfo")))
			return
		}
		defer client.Close()

		// 执行命令
		output, err := client.RunCMD(body.Cmd)
		if err != nil {
			c.JSON(200, resp.ErrMsg(err.Error()))
			return
		}
		c.JSON(200, resp.OkData(output))
		return
	}
}

// 网元主机SSH方式检查服务器环境
//
// POST /checkBySSH
//
//	@Tags			ne
//	@Accept			json
//	@Produce		json
//	@Param			data	body		object	true	"Request Param"
//	@Success		200		{object}	object	"Response Results"
//	@Security		TokenAuth
//	@Summary		Checking the server environment by SSH method of Net Element Hosting
//	@Description	Checking the server environment by SSH method of Net Element Hosting
//	@Router			/ne/host/checkBySSH [post]
func (s NeHostController) CheckBySSH(c *gin.Context) {
	language := reqctx.AcceptLanguage(c)
	var body model.NeHost
	if err := c.ShouldBindBodyWithJSON(&body); err != nil {
		errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
		c.JSON(422, resp.CodeMsg(resp.CODE_PARAM_PARSER, errMsgs))
		return
	}

	var connSSH ssh.ConnSSH
	body.CopyTo(&connSSH)
	// 创建链接SSH客户端
	var client *ssh.ConnSSH
	var err error
	if body.AuthMode == "2" {
		client, err = connSSH.NewClientByLocalPrivate()
	} else {
		client, err = connSSH.NewClient()
	}
	if err != nil {
		// 连接主机失败，请检查连接参数后重试
		c.JSON(200, resp.ErrMsg(i18n.TKey(language, "neHost.errByHostInfo")))
		return
	}
	defer client.Close()

	// 结果信息数据
	data := map[string]any{
		"addr":          body.Addr, // 服务器地址
		"kernelName":    "-",       // 内核名称 -s
		"nodename":      "-",       // 网络节点主机名 -n
		"kernelRelease": "-",       // 内核发布版本 -r
		"machine":       "-",       // 机器硬件名称 -m
		"prettyName":    "-",       // 系统发行版本
		"sudo":          false,     // 可提权
		"sshLink":       false,     // 可直连
	}

	// 执行命令 检查系统环境
	output, err := client.RunCMD("uname -snrm && cat /etc/os-release | grep PRETTY_NAME")
	if err != nil {
		c.JSON(200, resp.OkData(data))
		return
	}
	output = strings.TrimSuffix(output, "\n")
	sysInfoArr := strings.SplitN(output, "\n", 2)
	if len(sysInfoArr) == 2 {
		// uname -snrm
		baseInfoArr := strings.SplitN(sysInfoArr[0], " ", 4)
		data["kernelName"] = baseInfoArr[0]
		data["nodename"] = baseInfoArr[1]
		data["kernelRelease"] = baseInfoArr[2]
		data["machine"] = baseInfoArr[3]
		// cat /etc/os-release | grep PRETTY_NAME
		prettyName := sysInfoArr[1]
		index := strings.Index(prettyName, `"`)
		if index != -1 {
			data["prettyName"] = prettyName[index+1 : len(prettyName)-1]
		}
	}
	// 执行命令 检查sudo权限
	_, err = client.RunCMD("sudo -n uname")
	if err == nil {
		data["sudo"] = true
	} else {
		data["sudo"] = false
	}

	// 本地免密创建链接直连
	if body.AuthMode == "2" {
		data["sshLink"] = true
	} else {
		lcoalConnSSH := ssh.ConnSSH{
			User: body.User,
			Addr: body.Addr,
			Port: body.Port,
		}
		lcoalClient, err := lcoalConnSSH.NewClientByLocalPrivate()
		if err == nil {
			data["sshLink"] = true
			defer lcoalClient.Close()
		} else {
			data["sshLink"] = false
		}
	}

	c.JSON(200, resp.OkData(data))
}

// 网元主机SSH方式授权免密发送
//
// POST /authorizedBySSH
//
//	@Tags			ne
//	@Accept			json
//	@Produce		json
//	@Param			data	body		object	true	"Request Param"
//	@Success		200		{object}	object	"Response Results"
//	@Security		TokenAuth
//	@Summary		Network element host SSH method of authorization for password-free sending
//	@Description	Network element host SSH method of authorization for password-free sending
//	@Router			/ne/host/authorizedBySSH [post]
func (s NeHostController) AuthorizedBySSH(c *gin.Context) {
	language := reqctx.AcceptLanguage(c)
	var body model.NeHost
	if err := c.ShouldBindBodyWithJSON(&body); err != nil {
		errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
		c.JSON(422, resp.CodeMsg(resp.CODE_PARAM_PARSER, errMsgs))
		return
	}
	if body.AuthMode == "2" {
		c.JSON(422, resp.CodeMsg(resp.CODE_PARAM_CHEACK, "bind err: auth mode not equals 2"))
		return
	}

	// 本地免密创建链接直连
	sshLink := false
	lcoalConnSSH := ssh.ConnSSH{
		User: body.User,
		Addr: body.Addr,
		Port: body.Port,
	}
	lcoalClient, err := lcoalConnSSH.NewClientByLocalPrivate()
	if err == nil {
		sshLink = true
		defer lcoalClient.Close()
	}
	if sshLink {
		// 连接主机成功，无需重复免密授权认证
		c.JSON(200, resp.OkMsg(i18n.TKey(language, "neHost.okBySSHLink")))
		return
	}

	// 创建链接SSH客户端
	var connSSH ssh.ConnSSH
	body.CopyTo(&connSSH)
	client, err := connSSH.NewClient()
	if err != nil {
		// 连接主机失败，请检查连接参数后重试
		c.JSON(200, resp.ErrMsg(i18n.TKey(language, "neHost.errByHostInfo")))
		return
	}
	defer client.Close()

	// 发送密钥
	err = client.SendToAuthorizedKeys()
	if err != nil {
		c.JSON(200, resp.ErrMsg(err.Error()))
		return
	}
	c.JSON(200, resp.Ok(nil))
}
