package controller

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"iot_manager_service/app/device/edge_service"
	"iot_manager_service/app/middleware"
	"iot_manager_service/app/multimedia/dao"
	"iot_manager_service/app/multimedia/model"
	"iot_manager_service/app/multimedia/service"
	"iot_manager_service/util/common"
	"math"
	"net/http"
	"strconv"
)

var Library = new(libraryCtl)

type libraryCtl struct{}

func (c *libraryCtl) Detail(ctx *gin.Context) {
	id, e := strconv.Atoi(ctx.Query("id"))
	if e != nil {
		ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(e.Error(), nil))
		return
	}
	library, err := service.LibraryService.Get(id)
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Success, library))
}

func (c *libraryCtl) List(ctx *gin.Context) {
	searchValue := ctx.Query("searchValue")
	current, _ := strconv.Atoi(ctx.Query("current"))
	size, _ := strconv.Atoi(ctx.Query("size"))
	s := ctx.Query("sysType")
	libTypeStr := ctx.Query("libType")

	if current == 0 {
		current = 1
	}
	if size <= 0 || size > 100 {
		size = 10
	}
	sysType := -1
	if s != "" {
		sysType, _ = strconv.Atoi(s)
	}

	libType := -1
	if s != "" {
		libType, _ = strconv.Atoi(libTypeStr)
	}

	value, _ := ctx.Get(middleware.Authorization)
	claims := value.(*middleware.Claims)
	libraries, total, err := service.LibraryService.List(claims.TenantId, searchValue, current, size, sysType, libType)
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	pages := math.Ceil(float64(total) / float64(size))
	rsp := model.RsqLibraryList{
		Current: current,
		Size:    size,
		Total:   int(total),
		Pages:   int(pages),
		Records: libraries,
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Success, rsp))
}

func (c *libraryCtl) Remove(ctx *gin.Context) {
	ids := ctx.Query("ids")
	id, e := strconv.Atoi(ids)
	if e != nil {
		ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(e.Error(), nil))
		return
	}

	value, _ := ctx.Get(middleware.Authorization)
	claims := value.(*middleware.Claims)

	err := service.LibraryService.Remove(claims.UserId, claims.TenantId, id)
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Success, nil))
}

func (c *libraryCtl) GetList(ctx *gin.Context) {
	libName := ctx.Query("libName")
	t := ctx.Query("libType")
	s := ctx.Query("sysType")
	value, _ := ctx.Get(middleware.Authorization)
	claims := value.(*middleware.Claims)

	libType := -1
	if t != "" {
		libType, _ = strconv.Atoi(t)
	}
	sysType := -1
	if s != "" {
		sysType, _ = strconv.Atoi(s)
	}

	library, err := service.LibraryService.GetList(claims.TenantId, libName, libType, sysType)
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Success, library))
}

func (c *libraryCtl) UploadFile(ctx *gin.Context) {
	file, err := ctx.FormFile("file")
	if err != nil {
		ctx.JSON(http.StatusOK, common.FailResponse(err.Error(), nil))
		return
	}
	value, _ := ctx.Get(middleware.Authorization)
	claims := value.(*middleware.Claims)

	rsp, e := service.LibraryService.UploadFile(claims.TenantId, file)
	if e != nil {
		ctx.JSON(http.StatusOK, e)
		return
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Success, rsp))
}

func (c *libraryCtl) Submit(ctx *gin.Context) {
	var req dao.Library
	if err := ctx.ShouldBindJSON(&req); err != nil {
		ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(err.Error(), nil))
		return
	}
	value, _ := ctx.Get(middleware.Authorization)
	claims := value.(*middleware.Claims)
	if req.LibType == 4 {
		//如果是音频, 要上传到音频服务器得到文件ID
		fileName := fmt.Sprintf("%v/new/%v", claims.TenantId, req.LibName)
		fileId, err := edge_service.IpCastControlService{}.UploadMp3GetFileId(req.MaterialAddress, fileName)
		if err != nil {
			ctx.JSON(http.StatusOK, common.FailResponse(err.Error(), nil))
			return
		}
		req.FileId = fileId
	}
	e := service.LibraryService.Submit(claims.TenantId, claims.UserId, req)

	if e != nil {
		ctx.JSON(http.StatusOK, e)
		return
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Success, nil))
}
