package cast

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

func UpdateWorkImage(ctx *gin.Context) {
	var req *cast.UpdateWorkImageReq
	var infoResp *accountFiee.UserInfoResponse
	var err error
	if err = ctx.ShouldBind(&req); err != nil {
		service.Error(ctx, err)
		return
	}
	if config.AppConfig.System.AppMode != "dev" {
		artistId, _ := strconv.ParseUint(req.ArtistUuid, 10, 64)
		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
	newCtx := NewCtxWithUserInfo(ctx)
	resp, err := service.CastProvider.UpdateWorkImage(newCtx, req)
	if err != nil {
		service.Error(ctx, err)
		return
	}
	service.Success(ctx, resp)
	return
}

func UpdateWorkVideo(ctx *gin.Context) {
	var req *cast.UpdateWorkVideoReq
	var infoResp *accountFiee.UserInfoResponse
	var err error
	if err = ctx.ShouldBind(&req); err != nil {
		service.Error(ctx, err)
		return
	}
	if config.AppConfig.System.AppMode != "dev" {
		artistId, _ := strconv.ParseUint(req.ArtistUuid, 10, 64)
		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
	newCtx := NewCtxWithUserInfo(ctx)
	resp, err := service.CastProvider.UpdateWorkVideo(newCtx, req)
	if err != nil {
		service.Error(ctx, err)
		return
	}
	service.Success(ctx, resp)
	return
}

func UpdateApproval(ctx *gin.Context) {
	var req *cast.UpdateStatusReq
	var err error
	if err = ctx.ShouldBind(&req); err != nil {
		service.Error(ctx, err)
		return
	}
	req.WorkAction = cast.WorkActionENUM_APPROVAL
	newCtx := NewCtxWithUserInfo(ctx)
	//FIXME
	reqNew := &cast.UpdateStatusReq{
		WorkAction:    cast.WorkActionENUM_CONFIRM,
		WorkUuid:      req.WorkUuid,
		ConfirmStatus: 1,
	}
	resp, err := service.CastProvider.UpdateStatus(newCtx, reqNew)
	if err != nil {
		service.Error(ctx, err)
		return
	}
	service.Success(ctx, resp)
	return
}

func WorkList(ctx *gin.Context) {
	var (
		req  *cast.WorkListReq
		resp *cast.WorkListResp
	)
	var err error
	if err = ctx.ShouldBind(&req); err != nil {
		service.Error(ctx, err)
		return
	}
	newCtx := NewCtxWithUserInfo(ctx)
	resp, err = service.CastProvider.WorkList(newCtx, req)
	if err != nil {
		service.Error(ctx, err)
		return
	}
	service.Success(ctx, resp)
	return
}

func WorkDetail(ctx *gin.Context) {
	var (
		req  *cast.WorkDetailReq
		resp *cast.WorkDetailResp
	)
	var err error
	if err = ctx.ShouldBind(&req); err != nil {
		service.Error(ctx, err)
		return
	}
	newCtx := NewCtxWithUserInfo(ctx)
	resp, err = service.CastProvider.WorkDetail(newCtx, req)
	if err != nil {
		service.Error(ctx, err)
		return
	}
	service.Success(ctx, resp)
	return
}
