package cmii_operator

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

func TestCmiiK8sOperator_DeploymentAll(t *testing.T) {

	start := time.Now()
	deploymentList := CmiiOperator.DeploymentAll("devflight")
	elapsed := time.Since(start).Milliseconds()
	fmt.Printf("执行耗时： %d ms\n", elapsed)

	var nameList []string

	count := 1
	for _, deployment := range deploymentList {
		if deployment.Spec.Replicas != nil {
			count++
		}
		nameList = append(nameList, deployment.Name)
	}

	t.Logf("deployment list lenght is => %d", count)
	t.Logf("deployment name list are => %d", count)

	utils.BeautifulPrint(nameList)
	//for _, deployment := range deploymentList {
	//	utils.BeautifulPrint(deployment)
	//	println()
	//}

}
func TestCmiiK8sOperator_DeploymentAllInterface(t *testing.T) {

	start := time.Now()
	deploymentList := CmiiOperator.DeploymentAllInterface("devflight")
	elapsed := time.Since(start).Milliseconds()
	fmt.Printf("执行耗时： %d ms\n", elapsed)

	t.Logf("deployment list lenght is => %d", len(deploymentList))

	for _, deployment := range deploymentList {
		utils.BeautifulPrint(deployment)
		println()
	}
}

func TestCmiiK8sOperator_DeploymentFizz(t *testing.T) {

	start := time.Now()
	deploymentFizz := CmiiOperator.DeploymentFizz("int", "cmii-suav-supervision")
	elapsed := time.Since(start).Milliseconds()
	fmt.Printf("执行耗时： %d ms\n", elapsed)

	t.Logf("deployment list lenght is => %d", len(deploymentFizz))

	cmiiDeploymentInterface := CmiiDeploymentInterface{}
	for _, pod := range deploymentFizz {
		convert := cmiiDeploymentInterface.Convert(pod)
		utils.BeautifulPrint(convert)

		utils.BeautifulPrint(pod)
	}

	// elastic search

}

func TestCmiiK8sOperator_DeploymentScale(t *testing.T) {
	start := time.Now()

	CmiiOperator.DeploymentScale("devflight", "cmii-uav-depotautoreturn", 0)
	elapsed := time.Since(start).Milliseconds()
	fmt.Printf("执行耗时： %d ms\n", elapsed)

}

func TestCmiiK8sOperator_DeploymentUpdateTag(t *testing.T) {

	start := time.Now()
	CmiiOperator.DeploymentUpdateTag("demo", "cmii-uav-platform", "5.2.0-011001")
	elapsed := time.Since(start).Milliseconds()
	fmt.Printf("执行耗时： %d ms\n", elapsed)
}

func TestCmiiK8sOperator_DeploymentRestart(t *testing.T) {

	cmiiEnv := "int"
	appName := "cmii-uav-gis-server"

	CmiiOperator.DeploymentRestart(cmiiEnv, appName)

	utils.SplitLinePrint()

	check := CmiiOperator.DeploymentStatusCheck(cmiiEnv, appName, 180)
	assert.Equal(t, check, true, "deployment run failed!")
}

func TestCmiiK8sOperator_DeploymentRestartByKill(t *testing.T) {
	cmiiEnv := "demo"
	appName := "cmii-uav-platform"

	kill := CmiiOperator.DeploymentRestartByKill(cmiiEnv, appName)
	assert.Equal(t, kill, true, "deployment restart by kill failed !")

	utils.SplitLinePrint()

	check := CmiiOperator.DeploymentStatusCheck(cmiiEnv, appName, 180)
	assert.Equal(t, check, true, "deployment run failed!")

}

func TestCmiiK8sOperator_DeploymentOneInterface(t *testing.T) {
	start := time.Now()
	deploy := CmiiOperator.DeploymentOneInterface("devflight", "cmii-uav-depotautoreturn")
	elapsed := time.Since(start).Milliseconds()
	fmt.Printf("执行耗时： %d ms\n", elapsed)

	utils.BeautifulPrint(*deploy)
}

func TestCmiiK8sOperator_ReplicaSetExists(t *testing.T) {

	cmiiEnv := "uavcloud-devflight"
	appName := "cmii-admin-data-bf8f87cb7"

	exists := CmiiOperator.ReplicaSetExists(cmiiEnv, appName)

	utils.BeautifulPrint(*exists)
}

