package blogger

import (
	"github.com/ArtisanCloud/MediaX/pkg/client/config"
	"github.com/ArtisanCloud/MediaX/pkg/client/google/blogger/accessTokenClient/blogUserInfos"
	"github.com/ArtisanCloud/MediaX/pkg/client/google/blogger/accessTokenClient/blogs"
	"github.com/ArtisanCloud/MediaX/pkg/client/google/blogger/accessTokenClient/comments"
	"github.com/ArtisanCloud/MediaX/pkg/client/google/blogger/accessTokenClient/pageViews"
	"github.com/ArtisanCloud/MediaX/pkg/client/google/blogger/accessTokenClient/pages"
	"github.com/ArtisanCloud/MediaX/pkg/client/google/blogger/accessTokenClient/postUserInfos"
	"github.com/ArtisanCloud/MediaX/pkg/client/google/blogger/accessTokenClient/posts"
	"github.com/ArtisanCloud/MediaX/pkg/client/google/blogger/accessTokenClient/users"
	"github.com/ArtisanCloud/MediaX/pkg/client/google/core"
	"github.com/ArtisanCloud/MediaXCore/pkg/cache"
	"github.com/ArtisanCloud/MediaXCore/pkg/logger"
)

type GoogleBloggerClient struct {
	// Logger             *logger.Logger
	// Cache              cache.ICache
	GoogleClient       *core.GoogleClient
	BloggerConfig      *config.GoogleBloggerConfig
	AccessTokenHandler *core.GoogleAccessTokenHandler

	// clients
	blogs         *blogs.BloggerBlogsClient
	blogUserInfos *blogUserInfos.BloggerBlogUserInfosClient
	comments      *comments.BloggerCommentsClient
	pages         *pages.BloggerPagesClient
	pageViews     *pageViews.BloggerPageViewsClient
	posts         *posts.BloggerPostsClient
	postUserInfos *postUserInfos.BloggerPostUserInfosClient
	users         *users.BloggerUsersClient
}

func NewGoogleBloggerClient(cfg *config.GoogleBloggerConfig, logger *logger.Logger, cache cache.ICache) (*GoogleBloggerClient, error) {
	if cfg.ApiUrl == "" {
		cfg.ApiUrl = config.GoogleAppAPIUrl
	}
	c, err := core.NewGoogleClient(cfg.ClientConfig, logger, cache)
	if err != nil {
		return nil, err
	}

	handler, err := core.NewGoogleAccessTokenHandler(cfg.ClientConfig, logger, cache)
	if err != nil {
		return nil, err
	}

	// bind token handler to client
	c.TokenHandler = handler.AccessTokenHandler

	// override get custom token
	c.TokenHandler.GetCustomToken = cfg.GetOAuthToken

	return &GoogleBloggerClient{
		// Logger:             logger,
		// Cache:              cache,
		GoogleClient:       c,
		BloggerConfig:      cfg,
		AccessTokenHandler: handler,
	}, nil
}

// GetBlogsClient 获取博客客户端
func (client *GoogleBloggerClient) GetBlogsClient() *blogs.BloggerBlogsClient {
	if client.blogs == nil {
		client.blogs = blogs.NewClient(client.GoogleClient.BaseClient)
	}
	return client.blogs
}

// GetBlogUserInfosClient 获取博客用户信息客户端
func (client *GoogleBloggerClient) GetBlogUserInfosClient() *blogUserInfos.BloggerBlogUserInfosClient {
	if client.blogUserInfos == nil {
		client.blogUserInfos = blogUserInfos.NewClient(client.GoogleClient.BaseClient)
	}
	return client.blogUserInfos
}

// GetCommentsClient 获取评论客户端
func (client *GoogleBloggerClient) GetCommentsClient() *comments.BloggerCommentsClient {
	if client.comments == nil {
		client.comments = comments.NewClient(client.GoogleClient.BaseClient)
	}
	return client.comments
}

// GetPageViewsClient 获取页面视图客户端
func (client *GoogleBloggerClient) GetPageViewsClient() *pageViews.BloggerPageViewsClient {
	if client.pageViews == nil {
		client.pageViews = pageViews.NewClient(client.GoogleClient.BaseClient)
	}
	return client.pageViews
}

// GetPagesClient 获取页面客户端
func (client *GoogleBloggerClient) GetPagesClient() *pages.BloggerPagesClient {
	if client.pages == nil {
		client.pages = pages.NewClient(client.GoogleClient.BaseClient)
	}
	return client.pages
}

// GetPostUserInfosClient 获取文章用户信息客户端
func (client *GoogleBloggerClient) GetPostUserInfosClient() *postUserInfos.BloggerPostUserInfosClient {
	if client.postUserInfos == nil {
		client.postUserInfos = postUserInfos.NewClient(client.GoogleClient.BaseClient)
	}
	return client.postUserInfos
}

// GetPostsClient 获取文章客户端
func (client *GoogleBloggerClient) GetPostsClient() *posts.BloggerPostsClient {
	if client.posts == nil {
		client.posts = posts.NewClient(client.GoogleClient.BaseClient)
	}
	return client.posts
}

// GetUsersClient 获取用户客户端
func (client *GoogleBloggerClient) GetUsersClient() *users.BloggerUsersClient {
	if client.users == nil {
		client.users = users.NewClient(client.GoogleClient.BaseClient)
	}
	return client.users
}
