package image

import (
	"bufio"
	"fmt"
	"strings"
	"testing"
	"wdd.io/agent-common/assert"
	"wdd.io/agent-common/image"
	"wdd.io/agent-common/utils"
	"wdd.io/agent-deploy/d_app"
)

func TestGetRunningContainer(t *testing.T) {

	allContainer := GetRunningContainer()

	for _, container := range allContainer {
		utils.BeautifulPrint(container)
	}

}
func TestGetAllContainer(t *testing.T) {

	allContainer := GetAllContainer()

	for _, container := range allContainer {
		utils.BeautifulPrint(container)
	}
}

func TestImageGetAll(t *testing.T) {
	imageGetAll := GetAll()

	for _, summary := range imageGetAll {
		utils.BeautifulPrint(summary)
	}
}

func TestImageGetByName(t *testing.T) {
	image := "harbor.cdcyy.com.cn/cmii/cmii-uav-gateway:4.1.6-beta"
	imageGetByName := GetByName(image)

	utils.BeautifulPrint(imageGetByName)
}

func TestImageDelete(t *testing.T) {
	image := "harbor.cdcyy.com.cn/cmii/cmii-uav-gateway:4.1.6-beta"
	imageDelete := Delete(image)

	for _, item := range imageDelete {
		utils.BeautifulPrint(item)
	}
}

func TestImagePullFromCmiiHarbor(t *testing.T) {
	image := "harbor.cdcyy.com.cn/cmii/cmii-uav-gateway:4.1.6-beta"

	pullFromCmiiHarbor := PullFromDockerHub(image)
	defer pullFromCmiiHarbor.Close()

	scanner := bufio.NewScanner(pullFromCmiiHarbor)

	for scanner.Scan() {
		line := scanner.Text()

		if strings.Contains(line, "\"status\":\"Pulling from") {
			fmt.Println(line)
		}
		if strings.Contains(line, "Status: Image is up to date for") {
			fmt.Println(line)
		}
	}

	//var fs uintptr
	//
	//tools.DisplayJSONMessagesStream(pullFromCmiiHarbor, os.Stdout, fs, true, func(message tools.JSONMessage) {
	//
	//})
}

func TestImagePushToOctopusKindHarbor(t *testing.T) {

	// re-tag
	image := "harbor.cdcyy.com.cn/cmii/cmii-uav-gateway:4.1.6-beta"
	newTag := "10.250.0.100:8033/cmii/cmii-uav-gateway:4.1.6-beta"
	target := TagFromSourceToTarget(image, newTag)
	assert.Equal(t, target, true, "image re-tag error !")

	// push
	pushResult := UploadToOctopusKindHarbor(newTag)
	defer pushResult.Close()

	scanner := bufio.NewScanner(pushResult)
	for scanner.Scan() {

	}

	fmt.Println("image push success!")
}

func TestImageLoadFromFile(t *testing.T) {

	loadFromFile := LoadFromGzipFilePath("/root/cmlc=cmii=srs=v5.0.195.tar.gz")

	assert.Equal(t, loadFromFile, true, "image load error !")
}

func TestImageSaveToTarGZ(t *testing.T) {
	image := "harbor.cdcyy.com.cn/cmii/cmii-uav-gateway:4.1.6-beta"

	imageSaveToTarGZ, _ := SaveToGzipFile(image, "/home/wdd/IdeaProjects/ProjectOctopus/cmii_operator/log")

	assert.Equal(t, imageSaveToTarGZ, true, "image save to tar gz file error !")
}

func TestImageFullNameToGzipFileName(t *testing.T) {

	test := []string{
		"bitnami/redis:6.2.6-debian-10-r0",
		"simonrupf/chronyd:0.4.3",
		"harbor.cdcyy.com.cn/cmii/cmii-rtsp-operator:v4.1.0",
		"harbor.cdcyy.com.cn/cmii/ossrs/srs:v4.0.136",
		"ossrs/srs:v4.0.136",
		"mongo:5.0",
		"bitnami/minio:2023.5.4",
		"busybox:latest",
		"busybox",
	}

	test = append(test, d_app.MiddlewareAmd64...)

	for _, s := range test {
		gzipFileName := image.ImageFullNameToGzipFileName(s)
		fmt.Println(gzipFileName)
	}
}

func TestImageTagFromSourceToTarget(t *testing.T) {

	sourceImageName := "ossrs/srs:v5.0.195"
	targetImageName := "harbor.wdd.io:8033/cmii/srs:v5.0.195"

	if TagFromSourceToTarget(sourceImageName, targetImageName) {
		pushResult := UploadToOctopusKindHarbor(targetImageName)
		defer pushResult.Close()

		scanner := bufio.NewScanner(pushResult)
		for scanner.Scan() {

		}
	}

}

