package cast

import (
	"context"
	"errors"
	"fmt"
	"fonchain-fiee/api/accountFiee"
	"fonchain-fiee/api/bundle"
	"fonchain-fiee/api/cast"
	"fonchain-fiee/cmd/config"
	"fonchain-fiee/pkg/e"
	"fonchain-fiee/pkg/service"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"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
	artistID, _ := strconv.ParseUint(req.ArtistUuid, 10, 64)
	if err = CheckUserBundleBalance(int32(artistID)); err != nil {
		service.Error(ctx, err)
		return
	}
	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",
		}
	}
	artistID, _ := strconv.ParseUint(req.ArtistUuid, 10, 64)
	if err = CheckUserBundleBalance(int32(artistID)); err != nil {
		service.Error(ctx, err)
		return
	}
	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
}

func CheckUserBundleBalance(userID int32) (err error) {
	resp, err := service.BundleProvider.GetBundleBalanceByUserId(context.Background(), &bundle.GetBundleBalanceByUserIdReq{UserId: userID})
	if err != nil {
		return
	}
	zap.L().Info("CheckUserBundleBalance", zap.Any("resp", resp))
	fmt.Println("CheckUserBundleBalance resp", resp)
	if resp.AccountNumber-resp.AccountConsumptionNumber <= 0 {
		err = errors.New(e.ErrorBalanceInsufficient)
		return
	}
	return
}

func RePublish(ctx *gin.Context) {
	var (
		req  *cast.RePublishReq
		resp *cast.RePublishResp
	)
	var err error
	if err = ctx.ShouldBind(&req); err != nil {
		service.Error(ctx, err)
		return
	}
	newCtx := NewCtxWithUserInfo(ctx)
	resp, err = service.CastProvider.RePublish(newCtx, req)
	if err != nil {
		service.Error(ctx, err)
		return
	}
	artistID, _ := strconv.ParseUint(resp.ArtistUuid, 10, 64)
	_, err = service.BundleProvider.AddBundleBalance(context.Background(), &bundle.AddBundleBalanceReq{
		UserId:                   int32(artistID),
		AccountConsumptionNumber: -1,
	})
	if err != nil {
		service.Error(ctx, err)
		//FIXME 进行回滚
		return
	}
	service.Success(ctx, nil)
	return
}

//修改余量我还需要调用吗
func UserBundleBalanceCost() {
	service.BundleProvider.AddBundleBalance(context.Background(), &bundle.AddBundleBalanceReq{})
}