func TestCmiiK8sOperator_ReplicaSetByAppName(t *testing.T) {

	cmiiEnv := "uavcloud-devflight"
	appName := "cmii-admin-data"

	exists := CmiiOperator.ReplicaSetByAppName(cmiiEnv, appName)

	for _, replicaSet := range exists {
		utils.BeautifulPrint(replicaSet)
	}
}

func TestCmiiK8sOperator_PodAll(t *testing.T) {
	start := time.Now()
	podList := CmiiOperator.PodAll("devflight")
	elapsed := time.Since(start).Milliseconds()
	fmt.Printf("执行耗时： %d ms\n", elapsed)

	for _, pod := range podList {
		println()
		utils.BeautifulPrint(pod)
		println()
		break
	}
}

func TestCmiiK8sOperator_PodFizz(t *testing.T) {
	start := time.Now()
	podList := CmiiOperator.PodFizz("devflight", "cmii-uav-data-post-process")
	elapsed := time.Since(start).Milliseconds()
	fmt.Printf("执行耗时： %d ms\n", elapsed)
	t.Logf("pod list lenght is => %d", len(podList))

	cmiiPodInterface := CmiiPodInterface{}
	for _, pod := range podList {
		println()
		utils.BeautifulPrint(pod)
		println()
		convert := cmiiPodInterface.Convert(pod)
		utils.BeautifulPrint(convert)
	}

}

func TestCmiiK8sOperator_PodByAppName(t *testing.T) {
	cmiiEnv := "uat"
	appName := "cmii-admin-data"

	exists := CmiiOperator.PodByNodeName(cmiiEnv, appName)

	for _, podInterface := range exists {
		utils.BeautifulPrint(podInterface)
	}
}

func TestCmiiK8sOperator_PodFizz2(t *testing.T) {

	start := time.Now()
	podList := CmiiOperator.PodFizz("devflight", "notice")
	elapsed := time.Since(start).Milliseconds()
	fmt.Printf("执行耗时： %d ms\n", elapsed)
	t.Logf("pod list lenght is => %d", len(podList))

	podInterface := CmiiPodInterface{}

	for _, pod := range podList {
		convert := podInterface.Convert(pod)

		println()
		utils.BeautifulPrint(&convert)
		println()
	}

}

func TestCmiiK8sOperator_PodByNodeName(t *testing.T) {

	cmiiEnv := "uat"
	nodeName := "test-03.ecs.io"

	exists := CmiiOperator.PodByNodeName(cmiiEnv, nodeName)
	exists = FilterAllCmiiPodSoft(exists)
	for _, podInterface := range exists {
		utils.BeautifulPrint(podInterface)

		if !podInterface.PodStatus {
			podDelete := CmiiOperator.PodDelete(podInterface.Namespace, podInterface.Name)
			assert.Equal(t, podDelete, true, "delete pod failed !")
		}
	}
}

func TestCmiiK8sOperator_PodExec(t *testing.T) {

	podList := CmiiOperator.PodByAppName(devFlight, "cmii-uav-gateway")

	stdout, stderr := CmiiOperator.PodExec(devFlight, podList[0], []string{
		"env",
	})

	scanner := bufio.NewScanner(stdout)

	for scanner.Scan() {
		// Get the current line
		line := scanner.Text()

		// Check if the current line contains the search term
		fmt.Println(line)
	}
	utils.SplitLinePrint()

	scanner = bufio.NewScanner(stderr)
	for scanner.Scan() {
		// Get the current line
		line := scanner.Text()

		// Check if the current line contains the search term
		fmt.Println(line)
	}

}

func TestCmiiK8sOperator_DeploymentStatusCheck(t *testing.T) {

	cmiiEnv := "devflight"
	appName := "cmii-uav-gateway"

	check := CmiiOperator.DeploymentStatusCheck(cmiiEnv, appName, 180)
	assert.Equal(t, check, true, "deployment run failed!")

}

func TestCmiiK8sOperator_NodeAll(t *testing.T) {
	start := time.Now()
	nodeList := CmiiOperator.NodeAll("dev")
	elapsed := time.Since(start).Milliseconds()
	fmt.Printf("执行耗时： %d ms\n", elapsed)

	for _, node := range nodeList {

		println()
		utils.BeautifulPrint(node)
		println()
	}
}

func TestCmiiK8sOperator_NodeAllInterface(t *testing.T) {
	cmiiNodeInterfaces := CmiiOperator.NodeAllInterface("uat")

	for _, nodeInterface := range cmiiNodeInterfaces {
		println()
		utils.BeautifulPrint(nodeInterface)
		println()
	}
}
