// Code generated by gowebx, DO AVOID EDIT.
package api

import (
	"easyaudioencode/internal/conf"
	"easyaudioencode/internal/core/audioencode"
	"easyaudioencode/internal/core/host"
	"easyaudioencode/internal/core/media"
	"easyaudioencode/internal/core/transcode"
	"fmt"
	"git.lnton.com/lnton/pkg/reason"
	"git.lnton.com/lnton/pkg/web"
	"github.com/gin-gonic/gin"
	"os"
	"strconv"
)

type Loger interface {
	RecordLog(remark string) gin.HandlerFunc
}
type AudioEncodeAPI struct {
	core          *audioencode.Core
	meidaCore     *media.Core
	transcodeCore *transcode.Core
	cfg           *conf.Bootstrap
	HostCore      *host.Core
}

func NewAudioEncodeAPI(core *audioencode.Core, meidaCore *media.Core, transcodeCore *transcode.Core, HostCore *host.Core, cfg *conf.Bootstrap) AudioEncodeAPI {
	return AudioEncodeAPI{core: core, meidaCore: meidaCore, transcodeCore: transcodeCore, HostCore: HostCore, cfg: cfg}
}

func RegisterAudioEncode(g gin.IRouter, api AudioEncodeAPI, handler ...gin.HandlerFunc) {
	{

		group := g.Group("/api/audioencode", handler...)
		group.GET("", web.WarpH(api.findAudioEncode))
		group.GET("/:id", web.WarpH(api.getAudioEncode))
		group.PUT("/:id", web.WarpH(api.editAudioEncode))
		//group.POST("", web.WarpH(api.addAudioEncode))
		group.DELETE("/:id", web.WarpH(api.delAudioEncode))

		group.POST("/upload", web.WarpH(api.uploadAudioHandler))
		group.GET("/:id/code", web.WarpH(api.getCode))

		// 任务
		groupTask := g.Group("/api/audiotask", handler...)
		groupTask.POST("", web.WarpH(api.addAudioTask))
		groupTask.GET("", web.WarpH(api.findAudioTask))
		groupTask.DELETE("/:id", web.WarpH(api.delAudioTask))
		groupTask.DELETE("", web.WarpH(api.delAudioTaskAll))
		groupTask.GET("/cancel/:id", web.WarpH(api.cancelAudioTask))

	}
}

// >>> transcodeCore >>>>>>>>>>>>>>>>>>>>
func (a AudioEncodeAPI) getCode(c *gin.Context, _ *struct{}) (any, error) {
	ID, _ := strconv.Atoi(c.Param("id"))
	info, err := a.core.GetAudioEncode(c.Request.Context(), ID)
	if err != nil {
		return nil, reason.ErrServer.SetMsg(fmt.Sprintf(`find audioencode [%d] err [%s]`, ID, err.Error()))
	}
	a.transcodeCore.StartAudioEncode(info.SourceUrl, info.EncodeUrl, info.ID)
	return gin.H{"data": "OK!"}, err
}

func (a AudioEncodeAPI) findAudioEncode(c *gin.Context, in *audioencode.FindAudioEncodeInput) (any, error) {
	items, total, err := a.core.FindAudioEncode(c.Request.Context(), in)
	rows := make([]map[string]interface{}, 0)

	for _, item := range items {
		//row := structs.Map(item)
		row := make(map[string]interface{})

		row["name"] = item.Name
		row["file_name"] = item.FileName
		row["size"] = item.Size
		row["id"] = item.ID
		row["mode"] = item.Mode
		row["source_url"] = item.SourceUrl
		row["created_at"] = item.CreatedAt
		row["updated_at"] = item.UpdatedAt
		row["encode_status"] = item.EncodeStatus
		row["duration"] = item.Duration
		row["des"] = item.Des
		rows = append(rows, row)
	}

	return gin.H{"items": rows, "total": total}, err
}

func (a AudioEncodeAPI) getAudioEncode(c *gin.Context, _ *struct{}) (any, error) {
	ID, _ := strconv.Atoi(c.Param("id"))
	item, err := a.core.GetAudioEncode(c.Request.Context(), ID)
	if err != nil {
		return nil, reason.ErrServer.SetMsg(fmt.Sprintf(`find audioencode [%d] err [%s]`, ID, err.Error()))
	}
	row := make(map[string]interface{})

	row["name"] = item.Name
	row["file_name"] = item.FileName
	row["size"] = item.Size
	row["id"] = item.ID
	row["mode"] = item.Mode
	row["source_url"] = item.SourceUrl
	row["created_at"] = item.CreatedAt
	row["updated_at"] = item.UpdatedAt
	row["encode_status"] = item.EncodeStatus
	row["duration"] = item.Duration
	row["des"] = item.Des
	return gin.H{"data": row}, err
}

func (a AudioEncodeAPI) editAudioEncode(c *gin.Context, in *audioencode.EditAudioEncodeInput) (any, error) {
	ID, _ := strconv.Atoi(c.Param("id"))
	_, err := a.core.GetAudioEncode(c.Request.Context(), ID)
	if err != nil {
		return nil, reason.ErrServer.SetMsg(fmt.Sprintf(`find audioencode [%d] err [%s]`, ID, err.Error()))
	}

	_, err = a.core.EditAudioEncode(c.Request.Context(), in, ID)
	if err != nil {
		return nil, reason.ErrServer.SetMsg(fmt.Sprintf(`edit audioencode err [%s]`, err.Error()))
	}
	return gin.H{"data": "OK!"}, err
}

func (a AudioEncodeAPI) addAudioEncode(c *gin.Context, in *audioencode.AddAudioEncodeInput) (any, error) {

	_, err := a.core.AddAudioEncode(c.Request.Context(), in)
	if err != nil {
		return nil, reason.ErrServer.SetMsg(fmt.Sprintf(`add audioencode err [%s]`, err.Error()))
	}

	return gin.H{"data": "OK!"}, err
}

func (a AudioEncodeAPI) delAudioEncode(c *gin.Context, _ *struct{}) (any, error) {
	ID, _ := strconv.Atoi(c.Param("id"))
	info, err := a.core.DelAudioEncode(c.Request.Context(), ID)
	if err != nil {
		return nil, reason.ErrServer.SetMsg(fmt.Sprintf(`del audioencode [%d] err [%s]`, ID, err.Error()))
	}

	err = os.Remove(info.SourceUrl)
	if err != nil {
		return nil, reason.ErrServer.SetMsg(fmt.Sprintf(`del SourceUrl [%d] err [%s]`, ID, err.Error()))
	}
	err = os.Remove(info.EncodeUrl)
	if err != nil {
		return nil, reason.ErrServer.SetMsg(fmt.Sprintf(`del SourceUrl [%d] err [%s]`, ID, err.Error()))
	}
	return gin.H{"data": "OK!"}, err
}
