package controllers

import (
	"eta/eta_mini_ht_api/common/contants"
	"eta/eta_mini_ht_api/common/utils/page"
	stringUtils "eta/eta_mini_ht_api/common/utils/string"
	"strings"
)

type ListController struct {
	BaseController
	PageInfo page.PageInfo
}

const (
	video = "video"
	audio = "audio"
)

func (l *ListController) Prepare() {
	pageSize, _ := l.GetInt("pageSize")
	currentIndex, _ := l.GetInt("currentIndex")
	LatestId, _ := l.GetInt64("latestId")
	total, _ := l.GetInt64("total")
	if pageSize <= 0 {
		pageSize = contants.PageSizeDefault
	}
	if currentIndex <= 0 {
		currentIndex = 1
	}
	if LatestId < 0 {
		LatestId = 0
	}
	l.PageInfo = page.PageInfo{
		LatestId: LatestId,
		Current:  currentIndex,
		PageSize: pageSize,
		Total:    total,
	}
}

func (l *ListController) TransPermissionIds(permissionIds string) (permissionIdList []int, err error) {
	if strings.HasPrefix(permissionIds, ",") {
		permissionIds = permissionIds[1:len(permissionIds)]
	}
	if strings.HasSuffix(permissionIds, ",") {
		permissionIds = permissionIds[0 : len(permissionIds)-1]
	}
	if permissionIds == "" {
		permissionIdList = []int{}
	} else {
		permissionIdList, err = stringUtils.StringToIntSlice(strings.Split(permissionIds, ","))
	}
	return
}

func (l *ListController) CheckMediaType(mediaType string) bool {
	if mediaType == video || mediaType == audio {
		return true
	}
	return false
}

//func (l *ListController) Finish() {
//	l.PageInfo.Reset()
//	runMode := web.BConfig.RunMode
//	if l.Data["json"] == nil {
//		logger.Warn("apiRequest:[异常提醒:%v 接口:URI:%v;无返回值]", runMode, l.Ctx.Input.URI())
//		return
//	}
//	baseRes := l.Data["json"].(BaseResponse)
//	if !baseRes.Success {
//		logger.Info("apiRequest:[异常提醒:%v接口:URI:%v;ErrMsg:&v;Msg:%v]", l.Ctx.Input.URI(), baseRes.ErrMsg, baseRes.Msg)
//	} else {
//		if baseRes.Data == nil {
//			logger.Warn("apiRequest:[异常提醒:%v 接口:URI:%v;无返回值]", runMode, l.Ctx.Input.URI())
//			return
//		} else {
//			logger.Info("apiRequest:[uri:%s, resData:%s, ip:%s]", l.Ctx.Input.URI(), baseRes.Data)
//		}
//	}
//}