package image

import (
	"fmt"
	"strings"
	"testing"

	"wdd.io/agent-common/utils"
)

var DefaultHarborOperator *HarborOperator

func TestHarborOperator_BuildOperator_CMII(t *testing.T) {
	harborOperator := &HarborOperator{
		HarborHost:   "http://harbor.cdcyy.com.cn",
		HarborPort:   "",
		HarborUser:   "rad02_drone",
		HarborPass:   "Drone@1234",
		HarborClient: nil,
	}

	_, err := harborOperator.BuildOperator()
	if err != nil {
		t.Logf("error is %s", err.Error())
		return
	}

	DefaultHarborOperator = harborOperator

}

func TestHarborOperator_BuildOperator(t *testing.T) {
	harborOperator := &HarborOperator{
		HarborHost:   "http://harbor.wdd.io",
		HarborPort:   "8033",
		HarborUser:   "admin",
		HarborPass:   "V2ryStr@ngPss",
		HarborClient: nil,
	}

	_, err := harborOperator.BuildOperator()
	if err != nil {
		t.Logf("error is %s", err.Error())
		return
	}

	DefaultHarborOperator = harborOperator

}

func TestHarborOperator_RepoListAll(t *testing.T) {

	TestHarborOperator_BuildOperator_CMII(t)

	//repoListAll := DefaultHarborOperator.RepoListAll("ran")
	repoListAll := DefaultHarborOperator.RepoListAll("cmii")

	utils.BeautifulPrint(repoListAll)

}

func TestHarborOperator_RepoAllCmiiImage(t *testing.T) {

	TestHarborOperator_BuildOperator(t)

	repoListAll := DefaultHarborOperator.RepoAllCmiiImage()

	utils.BeautifulPrint(repoListAll)
}

func TestHarborOperator_ArtifactListAll(t *testing.T) {

	TestHarborOperator_BuildOperator_CMII(t)

	artifactListAll := DefaultHarborOperator.ArtifactListAll("cmii", "cmii-uav-user")

	var tagNameList []string

	for _, artifact := range artifactListAll {
		//if len(artifact.Tags) == 0 {
		//	fmt.Println(artifact.Digest)
		//}
		for _, tag := range artifact.Tags {
			fmt.Println(tag.Name)
			tagNameList = append(tagNameList, tag.Name)
		}
	}
	//utils.BeautifulPrint(artifactListAll)

	toDelete, keep := FindTagsToDelete(tagNameList)

	fmt.Printf("总共有 %d 个标签, 其中 %d 个可以被删除, 需要保留 %d 个。\n\n", len(tagNameList), len(toDelete), len(keep))
	fmt.Println("建议删除的标签列表:")
	for _, tag := range toDelete {
		fmt.Println(tag)
	}

}

func TestHarborOperator_CmiiHarborCleanUp(t *testing.T) {
	TestHarborOperator_BuildOperator_CMII(t)

	//repoListAll := DefaultHarborOperator.RepoListAll("ran")
	repoListAll := DefaultHarborOperator.RepoListAll("cmii")

	for _, repo := range repoListAll {
		repoName := strings.TrimPrefix(repo.Name, "cmii/")

		// 获取所有的
		artifactListAll := DefaultHarborOperator.ArtifactListAll("cmii", repoName)

		for _, artifact := range artifactListAll {
			//if len(artifact.Tags) == 0 {
			//	fmt.Println(artifact.Digest)
			//}
			var tagNameList []string

			for _, tag := range artifact.Tags {

				// 添加到Tag列表中
				tagNameList = append(tagNameList, tag.Name)

				if strings.Contains(tag.Name, "snapshot") || strings.Contains(tag.Name, "validation") || strings.Contains(tag.Name, "beta") || strings.Contains(tag.Name, "staging") || strings.Contains(tag.Name, "test") {

					fmt.Println(fmt.Sprintf("prepare delete image is %s:%s", repoName, tag.Name))

					//DefaultHarborOperator.ArtifactDeleteOne("cmii", repoName, artifact.Digest)

					fmt.Println()
				}
			}

			// 过滤所有可以删除的Tag
			//deletableTags := FilterTags(tagNameList)
			//
			//fmt.Printf("总共有 %d 个标签, 其中 %d 个可以被删除。\n\n", len(tagNameList), len(deletableTags))
			//fmt.Println("建议删除的标签列表:")
			//for _, tag := range deletableTags {
			//    fmt.Println(fmt.Sprintf("开始删除镜像 => %s:%s", repoName, tag))
			//    DefaultHarborOperator.ArtifactDeleteOne("cmii", repoName, tag)
			//}

		}

	}

	//utils.BeautifulPrint(artifactListAll)

}

func TestHarborOperator_ArtifactListOne(t *testing.T) {
	TestHarborOperator_BuildOperator(t)

	//reference := "sha256:0048162a053eef4d4ce3fe7518615bef084403614f8bca43b40ae2e762e11e06" // not ok icon
	reference := "sha256:27bd0055156abc20c29863750f13bbcc14019126da36d3941cfd82eb104ec31a" // ok digest
	// reference := "5.2.0" // ok tag
	artifactListOne := DefaultHarborOperator.ArtifactListOne("cmii", "cmii-uav-user", reference)

	utils.BeautifulPrint(artifactListOne)
}

func TestHarborOperator_ArtifactDeleteOne(t *testing.T) {
	TestHarborOperator_BuildOperator(t)
	reference := "sha256:27bd0055156abc20c29863750f13bbcc14019126da36d3941cfd82eb104ec31a"
	ccc := DefaultHarborOperator.ArtifactDeleteOne("cmii", "cmii-uav-user", reference)
	if !ccc {
		log.Error("delete failed")
	}
}

func TestHarborOperator_CmiiTagFilter(t *testing.T) {
	TestHarborOperator_BuildOperator_CMII(t)
	imageMap := DefaultHarborOperator.CmiiTagFilter("5.7")
	utils.BeautifulPrint(imageMap)
}

func TestHarborOperator_ArtifactDeleteFromNameTagList(t *testing.T) {

	TestHarborOperator_BuildOperator(t)
	allCmiiImageList := DefaultHarborOperator.CmiiTagFilter("5.5")
	errorDeleteList := DefaultHarborOperator.ArtifactDeleteFromNameTagList("cmii", allCmiiImageList)
	utils.BeautifulPrint(errorDeleteList)
}