func TestSaveSpecificImageToGzipFile(t *testing.T) {

	imageGzipFilePathPrefix := "/root/ai_image/"

	imageFullNameList := []string{
		"harbor.cdcyy.com.cn/cmii/cmlc-ai/cmlc-ai-operator:v5.2.0-t4-no-dino",
	}

	for _, imageFullName := range imageFullNameList {
		result := PullFromDockerHub(imageFullName)
		if result == nil {
			log.ErrorF("image pull error ! => %s", imageFullName)
			return
		}
		scc := bufio.NewScanner(result)
		for scc.Scan() {
			line := scc.Text()
			if strings.Contains(line, "\"status\":\"Pulling from") {
				fmt.Println(line)
			}
			if strings.Contains(line, "Status: Image is up to date for") {
				fmt.Println(line)
			}
		}

		// image pull success
		log.InfoF("image should have pulled successful ! => %s", imageFullName)
		ok, _ := SaveToGzipFile(imageFullName, imageGzipFilePathPrefix)
		if !ok {
			log.ErrorF("image save to gzip file error ! => %s", imageFullName)
			return
		}

	}

}

func TestConvertCmiiImageMapFromGzipFolder(t *testing.T) {

	versionMap := CmiiImageMapFromGzipFolder(OfflineImageGzipFolderPrefix)

	utils.BeautifulPrint(versionMap)
}

