package logic

import (
	"context"
	"fmt"
	"github.com/fonchain/fonchain-artistinfo/cmd/internal/dao"
	"github.com/fonchain/fonchain-artistinfo/pb/artist"
	"github.com/fonchain/fonchain-artistinfo/pb/artistInfoUser"
	"github.com/fonchain/fonchain-artistinfo/pkg/service"
)

type IArtistInfo interface {
	RegisterUser(req *artistInfoUser.RegisterUserRequest) (rep *artistInfoUser.RegisterUserRespond, err error)
	GetUserById(req *artistInfoUser.GetUserByIdRequest) (rep *artistInfoUser.GetUserByIdRespond, err error)
	GetUser(req *artistInfoUser.GetUserRequest) (rep *artistInfoUser.GetUserRespond, err error)
	CreateUserInfo(req *artistInfoUser.CreateUserInfoRequest) (rep *artistInfoUser.CreateUserInfoRespond, err error)
	//UpdateRealName(req *artistInfoUser.UpdateRealNameRequest) (rep *artistInfoUser.UpdateRealNameRespond, err error)
	FinishVerify(req *artistInfoUser.FinishVerifyRequest) (rep *artistInfoUser.FinishVerifyRespond, err error)
	CheckUserLock(req *artistInfoUser.CheckUserLockRequest) (rep *artistInfoUser.CheckUserLockRespond, err error)
	ArtistSupplyList(req *artistInfoUser.ArtistSupplyListRequest) (rep *artistInfoUser.ArtistSupplyListRespond, err error)
	FindUser(req *artistInfoUser.FindUserRequest) (rep *artistInfoUser.UserInfo, err error)
	UpdateUserData(req *artistInfoUser.UserInfo) (rep *artistInfoUser.CommonNoParams, err error)
}

func NewArtistInfo() IArtistInfo {
	return &ArtistInfoUser{}
}

type ArtistInfoUser struct{}

func (a *ArtistInfoUser) GetUser(req *artistInfoUser.GetUserRequest) (rep *artistInfoUser.GetUserRespond, err error) {
	rep, err = dao.GetUser(req)
	return
}

func (a *ArtistInfoUser) RegisterUser(req *artistInfoUser.RegisterUserRequest) (rep *artistInfoUser.RegisterUserRespond, err error) {
	rep, err = dao.RegisterUser(req)
	return
}
func (a *ArtistInfoUser) CreateUserInfo(req *artistInfoUser.CreateUserInfoRequest) (rep *artistInfoUser.CreateUserInfoRespond, err error) {
	fmt.Println("第二处")
	rep, err = dao.CreateUserInfo(req)
	return
}

func (a *ArtistInfoUser) GetUserById(req *artistInfoUser.GetUserByIdRequest) (userInfo *artistInfoUser.GetUserByIdRespond, err error) {
	userInfo, err = dao.GetUserById(req)
	return
}

//func (a *ArtistInfoUser) UpdateRealName(req *artistInfoUser.UpdateRealNameRequest) (rep *artistInfoUser.UpdateRealNameRespond, err error) {
//	rep, err = dao.UpdateRealName(req)
//	return
//}

func (a *ArtistInfoUser) FinishVerify(req *artistInfoUser.FinishVerifyRequest) (rep *artistInfoUser.FinishVerifyRespond, err error) {
	rep, err = dao.FinishVerify(req)
	return
}

func (a *ArtistInfoUser) CheckUserLock(req *artistInfoUser.CheckUserLockRequest) (rep *artistInfoUser.CheckUserLockRespond, err error) {
	err = dao.CheckUserLock(int64(req.Id))
	return
}
func (a *ArtistInfoUser) ArtistSupplyList(req *artistInfoUser.ArtistSupplyListRequest) (rep *artistInfoUser.ArtistSupplyListRespond, err error) {
	rep, err = dao.ArtistSupplyList(req)
	return
}
func (a *ArtistInfoUser) UserLock(req *artistInfoUser.UserLockRequest) (rep *artistInfoUser.UserLockRespond, err error) {
	rep, err = dao.UserLock(req)
	return
}
func (a *ArtistInfoUser) CheckInvitedCode(req *artistInfoUser.CheckInvitedCodeRequest) (rep *artistInfoUser.GetUserRespond, err error) {
	rep, err = dao.CheckInvitedCode(req)
	return
}

