package api

import (
	"baishuihu/db/model"
	"baishuihu/db/repo"
	"fmt"
	"strings"

	"github.com/gin-gonic/gin"
)

func Printr(c *gin.Context, apictx *ApiSession) (interface{}, error) {
	map1 := map[string]string{"a": "1234"}
	println(map1["b"] == "")
	println(map1["a"])
	return nil, nil
}

func SyncType(c *gin.Context, apictx *ApiSession) (interface{}, error) {

	articles := []*model.Article{}
	err := repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
		CollectName: repo.CollectionArticle,
		Project:     []string{"cid"},
	}, &articles)
	fmt.Println(len(articles))
	for _, article := range articles {
		cid := article.Cid
		category := model.Category{}
		found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
			CollectName: repo.CollectionCategory,
			Query:       repo.Map{"_id": cid},
			Project:     []string{"type"},
		}, &category)
		if err != nil {
			continue
		}
		if found {
			repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionArticle, article.Id.Hex(), &model.Article{Type: category.Type})
		}

	}

	return articles, err

}

var originStatic string = "https://www.infish.cn/adhuaxi/v1/public"
var targetStatic string = "https://3dqueen.cloud/baishuihu/v1/public"

func SyncStaticAddr(c *gin.Context, apictx *ApiSession) (interface{}, error) {
	articles := []*model.Article{}
	repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
		CollectName: repo.CollectionArticle,
		Project:     []string{"cover", "content"},
	}, &articles)
	fmt.Println(len(articles))
	for _, article := range articles {
		cover := article.Cover
		content := article.Content
		coverNew := strings.Replace(cover, originStatic, targetStatic, -1)
		contentNew := strings.Replace(content, originStatic, targetStatic, -1)
		repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionArticle, article.Id.Hex(), &model.Article{Cover: coverNew, Content: contentNew})
	}

	banners := []*model.Banner{}
	repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
		CollectName: repo.CollectionBanner,
		Project:     []string{"cover"},
	}, &banners)
	fmt.Println(len(banners))
	for _, banner := range banners {
		cover := banner.Cover
		coverNew := strings.Replace(cover, originStatic, targetStatic, -1)
		repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionBanner, banner.Id.Hex(), &model.Banner{Cover: coverNew})
	}

	categorys := []*model.Category{}
	repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
		CollectName: repo.CollectionCategory,
		Project:     []string{"cover"},
	}, &banners)
	fmt.Println(len(categorys))
	for _, category := range categorys {
		cover := category.Cover
		fmt.Println(cover)
		coverNew := strings.Replace(cover, originStatic, targetStatic, -1)
		repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionCategory, category.Id.Hex(), &model.Category{Cover: coverNew})
	}

	return articles, nil

}