func TestImageNameToTargetImageFullName(t *testing.T) {

	AllCmiiImageTagList := []string{
		"harbor.cdcyy.com.cn/cmii/cmii-uav-cloud-live:5.4.0",
		"harbor.cdcyy.com.cn/cmii/cmii-uav-data-post-process:5.4.0",
		"harbor.cdcyy.com.cn/cmii/cmii-uav-depotautoreturn:5.4.0",
		"harbor.cdcyy.com.cn/cmii/cmii-uav-logger:5.4.0",
		"harbor.cdcyy.com.cn/cmii/cmii-uav-mqtthandler:5.4.0",
		"harbor.cdcyy.com.cn/cmii/cmii-uav-notice:5.4.0",
		"harbor.cdcyy.com.cn/cmii/cmii-admin-gateway:5.4.0",
		"harbor.cdcyy.com.cn/cmii/cmii-uav-cms:5.4.0",
		"harbor.cdcyy.com.cn/cmii/cmii-uav-material-warehouse:5.4.0",
		"harbor.cdcyy.com.cn/cmii/cmii-uav-process:5.4.0",
		"harbor.cdcyy.com.cn/cmii/cmii-uav-autowaypoint:5.4.0",
		"harbor.cdcyy.com.cn/cmii/cmii-uav-brain:5.4.0",
		"harbor.cdcyy.com.cn/cmii/cmii-uav-mission:5.4.0",
		"harbor.cdcyy.com.cn/cmii/cmii-uav-oauth:5.4.0",
		"harbor.cdcyy.com.cn/cmii/cmii-uav-airspace:5.4.0",
		"harbor.cdcyy.com.cn/cmii/cmii-suav-supervision:5.4.0",
		"harbor.cdcyy.com.cn/cmii/cmii-uav-user:5.4.0",
		"harbor.cdcyy.com.cn/cmii/cmii-admin-user:5.4.0",
		"harbor.cdcyy.com.cn/cmii/cmii-uav-emergency:5.4.0",
		"harbor.cdcyy.com.cn/cmii/cmii-uav-kpi-monitor:5.4.0",
		"harbor.cdcyy.com.cn/cmii/cmii-uav-waypoint:5.4.0",
		"harbor.cdcyy.com.cn/cmii/cmii-uav-device:5.4.0",
		"harbor.cdcyy.com.cn/cmii/cmii-uav-alarm:5.4.0",
		"harbor.cdcyy.com.cn/cmii/cmii-uav-clusters:5.4.0",
		"harbor.cdcyy.com.cn/cmii/cmii-uav-surveillance:5.4.0",
		"harbor.cdcyy.com.cn/cmii/cmii-uav-threedsimulation:5.4.0",
		"harbor.cdcyy.com.cn/cmii/cmii-open-gateway:5.4.0",
		"harbor.cdcyy.com.cn/cmii/cmii-uav-industrial-portfolio:5.4.0",
		"harbor.cdcyy.com.cn/cmii/cmii-uav-tower:5.4.0",
		"harbor.cdcyy.com.cn/cmii/cmii-uav-developer:5.4.0",
		"harbor.cdcyy.com.cn/cmii/cmii-app-release:5.4.0",
		"harbor.cdcyy.com.cn/cmii/cmii-uav-gateway:5.4.0",
		"harbor.cdcyy.com.cn/cmii/cmii-uav-integration:5.4.0",
		"harbor.cdcyy.com.cn/cmii/cmii-admin-data:5.4.0",
		"harbor.cdcyy.com.cn/cmii/cmii-uav-platform-armypeople:5.4.0",
		"harbor.cdcyy.com.cn/cmii/cmii-uav-platform-base:5.4.0",
		"harbor.cdcyy.com.cn/cmii/cmii-uav-platform-logistics:5.4.0",
		"harbor.cdcyy.com.cn/cmii/cmii-uav-platform:5.4.0",
		"harbor.cdcyy.com.cn/cmii/cmii-uav-platform-seniclive:5.4.0",
		"harbor.cdcyy.com.cn/cmii/cmii-uav-platform-visualization:5.4.0",
		"harbor.cdcyy.com.cn/cmii/cmii-uav-platform-cms-portal:5.4.0",
		"harbor.cdcyy.com.cn/cmii/cmii-uav-platform-multiterminal:5.4.0",
		"harbor.cdcyy.com.cn/cmii/cmii-uav-platform-security:5.4.0",
		"harbor.cdcyy.com.cn/cmii/cmii-uav-platform-media:5.4.0",
		"harbor.cdcyy.com.cn/cmii/cmii-uav-platform-oms:5.4.0",
		"harbor.cdcyy.com.cn/cmii/cmii-uav-platform-securityh5:5.4.0",
		"harbor.cdcyy.com.cn/cmii/cmii-uav-platform-share:5.4.0",
		"harbor.cdcyy.com.cn/cmii/cmii-suav-platform-supervision:5.4.0",
		"harbor.cdcyy.com.cn/cmii/cmii-uav-platform-ai-brain:5.4.0",
		"harbor.cdcyy.com.cn/cmii/cmii-uav-platform-emergency-rescue:5.4.0",
		"harbor.cdcyy.com.cn/cmii/cmii-uav-platform-splice:5.4.0",
		"harbor.cdcyy.com.cn/cmii/cmii-uav-platform-jiangsuwenlv:5.4.0",
		"harbor.cdcyy.com.cn/cmii/cmii-uav-platform-open:5.4.0",
		"harbor.cdcyy.com.cn/cmii/cmii-uav-platform-qinghaitourism:5.4.0",
		"harbor.cdcyy.com.cn/cmii/cmii-uav-platform-qingdao:5.4.0",
		"harbor.cdcyy.com.cn/cmii/cmii-uav-platform-mws:5.4.0",
		"harbor.cdcyy.com.cn/cmii/cmii-uav-platform-threedsimulation:5.4.0",
		"harbor.cdcyy.com.cn/cmii/cmii-suav-platform-supervisionh5:5.4.0",
		"harbor.cdcyy.com.cn/cmii/cmii-uav-platform-detection:5.4.0",
		"harbor.cdcyy.com.cn/cmii/cmii-uav-platform-hljtt:5.4.0",
		"harbor.cdcyy.com.cn/cmii/cmii-live-operator:5.2.0",
		"ossrs/srs:v5.0.195",
		"harbor.cdcyy.com.cn/cmii/cmii-srs-oss-adaptor:2023-SA",
	}

	AllCmiiImageTagList = append(AllCmiiImageTagList, d_app.Rancher1204Amd64...)
	AllCmiiImageTagList = append(AllCmiiImageTagList, d_app.MiddlewareAmd64...)

	var result []string
	for _, imageName := range AllCmiiImageTagList {
		targetImageFullName := image.ImageNameToTargetImageFullName(imageName, "cq.com")
		result = append(result, targetImageFullName)
	}

	utils.BeautifulPrint(result)
}

func TestSaveImageListToGzipFile(t *testing.T) {

	allImageList := []string{
		"harbor.cdcyy.com.cn/cmii/cmii-uav-oauth:5.7.0",
		"harbor.cdcyy.com.cn/cmii/cmii-iam-gateway:5.6.0",
		"harbor.cdcyy.com.cn/cmii/cmii-uav-kpi-monitor:5.5.0",
	}

	gzipOK, gzipFileFullPath, errorGzipImageList := SaveImageListToGzipFile(allImageList, "/home/wdd/IdeaProjects/ProjectOctopus/agent-operator/image", "test.tar.gz")

	assert.Equal(t, gzipOK, true, "gzip image list to single file failed !")

	log.Info("gzip file is " + gzipFileFullPath)

	utils.BeautifulPrint(errorGzipImageList)
}

func TestImagePruneAllCmiiImages(t *testing.T) {

	errorRemoveImageNameList := PruneAllCmiiImages()

	utils.BeautifulPrintListWithTitle(errorRemoveImageNameList, "CMII Image Prune Error")
}
