package controller

import (
	"context"
	"fmt"

	"github.com/fonchain/fonchain-artistinfo/cmd/model"
	"github.com/fonchain/fonchain-artistinfo/pb/artistInfoUser"
	db "github.com/fonchain/fonchain-artistinfo/pkg/db"
	"gorm.io/gorm"

	"github.com/fonchain/fonchain-artistinfo/cmd/internal/logic"
)

var _ artistInfoUser.ArtistInfoUserServer = new(ArtistInfoUserProvider)

type ArtistInfoUserProvider struct {
	artistInfoUser.UnimplementedArtistInfoUserServer
	artistInfoLogic *logic.ArtistInfoUser
}

func (a *ArtistInfoUserProvider) FindUsersUserView(ctx context.Context, request *artistInfoUser.FindUsersRequest) (*artistInfoUser.FindUsersUserViewResponse, error) {
	return a.artistInfoLogic.FindUsersUserView(request)
}

func (a *ArtistInfoUserProvider) FindUsers(ctx context.Context, request *artistInfoUser.FindUsersRequest) (*artistInfoUser.FindUsersResponse, error) {
	return a.artistInfoLogic.FindUserList(request)
}

func (a *ArtistInfoUserProvider) RegisterUser(ctx context.Context, req *artistInfoUser.RegisterUserRequest) (rep *artistInfoUser.RegisterUserRespond, err error) {
	fmt.Println("第一处")
	// backup := &artistinfoUser.GetUserInfoRespond{}
	if rep, err = a.artistInfoLogic.RegisterUser(req); err != nil {
		return nil, err
	}
	return rep, nil
}

func (a *ArtistInfoUserProvider) UpdateIdCard(ctx context.Context, in *artistInfoUser.UpdateIdCardRequest) (rep *artistInfoUser.CommonNoParams, err error) {
	var thisUser model.User
	db.DB.Where("mgmt_acc_id = ?", in.MgmtAccId).Preload("RealNameInfo").Find(&thisUser)
	if thisUser.RealNameInfo == nil {
		thisUser.RealNameInfo = &model.RealName{}
	}
	if in.Name != "" {
		thisUser.RealNameInfo.Name = in.Name
	}
	if in.IdNum != "" {
		thisUser.RealNameInfo.IdNum = in.IdNum
	}
	if in.Age != 0 {
		thisUser.RealNameInfo.Age = int(in.Age)
	}
	if in.Sex != "" {
		thisUser.RealNameInfo.Sex = model.SexType(in.Sex)
	}
	if in.Address != "" {
		thisUser.RealNameInfo.Address = in.Address
	}
	if in.IdCardFront != "" {
		thisUser.RealNameInfo.IdCardFront = in.IdCardFront
	}
	if in.IdCardBack != "" {
		thisUser.RealNameInfo.IdCardBack = in.IdCardBack
	}
	if in.Birthday != "" {
		thisUser.RealNameInfo.Birthday = in.Birthday
	}
	err = db.DB.Session(&gorm.Session{FullSaveAssociations: true}).Save(&thisUser).Error
	return
}

func (a *ArtistInfoUserProvider) GetUser(ctx context.Context, req *artistInfoUser.GetUserRequest) (rep *artistInfoUser.GetUserRespond, err error) {
	fmt.Println("第一处")
	// backup := &artistinfoUser.GetUserInfoRespond{}
	if rep, err = a.artistInfoLogic.GetUser(req); err != nil {
		return nil, err
	}
	return rep, nil
}

func (a *ArtistInfoUserProvider) GetUserById(ctx context.Context, req *artistInfoUser.GetUserByIdRequest) (rep *artistInfoUser.GetUserByIdRespond, err error) {
	return a.artistInfoLogic.GetUserById(req)
}

//func (a *ArtistInfoUserProvider ) UpdateRealName(ctx context.Context, req *artistinfoUser.UpdateRealNameRequest) (rep *artistinfoUser.UpdateRealNameRespond, err error) {
//	fmt.Println("第一处")
//	// backup := &artistinfoUser.GetUserInfoRespond{}
//	if rep, err = a.artistInfoLogic.UpdateRealName(req); err != nil {
//		return nil, err
//	}
//	return rep, nil
//}

func (a *ArtistInfoUserProvider) FinishVerify(ctx context.Context, req *artistInfoUser.FinishVerifyRequest) (rep *artistInfoUser.FinishVerifyRespond, err error) {
	fmt.Println("第一处")
	// backup := &artistinfoUser.GetUserInfoRespond{}
	if rep, err = a.artistInfoLogic.FinishVerify(req); err != nil {
		return nil, err
	}
	return rep, nil
}
func (a *ArtistInfoUserProvider) UserLock(ctx context.Context, req *artistInfoUser.UserLockRequest) (rep *artistInfoUser.UserLockRespond, err error) {
	return a.artistInfoLogic.UserLock(req)
}
func (a *ArtistInfoUserProvider) CheckUserLock(ctx context.Context, req *artistInfoUser.CheckUserLockRequest) (rep *artistInfoUser.CheckUserLockRespond, err error) {
	fmt.Println("第一处")
	// backup := &artistinfoUser.GetUserInfoRespond{}
	if rep, err = a.artistInfoLogic.CheckUserLock(req); err != nil {
		return nil, err
	}
	return rep, nil
}

