package image

import (
	"fmt"
	"os"
	"path/filepath"
	"strings"
	"testing"
	"wdd.io/agent-common/real_project/zhejianyidong_erjipingtai"
	"wdd.io/agent-common/real_project/zjjt"
	"wdd.io/agent-common/utils"
)

var imageFullNameList = []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",
	"rancher/rancher:v2.7.0",
	"10.1.1.1:8033/cmii/ok:1.2",
	"docker.107421.xyz/rancher/shell:v0.1.6",
}

func TestImageFullNameToGzipFileName(t *testing.T) {

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

func TestGzipFileNameToImageFullName(t *testing.T) {

	test := []string{
		"cmlc=cmii=cmii-admin-data=5.5.0.tar.gz",
		"cmlc=cmii=cmii-uav-mqtthandler=5.5.0.tar.gz",
		"cmlc=cmii=cmii-admin-gateway=5.5.0.tar.gz",
		"cmlc=cmii=cmii-uav-multilink=5.5.0.tar.gz",
		"cmlc=cmii=cmii-admin-user=5.5.0.tar.gz",
		"cmlc=cmii=cmii-uav-notice=5.5.0.tar.gz",
		"cmlc=cmii=cmii-app-release=4.2.0-validation.tar.gz",
		"cmlc=cmii=cmii-uav-oauth=5.5.0.tar.gz",
		"cmlc=cmii=cmii-live-operator=5.2.0.tar.gz",
		"cmlc=cmii=cmii-uav-platform=5.5.0.tar.gz",
		"cmlc=cmii=cmii-open-gateway=5.5.0.tar.gz",
		"cmlc=cmii=cmii-uav-platform-ai-brain=5.5.0.tar.gz",
		"cmlc=cmii=cmii-srs-oss-adaptor=2023-SA.tar.gz",
		"cmlc=cmii=cmii-uav-platform-armypeople=5.5.0.tar.gz",
		"cmlc=cmii=cmii-suav-platform-supervision=5.5.0.tar.gz",
		"cmlc=cmii=cmii-uav-platform-base=5.4.0.tar.gz",
		"cmlc=cmii=cmii-suav-platform-supervisionh5=5.5.0.tar.gz",
		"cmlc=cmii=cmii-uav-platform-cms-portal=5.5.0.tar.gz",
		"cmlc=cmii=cmii-suav-supervision=5.4.0-032501.tar.gz",
		"cmlc=cmii=cmii-uav-platform-detection=5.5.0.tar.gz",
		"cmlc=cmii=cmii-uav-airspace=5.5.0.tar.gz",
		"cmlc=cmii=cmii-uav-platform-jiangsuwenlv=4.1.3-jiangsu-0427.tar.gz",
		"cmlc=cmii=cmii-uav-alarm=5.5.0.tar.gz",
		"cmlc=cmii=cmii-uav-platform-logistics=5.5.0.tar.gz",
		"cmlc=cmii=cmii-uav-autowaypoint=4.2.0-beta.tar.gz",
		"cmlc=cmii=cmii-uav-platform-media=5.5.0.tar.gz",
		"cmlc=cmii=cmii-uav-brain=5.5.0.tar.gz",
		"cmlc=cmii=cmii-uav-platform-multiterminal=5.5.0.tar.gz",
		"cmlc=cmii=cmii-uav-cloud-live=5.5.0.tar.gz",
		"cmlc=cmii=cmii-uav-platform-mws=5.5.0.tar.gz",
		"cmlc=cmii=cmii-uav-cms=5.5.0.tar.gz",
		"cmlc=cmii=cmii-uav-platform-oms=5.5.0.tar.gz",
		"cmlc=cmii=cmii-uav-data-post-process=5.5.0.tar.gz",
		"cmlc=cmii=cmii-uav-platform-open=5.5.0-0419.tar.gz",
		"cmlc=cmii=cmii-uav-developer=5.5.0.tar.gz",
		"cmlc=cmii=cmii-uav-platform-qinghaitourism=4.1.0-21377-0508.tar.gz",
		"cmlc=cmii=cmii-uav-device=5.5.0.tar.gz",
		"cmlc=cmii=cmii-uav-platform-security=5.5.0.tar.gz",
		"cmlc=cmii=cmii-uav-emergency=5.3.0.tar.gz",
		"cmlc=cmii=cmii-uav-platform-securityh5=5.5.0.tar.gz",
		"cmlc=cmii=cmii-uav-gateway=5.5.0.tar.gz",
		"cmlc=cmii=cmii-uav-platform-share=5.5.0.tar.gz",
		"cmlc=cmii=cmii-uav-gis-server=5.5.0.tar.gz",
		"cmlc=cmii=cmii-uav-platform-splice=5.5.0.tar.gz",
		"cmlc=cmii=cmii-uav-grid-datasource=5.2.0-24810.tar.gz",
		"cmlc=cmii=cmii-uav-platform-threedsimulation=5.2.0-21392.tar.gz",
		"cmlc=cmii=cmii-uav-grid-engine=5.1.0.tar.gz",
		"cmlc=cmii=cmii-uav-process=5.5.0.tar.gz",
		"cmlc=cmii=cmii-uav-grid-manage=5.1.0.tar.gz",
		"cmlc=cmii=cmii-uav-surveillance=5.5.0.tar.gz",
		"cmlc=cmii=cmii-uav-industrial-portfolio=5.5.0-041801.tar.gz",
		"cmlc=cmii=cmii-uav-threedsimulation=5.5.0.tar.gz",
		"cmlc=cmii=cmii-uav-integration=5.5.0-0419.tar.gz",
		"cmlc=cmii=cmii-uav-tower=5.5.0.tar.gz",
		"cmlc=cmii=cmii-uav-kpi-monitor=5.5.0.tar.gz",
		"cmlc=cmii=cmii-uav-user=5.5.0.tar.gz",
		"cmlc=cmii=cmii-uav-logger=5.5.0.tar.gz",
		"cmlc=cmii=cmii-uav-waypoint=5.5.0.tar.gz",
		"cmlc=cmii=cmii-uav-material-warehouse=5.5.0.tar.gz",
		"cmlc=cmii=srs=v5.0.195.tar.gz",
		"cmlc=cmii=cmii-uav-mission=5.5.0.tar.gz",
	}

	test2 := []string{
		"docker=rancher=backup-restore-operator=v1.0.3.tar.gz",
		"docker=rancher=kube-api-auth = v0.1.4.tar.gz",
		"docker=rancher=calico-cni=v3.17.2.tar.gz",
		"docker=rancher=kubernetes-external-dns=v0.7.3.tar.gz",
		"docker=rancher=calico-ctl=v3.17.2.tar.gz",
		"docker=rancher=library-busybox=1.31.1.tar.gz",
		"docker=rancher=calico-kube-controllers=v3.17.2.tar.gz",
		"docker=rancher=library-busybox=1.32.1.tar.gz",
		"docker=rancher=calico-node=v3.17.2.tar.gz",
		"docker=rancher=library-nginx=1.19.2-alpine.tar.gz",
		"docker=rancher=calico-pod2daemon-flexvol=v3.17.2.tar.gz",
		"docker=rancher=library-traefik=1.7.19.tar.gz",
		"docker=rancher=cis-operator=v1.0.3.tar.gz",
		"docker=rancher=local-path-provisioner=v0.0.11.tar.gz",
		"docker=rancher=cluster-proportional-autoscaler=1.7.1.tar.gz",
		"docker=rancher=local-path-provisioner=v0.0.14.tar.gz",
		"docker=rancher=cluster-proportional-autoscaler=1.8.1.tar.gz",
		"docker=rancher=local-path-provisioner=v0.0.19.tar.gz",
		"docker=rancher=configmap-reload=v0.3.0-rancher4.tar.gz",
		"docker=rancher=log-aggregator=v0.1.7.tar.gz",
		"docker=rancher=coredns-coredns=1.8.0.tar.gz",
		"docker=rancher=metrics-server=v0.4.1.tar.gz",
		"docker=rancher=coreos-etcd=v3.4.14-rancher1.tar.gz",
		"docker=rancher=nginx-ingress-controller-defaultbackend=1.5-rancher1.tar.gz",
		"docker=rancher=coreos-flannel=v0.13.0-rancher1.tar.gz",
		"docker=rancher=nginx-ingress-controller=nginx-0.43.0-rancher1.tar.gz",
		"docker=rancher=coreos-kube-state-metrics=v1.9.7.tar.gz",
		"docker=rancher=opa-gatekeeper=v3.1.0-beta.7.tar.gz",
		"docker=rancher=coreos-prometheus-config-reloader=v0.39.0.tar.gz",
		"docker=rancher=openzipkin-zipkin=2.14.2.tar.gz",
		"docker=rancher=coreos-prometheus-operator=v0.39.0.tar.gz",
		"docker=rancher=pause=3.2.tar.gz",
		"docker=rancher=externalip-webhook=v0.1.6.tar.gz",
		"docker=rancher=plugins-docker=18.09.tar.gz",
		"docker=rancher=flannel-cni=v0.3.0-rancher6.tar.gz",
		"docker=rancher=prom-alertmanager=v0.21.0.tar.gz",
		"docker=rancher=fleet-agent=v0.3.4.tar.gz",
		"docker=rancher=prometheus-auth=v0.2.1.tar.gz",
		"docker=rancher=fleet=v0.3.4.tar.gz",
		"docker=rancher=prom-node-exporter=v1.0.1.tar.gz",
		"docker=rancher=fluentd=v0.1.24.tar.gz",
		"docker=rancher=prom-prometheus=v2.18.2.tar.gz",
		"docker=rancher=grafana-grafana=7.1.5.tar.gz",
		"docker=rancher=rancher-agent=v2.5.7.tar.gz",
		"docker=rancher=hyperkube=v1.20.4-rancher1.tar.gz",
		"docker=rancher=rancher=v2.5.7.tar.gz",
		"docker=rancher=istio-kubectl=1.5.10.tar.gz",
		"docker=rancher=rancher-webhook=v0.1.0-beta9.tar.gz",
		"docker=rancher=jimmidyson-configmap-reload=v0.3.0.tar.gz",
		"docker=rancher=rke-tools=v0.1.72.tar.gz",
		"docker=rancher=k8s-dns-dnsmasq-nanny=1.15.2.tar.gz",
		"docker=rancher=security-scan=v0.1.14.tar.gz",
		"docker=rancher=k8s-dns-kube-dns=1.15.2.tar.gz",
		"docker=rancher=security-scan=v0.2.2.tar.gz",
		"docker=rancher=k8s-dns-node-cache=1.15.13.tar.gz",
		"docker=rancher=shell=v0.1.6.tar.gz",
		"docker=rancher=k8s-dns-sidecar=1.15.2.tar.gz",
		"docker=rancher=sonobuoy-sonobuoy=v0.16.3.tar.gz",
		"docker=rancher=klipper-lb=v0.1.2.tar.gz",
		"docker=rancher=system-upgrade-controller=v0.6.2.tar.gz",
	}

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

	test = append(test, test2...)
	test = append(test, test3...)

	for _, s := range test {
		gzipFileName := GzipFileNameToImageFullName(s)
		fmt.Println(gzipFileName)
		fullName := ImageNameToTargetImageFullName(gzipFileName, "10.250.0.100:8033")
		fmt.Println(fullName)
		fmt.Println("--------------------")
	}
}

func TestImageGzipFileNameToImageFullName_1(t *testing.T) {
	gzipFilePrefix := "/root/octopus_image/middle/"
	filepath.WalkDir(gzipFilePrefix, func(path string, d os.DirEntry, err error) error {
		//fmt.Println(path)
		name := d.Name()
		if strings.HasSuffix(name, ".tar.gz") {
			fullName := GzipFileNameToImageFullName(name)
			fmt.Println(fullName)
		}

		return nil
	})
}

func TestImageGzipFileNameToImageFullName(t *testing.T) {

	frontendMap := make(map[string]string)
	backendMap := make(map[string]string)
	srsMap := make(map[string]string)

	gzipFilePrefix := "/root/octopus_image/middle/"
	filepath.WalkDir(gzipFilePrefix, func(path string, d os.DirEntry, err error) error {
		//fmt.Println(path)
		name := d.Name()
		if strings.HasSuffix(name, ".tar.gz") {
			imageName, imageTag := GzipFileNameToImageNameAndTag(name)
			if strings.Contains(imageName, "platform") {
				frontendMap[imageName] = imageTag
			} else if strings.Contains(imageName, "srs") {
				srsMap[imageName] = imageTag
			} else if strings.Contains(imageName, "operator") {
				srsMap[imageName] = imageTag
			} else {
				backendMap[imageName] = imageTag
			}
		}

		return nil
	})

	utils.BeautifulPrint(backendMap)
	utils.BeautifulPrint(frontendMap)
	utils.BeautifulPrint(srsMap)
}

func TestFrontendBackendImageMapFromCmiiImageMap(t *testing.T) {
	frontendImageVersionMap, backendImageVersionMap, _ := FrontendBackendSrsImageMapFromCmiiImageMap(zjjt.CmiiImageMap)

	utils.BeautifulPrint(frontendImageVersionMap)
	utils.BeautifulPrint(backendImageVersionMap)
}

func TestImageFullNameToImageNameAndTag(t *testing.T) {

	for _, s := range imageFullNameList {
		imageName, imageTag := ImageFullNameToImageNameAndTag(s)
		fmt.Printf("%-8s %-8s %-8s\n", imageName, imageTag, s)

	}
}

func TestImageFullNameToAppName(t *testing.T) {

	for _, s := range imageFullNameList {
		imageName := ImageFullNameToAppName(s)
		fmt.Printf("%-8s %-8s\n", imageName, s)

	}
}

func TestCmiiImageMapFromImageFullNameList(t *testing.T) {
	imageList := zhejianyidong_erjipingtai.Cmii570ImageList

	cmiiImageVersionMap := CmiiImageMapFromImageFullNameList(imageList)

	utils.BeautifulPrint(cmiiImageVersionMap)
}
