package cast

import (
	"context"
	"errors"
	"fonchain-fiee/api/accountFiee"
	"fonchain-fiee/api/cast"
	"fonchain-fiee/cmd/config"
	"fonchain-fiee/pkg/e"
	"fonchain-fiee/pkg/service"
	"github.com/gin-gonic/gin"
	"strconv"
)

func MediaUserList(ctx *gin.Context) {
	var req *cast.MediaUserListReq
	var err error
	if err = ctx.ShouldBind(&req); err != nil {
		service.Error(ctx, err)
		return
	}
	if err = req.Validate(); err != nil {
		service.Error(ctx, err)
		return
	}
	resp, err := service.CastProvider.MediaUserList(ctx, req)
	if err != nil {
		service.Error(ctx, err)
		return
	}
	service.Success(ctx, resp)
	return
}

func UnbindManager(ctx *gin.Context) {
	var req *cast.UnbindManagerReq
	var err error
	if err = ctx.ShouldBind(&req); err != nil {
		service.Error(ctx, err)
		return
	}
	if err = req.Validate(); err != nil {
		service.Error(ctx, err)
		return
	}
	_, err = service.CastProvider.UnbindManager(ctx, req)
	if err != nil {
		service.Error(ctx, err)
		return
	}
	service.Success(ctx, nil)
	return
}

func BindManager(ctx *gin.Context) {
	var req *cast.BindManagerReq
	var err error
	if err = ctx.ShouldBind(&req); err != nil {
		service.Error(ctx, err)
		return
	}
	if err = req.Validate(); err != nil {
		service.Error(ctx, err)
		return
	}
	_, err = service.CastProvider.BindManager(ctx, req)
	if err != nil {
		service.Error(ctx, err)
		return
	}
	service.Success(ctx, nil)
	return
}

func UpdateMediaAccount(ctx *gin.Context) {
	var req *cast.UpdateMediaAccountReq
	var infoResp *accountFiee.UserInfoResponse
	var err error
	if err = ctx.ShouldBind(&req); err != nil {
		service.Error(ctx, err)
		return
	}
	if err = req.Validate(); err != nil {
		service.Error(ctx, err)
		return
	}
	// 查询艺人的信息
	// 字符串转整型
	artistID, err := strconv.ParseUint(req.ArtistUuid, 10, 64)
	if config.AppConfig.System.AppMode != "dev" {
		infoResp, err = service.AccountFieeProvider.Info(context.Background(), &accountFiee.InfoRequest{
			ID:     artistID,
			Domain: "app",
		})
		if err != nil {
			service.Error(ctx, err)
			return
		}
	} else {
		infoResp = &accountFiee.UserInfoResponse{
			Name:   "小波",
			TelNum: "18288888888",
		}
	}
	req.ArtistName = infoResp.Name
	req.ArtistPhone = infoResp.TelNum
	if _, ok := cast.PlatformIDENUM_name[int32(req.PlatformID)]; !ok {
		service.Error(ctx, errors.New(e.GetMsg(e.InvalidParams)))
		return
	}
	resp, err := service.CastProvider.UpdateMediaAccount(ctx, req)
	if err != nil {
		service.Error(ctx, err)
		return
	}
	service.Success(ctx, resp)
	return
}
