package controller

import (
	"github.com/gin-gonic/gin"
	"iot_manager_service/app/middleware"
	"iot_manager_service/app/multimedia/model"
	"iot_manager_service/app/multimedia/service"
	service2 "iot_manager_service/app/system/service"
	"iot_manager_service/util/common"
	"math"
	"net/http"
	"strconv"
	"strings"
)

// PublishLibraries 节目播放
var PublishLibraries = new(PublishLibrariesCtl)

type PublishLibrariesCtl struct{}

// List 节目播放列表
func (c *PublishLibrariesCtl) List(ctx *gin.Context) {
	searchValue := ctx.Query("searchValue")
	current, _ := strconv.Atoi(ctx.Query("current"))
	size, _ := strconv.Atoi(ctx.Query("size"))
	sysType, _ := strconv.Atoi(ctx.Query("sysType"))
	if current == 0 {
		current = 1
	}
	if size <= 0 || size > 100 {
		size = 10
	}
	value, _ := ctx.Get(middleware.Authorization)
	claims := value.(*middleware.Claims)

	libraries, total, err := service.PublishLibrariesService.List(claims.TenantId, searchValue, current, size, sysType)
	for i, library := range libraries {
		stime := library.StartTime.DateString()
		etime := library.EndTime.DateString()
		libraries[i].PlayPeriod = stime + " - " + etime
		libraries[i].ResolutionName = service2.DictService.GetResolutionName(library.TenantId, library.Resolution)
		libraries[i].CountDeviceNum = len(strings.Split(library.InfoId, ","))
	}
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}

	pages := math.Ceil(float64(total) / float64(size))
	rsp := model.RsqPublishLibrariesList{
		Current: current,
		Size:    size,
		Total:   int(total),
		Pages:   int(pages),
		Records: libraries,
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Success, rsp))
}

// GetLibraryList 节目播放 预览
func (c *PublishLibrariesCtl) GetLibraryList(ctx *gin.Context) {
	id := ctx.Query("id")
	librariesId, e := strconv.Atoi(id)
	if e != nil {
		ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(e.Error(), nil))
		return
	}
	value, _ := ctx.Get(middleware.Authorization)
	claims := value.(*middleware.Claims)

	programId, err := service.PublishLibrariesService.GetProgram(librariesId)
	if err != nil {
		ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(e.Error(), nil))
		return
	}
	list, errors := service.ProgramService.GetLibraryList(claims.TenantId, programId)
	if errors != nil {
		ctx.JSON(http.StatusOK, e)
		return
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Success, list))
}

// AffiliateEquipment 关联设备
func (c *PublishLibrariesCtl) AffiliateEquipment(ctx *gin.Context) {
	sysType, _ := strconv.Atoi(ctx.Query("sysType"))
	deviceIds := ctx.Query("infoId") //设备id
	value, _ := ctx.Get(middleware.Authorization)
	claims := value.(*middleware.Claims)

	devices, err := service.ProgramService.RelationDeviceListByIds(claims.TenantId, sysType, deviceIds)
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}

	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Success, devices))
}

// UpdateStatus 变更状态
func (c *PublishLibrariesCtl) UpdateStatus(ctx *gin.Context) {
	id, _ := strconv.Atoi(ctx.Query("id"))
	status, _ := strconv.Atoi(ctx.Query("status"))
	name := ctx.Query("name")
	value, _ := ctx.Get(middleware.Authorization)
	claims := value.(*middleware.Claims)

	_, err := service.PublishLibrariesService.UpdateStatus(claims.TenantId, id, status, name)
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Success, nil))
}

func (c *PublishLibrariesCtl) Remove(ctx *gin.Context) {
	value, _ := ctx.Get(middleware.Authorization)
	claims := value.(*middleware.Claims)

	ids, _ := strconv.Atoi(ctx.Query("ids"))
	err := service.PublishLibrariesService.Remove(claims.UserId, claims.TenantId, ids)
	if err != nil {
		ctx.JSON(http.StatusOK, err)
		return
	}
	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, nil))
}
