package controllers

import (
	"encoding/json"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/logs"
	"k8s-console/dto"
	"k8s-console/models"
	"k8s-console/utils"
	scV1 "k8s.io/api/storage/v1"
	"strings"
)

type StoreController struct {
	beego.Controller
}

/**
  存储类 --- 分页获取存储类
*/
// @router /getScList [post]
func (u *StoreController) GetAllSc() {
	lang := u.GetString("lang")
	infoLogger.Println("语言: ", lang)
	var requestDto dto.GetScListRequestDto
	var errorResponseDto = dto.BaseResponseDto{300, utils.GetI18nLang(lang, "sysError")}
	var successResponseDto = dto.GetPageListBaseResponseDto{}
	successResponseDto.Status = 200
	if err := json.Unmarshal(u.Ctx.Input.RequestBody, &requestDto); err != nil {
		errorLogger.Println(err.Error())
		errorResponseDto.Message = utils.GetI18nLang(lang, "requestParamIncorrectFormat")
		u.Data["json"] = errorResponseDto
		u.ServeJSON()
		return
	}
	infoLogger.Println("查询存储类列表请求参数: ", requestDto)
	pageIndex := requestDto.PageIndex
	pageSize := requestDto.PageSize
	if pageIndex < 0 {
		errorResponseDto.Message = utils.GetI18nLang(lang, "pageIndexRange")
		u.Data["json"] = errorResponseDto
		u.ServeJSON()
		return
	}
	if pageSize <= 0 {
		errorResponseDto.Message = utils.GetI18nLang(lang, "pageSizeRange")
		u.Data["json"] = errorResponseDto
		u.ServeJSON()
		return
	}
	beans, err := models.GetAllSc(lang)
	if err != nil {
		logs.Error("查询sc失败:", err.Error())
		errorResponseDto.Message = utils.GetI18nLang(lang, "")
		u.Data["json"] = errorResponseDto
		u.ServeJSON()
		return
	}
	resultList := []models.ScBean{}
	totalRecords := len(beans)
	totalPages := 0
	if totalRecords > 0 {
		totalPages = totalRecords/pageSize + 1
		if totalRecords%pageSize == 0 {
			totalPages = totalRecords / pageSize
		}
		if totalRecords <= pageIndex*pageSize {
			errorResponseDto.Message = utils.GetI18nLang(lang, "pageIndexOutRange")
			u.Data["json"] = errorResponseDto
			u.ServeJSON()
			return
		}
		resultList = beans[pageIndex*pageSize:]
		if len(beans)-pageIndex*pageSize >= pageSize {
			resultList = beans[pageIndex*pageSize : (pageIndex+1)*pageSize]
		}

	} else {
		if totalRecords < pageIndex*pageSize {
			errorResponseDto.Message = utils.GetI18nLang(lang, "pageIndexOutRange")
			u.Data["json"] = errorResponseDto
			u.ServeJSON()
			return
		}
	}
	successResponseDto.Result = resultList
	successResponseDto.PageInfo = dto.PageInfoBaseResponseDto{
		TotalRecords: totalRecords,
		TotalPages:   totalPages,
	}
	u.Data["json"] = successResponseDto
	u.ServeJSON()
}

// @router /delScByName [post]
func (u *StoreController) DelScByName() {
	lang := u.GetString("lang")
	infoLogger.Println("语言: ", lang)
	var requestDto dto.DelScRequestDto
	var responseDto = dto.BaseResponseDto{300, utils.GetI18nLang(lang, "sysError")}
	if err := json.Unmarshal(u.Ctx.Input.RequestBody, &requestDto); err != nil {
		errorLogger.Println(err.Error())
		responseDto.Message = utils.GetI18nLang(lang, "requestParamIncorrectFormat")
		u.Data["json"] = responseDto
		u.ServeJSON()
		return
	}
	infoLogger.Println("删除存储类请求参数: ", requestDto)

	if strings.TrimSpace(requestDto.ScName) == "" {
		errorLogger.Println("存储类不能为空")
		responseDto.Message = utils.GetI18nLang(lang, "requestParamIncorrectFormat")
		u.Data["json"] = responseDto
		u.ServeJSON()
		return
	}
	e := models.DeleteScByScName(requestDto.ScName)
	if e != nil {
		logs.Error("删除sc失败：", e.Error())
		responseDto.Message = e.Error()
		u.Data["json"] = responseDto
		u.ServeJSON()
		return
	}
	infoLogger.Printf("删除集群%s 存储类成功", requestDto.ScName)
	responseDto.Status = 200
	responseDto.Message = utils.GetI18nLang(lang, "success")
	u.Data["json"] = responseDto
	u.ServeJSON()
}

/**
  校验创建存储类的格式
*/
// @router /checkScContent [post]
func (u *StoreController) CheckScContent() {
	lang := u.GetString("lang")
	infoLogger.Println("语言: ", lang)
	var requestDto dto.CreateScReqestDto
	var responseDto = dto.BaseResponseDto{300, utils.GetI18nLang(lang, "sysError")}
	if err := json.Unmarshal(u.Ctx.Input.RequestBody, &requestDto); err != nil {
		errorLogger.Println(err.Error())
		responseDto.Message = utils.GetI18nLang(lang, "requestParamIncorrectFormat")
		u.Data["json"] = responseDto
		u.ServeJSON()
		return
	}
	infoLogger.Println("校验存储类请求参数: ", requestDto)
	scOptions := scV1.StorageClass{}
	// 复制字段
	utils.StructCopy(&scOptions, &requestDto)
	//校验格式是否正确
	err := models.CheckSc(lang, scOptions)
	if err != nil {
		responseDto.Message = err.Error()
		u.Data["json"] = responseDto
		u.ServeJSON()
		return
	}
	responseDto.Status = 200
	responseDto.Message = utils.GetI18nLang(lang, "success")
	u.Data["json"] = responseDto
	u.ServeJSON()
}

/**
  创建存储类
*/
// @router /createSc [post]
func (u *StoreController) CreateSc() {
	lang := u.GetString("lang")
	infoLogger.Println("语言: ", lang)
	var requestDto dto.CreateScReqestDto
	var responseDto = dto.BaseResponseDto{300, utils.GetI18nLang(lang, "sysError")}
	if err := json.Unmarshal(u.Ctx.Input.RequestBody, &requestDto); err != nil {
		errorLogger.Println(err.Error())
		responseDto.Message = utils.GetI18nLang(lang, "requestParamIncorrectFormat")
		u.Data["json"] = responseDto
		u.ServeJSON()
		return
	}
	infoLogger.Println("存储类创建请求参数: ", requestDto)
	scOptions := scV1.StorageClass{}
	// 复制字段
	utils.StructCopy(&scOptions, &requestDto)
	//校验格式是否正确
	err := models.CheckSc(lang, scOptions)
	if err != nil {
		responseDto.Message = err.Error()
		u.Data["json"] = responseDto
		u.ServeJSON()
		return
	}
	logs.Info("校验成功，创建存储类")
	err = models.CreateSc(lang, scOptions)
	if err != nil {
		responseDto.Message = err.Error()
		u.Data["json"] = responseDto
		u.ServeJSON()
		return
	}
	responseDto.Status = 200
	responseDto.Message = utils.GetI18nLang(lang, "success")
	u.Data["json"] = responseDto
	u.ServeJSON()
}