func (a *ArtistInfoUser) UnFinishList(req *artistInfoUser.UnFinishListRequest) (rep *artistInfoUser.UnFinishListRespond, err error) {
	rep, err = dao.UnFinishList(req)
	return
}

func (a *ArtistInfoUser) GetUserMsg(req *artistInfoUser.GetUserMsgRequest) (rep *artistInfoUser.GetUserMsgRespond, err error) {
	rep, err = dao.GetUserMsg(req)
	return
}

func (a *ArtistInfoUser) FindUser(req *artistInfoUser.FindUserRequest) (rep *artistInfoUser.UserInfo, err error) {
	return dao.FindUser(req)
}
func (a *ArtistInfoUser) FindUserList(req *artistInfoUser.FindUsersRequest) (rep *artistInfoUser.FindUsersResponse, err error) {
	res, total, err := dao.FindUserList(req)
	rep = &artistInfoUser.FindUsersResponse{Data: res, Page: &artistInfoUser.UserCommonPageInfo{
		Page:     req.Page,
		PageSize: req.PageSize,
		Total:    total,
	}}
	return
}
func (a *ArtistInfoUser) FindUsersUserView(req *artistInfoUser.FindUsersRequest) (rep *artistInfoUser.FindUsersUserViewResponse, err error) {
	res, total, err := dao.GetViewUserList(req)
	var returnData []*artistInfoUser.UserView
	var artistUidList = []string{}

	for _, v := range res {
		if v.ArtistUid != "" {
			artistUidList = append(artistUidList, v.ArtistUid)
		}
		returnData = append(returnData, &artistInfoUser.UserView{
			UserId:            v.UserId,
			AccId:             v.AccId,
			ArtistUid:         v.ArtistUid,
			TelNum:            v.TelNum,
			InviteCode:        v.InviteCode,
			Account:           v.Account,
			Photo:             v.Photo,
			IsRealName:        v.IsRealName,
			FddState:          v.FddState,
			IsRead:            v.IsRead,
			IsLock:            v.IsLock,
			RealName:          v.RealName,
			IdNum:             v.IdNum,
			Sex:               string(v.Sex),
			Age:               v.Age,
			Address:           v.RealAddress,
			IdcardBack:        v.IdcardBack,
			IdcardFront:       v.IdcardFront,
			InviterInviteCode: v.InviterInviteCode,
			InviterRealName:   v.InviterRealName,
			DeletedAt:         v.DeletedAt,
			UpdatedAt:         v.UpdatedAt.Unix(),
			CreatedAt:         v.CreatedAt.Unix(),
			LatestLockTime:    v.LatestLockTime,
		})
	}
	if artistUidList != nil {
		var artistDatas *artist.ArtistListResponse
		artistDatas, err = service.GrpcArtistImpl.ArtistList(context.Background(), &artist.ArtistListRequest{
			Page:     1,
			PageSize: int32(len(artistUidList)),
			Uids:     artistUidList,
		})
		if err != nil {
			fmt.Println("错误：GrpcArtistImpl.GetInfoByUuids ", err.Error())
			return nil, err
		}
		for _, artistValue := range artistDatas.Data {
			for i, user := range returnData {
				if artistValue.Uid == user.ArtistUid {
					returnData[i].CertificateNum = artistValue.CaaCertNum
					returnData[i].OpenBank = artistValue.BankNum
					returnData[i].PenName = artistValue.PenName
					returnData[i].StageName = artistValue.StageName
					break
				}
			}
		}

	}
	rep = &artistInfoUser.FindUsersUserViewResponse{Data: returnData, Page: &artistInfoUser.UserCommonPageInfo{
		Page:     req.Page,
		PageSize: req.PageSize,
		Total:    total,
	}}
	return
}
func (a *ArtistInfoUser) UpdateUserData(req *artistInfoUser.UserInfo) (rep *artistInfoUser.CommonNoParams, err error) {
	rep, err = dao.UpdateUserData(req)
	return
}
func (a *ArtistInfoUser) PreSaveArtistInfo(in *artistInfoUser.PreSaveArtistInfoData) (rep *artistInfoUser.CommonNoParams, err error) {
	rep, err = dao.PreSaveArtistInfo(in)
	return
}
func (a *ArtistInfoUser) GetPreSaveArtistInfo(in *artistInfoUser.GetPreSaveArtistInfoRequest) (res *artistInfoUser.PreSaveArtistInfoData, err error) {
	res, err = dao.GetPreSaveArtistInfo(in)
	return
}
