package svc

import (
	"context"
	"git.noahlan.cn/n-admin/n-admin-server/api/internal/types"
	"git.noahlan.cn/n-admin/n-admin-server/rpc/core/types/core"
	"github.com/jinzhu/copier"
)

type Convert struct {
	svcCtx *ServiceContext
}

func NewConvert(svcCtx *ServiceContext) *Convert {
	return &Convert{
		svcCtx: svcCtx,
	}
}

func (c *Convert) ConvertPaginationRpc(page *types.Pagination) *core.Pagination {
	if page == nil {
		return nil
	}
	return &core.Pagination{
		Size:    page.Size,
		Total:   page.Total,
		Current: page.Current,
	}
}

func (c *Convert) ConvertPaginationApi(page *core.Pagination) *types.Pagination {
	if page == nil {
		return nil
	}
	return &types.Pagination{
		Size:    page.Size,
		Current: page.Current,
		Total:   page.Total,
	}
}

func (c *Convert) ConvertRole(_ context.Context, v *core.RoleInfo) *types.RoleInfo {
	if v == nil {
		return nil
	}
	var tmp types.RoleInfo
	_ = copier.Copy(&tmp, v)
	return &tmp
}

func (c *Convert) ConvertUser(ctx context.Context, v *core.UserInfo) *types.UserInfo {
	if v == nil {
		return nil
	}
	var tmp types.UserInfo
	_ = copier.Copy(&tmp, v)
	tmp.Password = "***"

	// Metadata
	if len(v.Metas) > 0 {
		// 导出meta数据
		metaMap := make(map[string]string)
		for key, val := range v.Metas {
			metaMap[key] = val
		}
		tmp.Metas = metaMap
	}

	// Relationship
	if len(v.Roles) > 0 {
		tmp.Roles = make([]string, len(v.Roles))
		for i, item := range v.Roles {
			tmp.Roles[i] = item.Code
		}
	}
	if v.LoginRecord != nil {
		tmp.LoginRecord = c.ConvertLoginRecord(ctx, v.LoginRecord)
	}

	//if len(v.Departments) > 0 {
	//	tmp.Departments = make([]*core.DepartmentInfo, len(v.Edges.Departments))
	//	for i, item := range v.Edges.Departments {
	//		tmp.Departments[i] = c.ConvertDepartment(ctx, item)
	//	}
	//}
	//if len(v.Token) > 0 {
	//	tmp.Tokens = make([]*core.TokenInfo, len(v.Edges.Token))
	//	for i, item := range v.Edges.Token {
	//		tmp.Tokens[i] = c.ConvertToken(ctx, item)
	//	}
	//}
	//if len(v.Socials) > 0 {
	//	tmp.Socials = make([]*core.UserSocialInfo, len(v.Edges.Socials))
	//	for i, item := range v.Edges.Socials {
	//		tmp.Socials[i] = c.ConvertUserSocial(ctx, item)
	//	}
	//}

	return &tmp
}

func (c *Convert) ConvertToken(_ context.Context, v *core.TokenInfo) *types.TokenInfo {
	if v == nil {
		return nil
	}
	var tmp types.TokenInfo
	_ = copier.Copy(&tmp, v)

	//if v.User != nil {
	//	tmp.User = c.ConvertUser(ctx, v.Edges.User)
	//}

	return &tmp
}

func (c *Convert) ConvertUserSocial(_ context.Context, v *core.UserSocialInfo) *types.UserSocialInfo {
	if v == nil {
		return nil
	}
	var tmp types.UserSocialInfo
	_ = copier.Copy(&tmp, v)

	//if v.User != nil {
	//	tmp.User = c.ConvertUser(ctx, v.Edges.User)
	//}

	return &tmp
}

func (c *Convert) ConvertLoginRecord(_ context.Context, v *core.LoginRecordInfo) *types.UserLoginRecordInfo {
	if v == nil {
		return nil
	}
	var tmp types.UserLoginRecordInfo
	_ = copier.Copy(&tmp, v)

	return &tmp
}

func (c *Convert) ConvertDepartment(ctx context.Context, v *core.DepartmentInfo) *types.DepartmentInfo {
	if v == nil {
		return nil
	}
	var tmp types.DepartmentInfo
	_ = copier.Copy(&tmp, v)

	// relationship
	if v.Parent != nil {
		tmp.Parent = c.ConvertDepartment(ctx, v.Parent)
	}
	if len(v.Children) > 0 {
		tmp.Children = make([]*types.DepartmentInfo, len(v.Children))
		for i, item := range v.Children {
			tmp.Children[i] = c.ConvertDepartment(ctx, item)
		}
	}
	//if len(v.Edges.Users) > 0 {
	//	tmp.Users = make([]*core.UserInfo, len(v.Edges.Users))
	//	for i, item := range v.Edges.Users {
	//		tmp.Users[i] = c.ConvertUser(ctx, item)
	//	}
	//}
	if v.Leader != nil {
		tmp.Leader = c.ConvertUser(ctx, v.Leader)
	}

	return &tmp
}

func (c *Convert) ConvertOAuthProvider(_ context.Context, v *core.OauthProviderInfo) *types.OauthProviderInfo {
	if v == nil {
		return nil
	}
	var tmp types.OauthProviderInfo
	_ = copier.Copy(&tmp, v)

	return &tmp
}

func (c *Convert) ConvertDistrict(_ context.Context, v *core.DistrictInfo) *types.DistrictInfo {
	if v == nil {
		return nil
	}
	var tmp types.DistrictInfo
	_ = copier.Copy(&tmp, v)
	tmp.Children = make([]*types.DistrictInfo, len(v.Children))

	return &tmp
}

func (c *Convert) ConvertDictionary(ctx context.Context, v *core.DictionaryInfo) *types.DictionaryInfo {
	if v == nil {
		return nil
	}
	var tmp types.DictionaryInfo
	_ = copier.Copy(&tmp, v)

	if len(v.Details) > 0 {
		tmp.Details = make([]*types.DictionaryDetailInfo, len(v.Details))
		for i, item := range v.Details {
			tmp.Details[i] = c.ConvertDictionaryDetail(ctx, item)
		}
	}
	return &tmp
}

func (c *Convert) ConvertDictionaryDetail(ctx context.Context, v *core.DictionaryDetailInfo) *types.DictionaryDetailInfo {
	if v == nil {
		return nil
	}
	var tmp types.DictionaryDetailInfo
	_ = copier.Copy(&tmp, v)

	// relationship
	if v.Dictionary != nil {
		tmp.Dictionary = c.ConvertDictionary(ctx, v.Dictionary)
	}
	return &tmp
}
