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(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(t)

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

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

}

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)
			//}
			for _, tag := range artifact.Tags {
				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(tag.Name)
					fmt.Println(artifact.Digest)
					fmt.Println()
					DefaultHarborOperator.ArtifactDeleteOne("cmii", repoName, artifact.Digest)
				}
			}
		}
	}

	//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)
}
