package controller

import (
	"context"
	"errors"
	"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) GetInviteStaticList(ctx context.Context, request *artistInfoUser.GetInviteStaticListRequest) (*artistInfoUser.GetInviteStaticListResponse, error) {
	return a.artistInfoLogic.GetInviteStaticList(request)
}

func (a *ArtistInfoUserProvider) GetInviterUserList(ctx context.Context, request *artistInfoUser.GetInviterUserListRequest) (*artistInfoUser.GetInvitedUserListResponse, error) {
	return a.artistInfoLogic.GetInviterUserList(request)
}

func (a *ArtistInfoUserProvider) GetInvitedUserList(ctx context.Context, request *artistInfoUser.GetInvitedUserListRequest) (*artistInfoUser.GetInvitedUserListResponse, error) {
	return a.artistInfoLogic.GetInvitedUserList(request)
}

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("第一处")
	//新增邀请码判断 by JJXu 2023-03-07
	//if req.UserInviteCode == "" {
	//	return nil, errors.New("邀请码不能为空")
	//}
	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.InvitedUserId == 0 {
		var thisUser model.User
		if err = db.DB.Where("id = ? AND invite_code = ?", in.InvitedUserId, in.InviteCode).First(&thisUser).Error; err != nil {
			if err == gorm.ErrRecordNotFound {
				return nil, errors.New("受邀请人不存在")
			} else {
				return nil, err
			}
		}
	}
	var data model.Invite
	// 受邀请者只能绑定一个邀请人
	if err = db.DB.Where("invited_id = ?", in.InvitedUserId).Find(&data).Error; err != nil {
		return nil, err
	}
	if data.ID == 0 {
		data = model.Invite{
			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("invite_code = ? AND invited_code =?", in.InviteCode, in.InvitedCode).Find(&countData).Error; err != nil {
		return nil, err
	}
	if countData.ID == 0 {
		countData = model.UserInvited{
			InvitedUserId: in.InvitedUserId, //受邀请人的画家宝用户id
			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).Update("count", countData.Count).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)
}