package main

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

func TestCmiiK8sOperator_DeploymentAll(t *testing.T) {

	start := time.Now()
	deploymentList := DefaultCmiiOperator.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 := DefaultCmiiOperator.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 := DefaultCmiiOperator.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 := config.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()

	DefaultCmiiOperator.DeploymentScale(config.Demo, "cmii-uav-industrial-portfolio", 1)
	elapsed := time.Since(start).Milliseconds()
	fmt.Printf("执行耗时： %d ms\n", elapsed)

}

func TestCmiiK8sOperator_DeploymentUpdateTag(t *testing.T) {

	start := time.Now()
	DefaultCmiiOperator.DeploymentUpdateTag("Demo", "cmii-uav-gateway", "5.7.0")
	elapsed := time.Since(start).Milliseconds()
	fmt.Printf("执行耗时： %d ms\n", elapsed)
}

func TestCmiiK8sOperator_DeploymentRestart(t *testing.T) {

	cmiiEnv := config.Integration
	appName := "cmii-uav-data-post-process"

	DefaultCmiiOperator.DeploymentRestart(cmiiEnv, appName)

	utils.SplitLinePrint()

	check := DefaultCmiiOperator.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 := DefaultCmiiOperator.DeploymentRestartByKill(cmiiEnv, appName)
	assert.Equal(t, kill, true, "deployment restart by kill failed !")

	utils.SplitLinePrint()

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

}

func TestCmiiK8sOperator_ReplicaSetExists(t *testing.T) {

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

	exists := DefaultCmiiOperator.ReplicaSetExists(cmiiEnv, appName)

	utils.BeautifulPrint(*exists)
}

func TestCmiiK8sOperator_ReplicaSetByAppName(t *testing.T) {

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

	exists := DefaultCmiiOperator.ReplicaSetByAppName(cmiiEnv, appName)

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

func TestCmiiK8sOperator_PodAll(t *testing.T) {
	start := time.Now()
	podList := DefaultCmiiOperator.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 := DefaultCmiiOperator.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 := config.CmiiPodInterface{}
	for _, pod := range podList {
		println()
		utils.BeautifulPrint(pod)
		println()
		convert := cmiiPodInterface.Convert(pod)
		utils.BeautifulPrint(convert)
	}

}

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

	exists := DefaultCmiiOperator.PodByAppName(cmiiEnv, appName)

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

func TestCmiiK8sOperator_PodFizz2(t *testing.T) {

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

	podInterface := config.CmiiPodInterface{}

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

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

}

func TestCmiiK8sOperator_PodByNodeName(t *testing.T) {

	cmiiEnv := config.DevOperation
	nodeName := "Dev-master-01"

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

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

func TestCmiiK8sOperator_NodeCordon(t *testing.T) {
	cmiiEnv := config.DevOperation
	nodeName := "Dev-master-01"

	cordon := DefaultCmiiOperator.NodeCordon(cmiiEnv, nodeName)
	assert.Equal(t, cordon, true, "cordon node failed !")
}

func TestCmiiK8sOperator_NodeUnCordon(t *testing.T) {
	cmiiEnv := config.DevOperation
	nodeName := "Dev-06.ecs.io"

	uncordon := DefaultCmiiOperator.NodeUnCordon(cmiiEnv, nodeName)
	assert.Equal(t, uncordon, true, "uncordon node failed !")
}

func TestCmiiK8sOperator_PodEvictFromNode(t *testing.T) {
	cmiiEnv := config.DevOperation
	nodeName := "Dev-04.ecs.io"

	podList := DefaultCmiiOperator.PodEvictFromNode(cmiiEnv, nodeName)

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

}

func TestCmiiK8sOperator_PodExec(t *testing.T) {

	podList := DefaultCmiiOperator.PodByAppName(config.DevFlight, "cmii-uav-gateway")

	stdout, stderr := DefaultCmiiOperator.PodExec(config.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 := DefaultCmiiOperator.DeploymentStatusCheck(cmiiEnv, appName, 180)
	assert.Equal(t, check, true, "deployment run failed!")

}

func TestCmiiK8sOperator_NodeAll(t *testing.T) {
	start := time.Now()
	nodeList := DefaultCmiiOperator.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 := DefaultCmiiOperator.NodeAllInterface(config.Demo)

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

func TestCmiiK8sOperator_DeploymentOneInterface(t *testing.T) {

	namespace := config.Demo
	appName := "cmii-uav-mission"

	deploy := DefaultCmiiOperator.DeploymentOneInterface(namespace, appName)

	utils.BeautifulPrint(*deploy)
}