func (a *ArtistInfoUserProvider) ArtistSupplyList(ctx context.Context, req *artistInfoUser.ArtistSupplyListRequest) (rep *artistInfoUser.ArtistSupplyListRespond, err error) {
	fmt.Println("第一处")
	// backup := &artistinfoUser.GetUserInfoRespond{}
	if rep, err = a.artistInfoLogic.ArtistSupplyList(req); err != nil {
		return nil, err
	}
	return rep, nil
}

func (a *ArtistInfoUserProvider) CheckInvitedCode(ctx context.Context, req *artistInfoUser.CheckInvitedCodeRequest) (rep *artistInfoUser.GetUserRespond, err error) {
	fmt.Println("第一处")
	// backup := &artistinfoUser.CheckInvitedCodeInfoRespond{}
	if rep, err = a.artistInfoLogic.CheckInvitedCode(req); err != nil {
		return nil, err
	}
	return rep, nil
}

func (a *ArtistInfoUserProvider) UnFinishList(ctx context.Context, req *artistInfoUser.UnFinishListRequest) (rep *artistInfoUser.UnFinishListRespond, err error) {
	fmt.Println("第一处")
	// backup := &artistinfoUser.UnFinishListInfoRespond{}
	if rep, err = a.artistInfoLogic.UnFinishList(req); err != nil {
		return nil, err
	}
	return rep, nil
}

func (a *ArtistInfoUserProvider) GetUserMsg(ctx context.Context, req *artistInfoUser.GetUserMsgRequest) (rep *artistInfoUser.GetUserMsgRespond, err error) {
	fmt.Println("第一处")
	// backup := &artistinfoUser.GetUserMsgInfoRespond{}
	if rep, err = a.artistInfoLogic.GetUserMsg(req); err != nil {
		return nil, err
	}
	return rep, nil
}

// 绑定邀请人和受邀请人的账号,并加入到次数统计
func (a *ArtistInfoUserProvider) BindInviteInvitedAccount(ctx context.Context, in *artistInfoUser.BindInviteInvitedAccountRequest) (res *artistInfoUser.BindInviteInvitedAccountRespond, err error) {
	if in.UserId == 0 || in.InvitedUserId == 0 {
		return nil, err
	}
	// todo 变量in 参数检测: 邀请码没填的话需要从账号服务获取
	// ..
	// ..
	var data model.Invite
	// 只能绑定一个邀请人
	if err = db.DB.Where("invited_id = ?", in.InvitedUserId).Find(&data).Error; err != nil {
		return nil, err
	}
	if data.UserId == 0 {
		data = model.Invite{
			UserId:      in.UserId,
			InvitedId:   in.InvitedUserId,
			InvitedCode: in.InvitedCode,
			InviteCode:  in.InviteCode,
		}
		if err = db.DB.Create(&data).Error; err != nil {
			return nil, err
		}
	}

	// 添加到次数统计
	var countData model.UserInvited
	if err = db.DB.Where("user_id= ? AND invited_user_id= ?", in.UserId, in.GetInvitedUserId()).Find(&countData).Error; err != nil {
		return nil, err
	}
	if countData.UserId == 0 {
		countData = model.UserInvited{
			UserId:        in.UserId,
			InvitedUserId: in.InvitedUserId,
			Count:         1,
			InvitedCode:   in.InvitedCode,
			InviteCode:    in.InviteCode,
		}
		if err = db.DB.Create(&countData).Error; err != nil {
			return nil, err
		}
	} else {
		countData.Count += 1
		if err = db.DB.Model(model.UserInvited{}).Where("id = ?", countData.ID).Updates(&countData).Error; err != nil {
			return nil, err
		}
	}
	return nil, err
}

func (a *ArtistInfoUserProvider) BindArtistId(ctx context.Context, in *artistInfoUser.BindArtistIdRequest) (*artistInfoUser.BindArtistIdResp, error) {
	var updateData = map[string]any{
		"mgmt_artist_id":  in.ArtistId,
		"mgmt_artist_uid": in.ArtistUid,
		"fdd_state":       2,
	}
	if err := db.DB.Model(model.User{}).Where("mgmt_acc_id = ?", in.UserId).Updates(&updateData).Error; err != nil {
		return &artistInfoUser.BindArtistIdResp{Error: err.Error()}, err
	}
	return nil, nil
}

func (a *ArtistInfoUserProvider) FindUser(ctx context.Context, in *artistInfoUser.FindUserRequest) (*artistInfoUser.UserInfo, error) {
	return a.artistInfoLogic.FindUser(in)
}

func (a *ArtistInfoUserProvider) UpdateUserData(ctx context.Context, in *artistInfoUser.UserInfo) (*artistInfoUser.CommonNoParams, error) {
	return a.artistInfoLogic.UpdateUserData(in)
}

func (a *ArtistInfoUserProvider) PreSaveArtistInfo(ctx context.Context, in *artistInfoUser.PreSaveArtistInfoData) (res *artistInfoUser.CommonNoParams, er error) {
	return a.artistInfoLogic.PreSaveArtistInfo(in)
}
func (a *ArtistInfoUserProvider) GetPreSaveArtistInfo(ctx context.Context, in *artistInfoUser.GetPreSaveArtistInfoRequest) (res *artistInfoUser.PreSaveArtistInfoData, err error) {
	return a.artistInfoLogic.GetPreSaveArtistInfo(in)
}
