package image

import (
	"bufio"
	"fmt"
	"strconv"
	"strings"
	"testing"
	"time"
	"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 := 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 := PullFromCmiiHarbor(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 := PushToOctopusKindHarbor(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 := SaveToTarGZ(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/mysql:8.1.0-debian-11-r42",
		"simonrupf/chronyd:0.4.3",
		"bitnami/bitnami-shell:11-debian-11-r136",
		"harbor.cdcyy.com.cn/cmii/cmii-rtsp-operator:v4.1.0",
		"harbor.cdcyy.com.cn/cmii/ossrs/srs:v4.0.136",
		"bitnami/rabbitmq:3.9.12-debian-10-r3",
		"ossrs/srs:v4.0.136",
		"emqx/emqx:4.2.12",
		"nacos/nacos-server:v2.1.2",
		"mongo:5.0",
		"bitnami/minio:2023.5.4",
		"kubernetesui/metrics-scraper:v1.0.4",
		"redis:6.0.20-alpine",
		"dyrnq/nfs-subdir-external-provisioner:v4.0.2",
		"busybox:latest",
		"busybox",
	}

	for _, s := range test {
		gzipFileName := convertImageGzipFileName(s)
		fmt.Printf(" %s to %s \n", s, gzipFileName)
	}
}

func TestImagePruneAllCmiiImages(t *testing.T) {

	errorRemoveImageNameList := PruneAllCmiiImages()

	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 TagFromSourceToTarget(sourceImageName, targetImageName) {
		pushResult := PushToOctopusKindHarbor(targetImageName)
		defer pushResult.Close()

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

		}
	}

}

func fibonacci(c, quit chan int64) {
	x, y := int64(0), int64(1)
	for {
		select {
		case c <- x:
			x, y = y, x+y
			fmt.Println("count is " + strconv.FormatInt(int64(<-c), 10))
		case <-quit:
			fmt.Println("quit current x is " + strconv.FormatInt(int64(x), 10))
			return
		}
	}
}
func TestWriteDependencyImageToFile(t *testing.T) {
	//WriteDependencyImageToFile()

	c := make(chan int64, 1)
	quit := make(chan int64, 1)
	go fibonacci(c, quit)

	after := time.After(time.Second)

	<-after
	quit <- 1
}
