package controllers

import (
	"encoding/json"
	"github.com/astaxie/beego"
	"k8s-console/dto"
	"k8s-console/utils"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"strings"
)

// StatefulSetController operations for Service
type StatefulSetController struct {
	beego.Controller
}

// URLMapping ...
func (c *StatefulSetController) URLMapping() {
	c.Mapping("getStatefulSetServiceName", c.GetStatefulSetServiceName)
}

// Post ...
// @Title GetStatefulSetServiceName
// @Description get StatefulSetServiceName
// @Param	lang		query 	string	true	"api result language type"
// @Param	body		body 	dto.GetStatefulSetServiceNameRequestDto	true	""
// @Success 200 {object} dto.BaseResultResponseDto
// @Failure 300 {object} dto.BaseResponseDto
// @router /getStatefulSetServiceName [post]
func (c *StatefulSetController) GetStatefulSetServiceName() {
	lang := c.GetString("lang")
	infoLogger.Println("语言: ", lang)
	var requestDto dto.GetStatefulSetServiceNameRequestDto
	var errorResponseDto = dto.BaseResponseDto{300, utils.GetI18nLang(lang, "sysError")}
	var successResponseDto = dto.BaseResultResponseDto{}
	successResponseDto.Status = 200
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &requestDto); err != nil {
		errorLogger.Println(err.Error())
		errorResponseDto.Message = utils.GetI18nLang(lang, "requestParamIncorrectFormat")
		c.Data["json"] = errorResponseDto
		c.ServeJSON()
		return
	}
	infoLogger.Println("获取StatefulSet的ServiceName请求参数: ", requestDto)

	nameSpace := requestDto.NameSpace
	statefulSetName := requestDto.StatefulSetName
	if strings.TrimSpace(nameSpace) == "" {
		errorLogger.Println("命名空间不能为空")
		errorResponseDto.Message = utils.GetI18nLang(lang, "requestParamIncorrectFormat")
		c.Data["json"] = errorResponseDto
		c.ServeJSON()
		return
	}
	if strings.TrimSpace(statefulSetName) == "" {
		errorLogger.Println("StatefulSet名称不能为空")
		errorResponseDto.Message = utils.GetI18nLang(lang, "requestParamIncorrectFormat")
		c.Data["json"] = errorResponseDto
		c.ServeJSON()
		return
	}
	//查询集群所有命名空间信息
	allNameSpaceList, err := utils.GetClusterAllNameSpaces(lang)
	infoLogger.Println(allNameSpaceList)
	if err != nil {
		errorLogger.Printf("查询集群所有命名空间失败: %s", err.Error())
		errorResponseDto.Message = utils.GetI18nLang(lang, "getClusterAllNamespacesFail")
		c.Data["json"] = errorResponseDto
		c.ServeJSON()
		return
	}
	if len(allNameSpaceList) > 0 && !utils.IsContains(allNameSpaceList, nameSpace) {
		errorLogger.Println("集群不存在此命名空间")
		errorResponseDto.Message = utils.GetI18nLang(lang, "theNameSpaceNotExistInCluster")
		c.Data["json"] = errorResponseDto
		c.ServeJSON()
		return
	}

	// 获取k8s客户端
	k8sClient := utils.GetK8sClient()
	if k8sClient == nil {
		errorResponseDto.Message = utils.GetI18nLang(lang, "createK8sConFail")
		c.Data["json"] = errorResponseDto
		c.ServeJSON()
		return
	}

	//查询StatefulSet详情
	statefulSetInfo, err := k8sClient.AppsV1().StatefulSets(nameSpace).Get(statefulSetName, metav1.GetOptions{})
	if err != nil {
		errorLogger.Printf("查询集群%s空间%s的StatefulSet详情失败: %s", nameSpace, statefulSetName, err.Error())
		errorResponseDto.Message = utils.GetI18nLang(lang, "getControllerPortList_getStatefulSetInfoFail")
		c.Data["json"] = errorResponseDto
		c.ServeJSON()
		return
	}
	serviceName := statefulSetInfo.Spec.ServiceName
	infoLogger.Printf("ServiceName:%s", serviceName)

	successResponseDto.Result = serviceName
	c.Data["json"] = successResponseDto
	c.ServeJSON()
}
