package cmii_operator

import (
	"bufio"
	"fmt"
	"strings"
	"testing"
	"wdd.io/agent-go/assert"
	"wdd.io/agent-go/utils"
)

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 := ImageGetAll()

	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 := ImageGetByName(image)

	utils.BeautifulPrint(imageGetByName)
}

func TestImageDelete(t *testing.T) {
	image := "harbor.cdcyy.com.cn/cmii/cmii-uav-gateway:4.1.6-beta"
	imageDelete := ImageDelete(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 := ImagePullFromCmiiHarbor(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 := ImageTagFromSourceToTarget(image, newTag)
	assert.Equal(t, target, true, "image re-tag error !")

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

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

	}

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

func TestImageLoadFromFile(t *testing.T) {
	loadFromFile := ImageLoadFromFile("/root/octopus_image/cqga/cqga/cmlc=cmii=cmii-uav-user=5.2.0.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 := ImageSaveToTarGZ(image, "/home/wdd/IdeaProjects/ProjectOctopus/cmii_operator/log")

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

func TestConvertImageGzipFileName(t *testing.T) {

	test := []string{
		"bitnami/redis:6.2.6-debian-10-r0",
		"bitnami/redis:6.2.14-debian-11-r1",
		"bitnami/mysql:8.0.35-debian-11-r1",
		"bitnami/mysql:8.1.0-debian-11-r42",
		"simonrupf/chronyd:0.4.3",
		"bitnami/bitnami-shell:10-debian-10-r140",
		"bitnami/bitnami-shell:11-debian-11-r136",
		"bitnami/rabbitmq:3.9.12-debian-10-r3",
		"bitnami/rabbitmq:3.11.26-debian-11-r2",
		"ossrs/srs:v4.0.136",
		"emqx/emqx:4.2.12",
		"nacos/nacos-server:v2.1.2",
		"nacos/nacos-server:v2.1.2-slim",
		"mongo:5.0",
		"rabbitmq:3.9-management",
		"bitnami/minio:2022.5.4",
		"bitnami/minio:2023.5.4",
		"kubernetesui/dashboard:v2.0.1",
		"kubernetesui/metrics-scraper:v1.0.4",
		"ossrs/srs:v4.0-r3",
		"nginx:1.21.3",
		"redis:6.0.20-alpine",
		"dyrnq/nfs-subdir-external-provisioner:v4.0.2",
		"busybox:latest",
		"busybox",
	}

	for _, s := range test {
		gzipFileName := convertImageGzipFileName(s)
		t.Logf(" %s to %s", s, gzipFileName)
	}
}

func TestImagePruneAllCmiiImages(t *testing.T) {

	errorRemoveImageNameList := ImagePruneAllCmiiImages()

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

func TestImageTagFromSourceToTarget(t *testing.T) {

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

	if ImageTagFromSourceToTarget(sourceImageName, targetImageName) {
		pushResult := ImagePushToOctopusKindHarbor(targetImageName)
		defer pushResult.Close()

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

		}
	}

}
