package crawler_manager

import (
	"testing"
	//grpc1 "google.golang.org/grpc"
	"os"

	"os/exec"

	"path/filepath"

	"strings"
	"io/ioutil"

	"git.loafle.net/overflow/agent_api/config_manager"
	"encoding/json"
	"fmt"

)



func TestCallInit(t *testing.T) {
	//CallInit("")
}



func TestPid(t *testing.T) {

	pp, err := os.FindProcess(12314)
	if err != nil{
		t.Log("err : ", err)
	}

	t.Log(pp.Pid)

}

func TestPr(t *testing.T) {
	//ps -aux | awk '{print $2}' | grep 15538
	bytes, _ := exec.Command("ps","-aux", "awk", "{print $2}").Output()

	t.Log(string(bytes))

}

func TestExe(t *testing.T) {
	ps := exec.Command("ps", "-aux")
	awk := exec.Command("awk", "{print $2}")
	grep := exec.Command("grep", "22373")

	awk.Stdin, _ = ps.StdoutPipe()
	grep.Stdin,_ = awk.StdoutPipe()

	ps.Start()
	awk.Start()

	byt, _ := grep.Output()

	t.Log(len(byt))
}

func TestExeState(t *testing.T) {
	ps := exec.Command("ps", "-aux")
	awk := exec.Command("awk", "{print $2, $7}")
	grep := exec.Command("grep", "22373")
	awk2 := exec.Command("awk", "{print $2}")

	awk.Stdin, _ = ps.StdoutPipe()
	grep.Stdin,_ = awk.StdoutPipe()
	awk2.Stdin,_ = grep.StdoutPipe()

	ps.Start()
	awk.Start()
	grep.Start()

	byt, _ := awk2.Output()

	t.Log(len(byt))
	t.Log(string(byt))
	t.Log(len(strings.TrimSpace(string(byt))))
}

func TestPipe(t *testing.T) {
	c1 := exec.Command("ls")
	c2 := exec.Command("wc", "-l")
	c2.Stdin, _ = c1.StdoutPipe()
	//c2.Stdout = os.Stdout

	_ = c1.Start()
	aa, _ := c2.Output()

	t.Log(string(aa))
}

func TestCom(t *testing.T) {
	str:= "Z"

	if str == "Z" {
		t.Log("aaa")
	}
}

func TestReadConfig(t *testing.T) {

	//c := ReadConfig("/home/snoop/develop/path/go/src/loafle.com/overflow/crawler_go/config/example.json")
	//t.Log(c)

}


func TestDir(t *testing.T) {
	a := filepath.Dir("/home/snoop/develop/path/go/src/loafle.com/overflow/crawler_go/config/example.json")

	t.Log(a)
}


func TestMyPid(t *testing.T) {
	t.Log(os.Getpid())
}


func TestDir22(t *testing.T) {

	//cs := IsStartContainer()

	//t.Log(cs)

}

func TestCrateDir(t *testing.T) {

	cp := "/home/snoop/develop/path/go/src/loafle.com/overflow/crawler_manager_go/"

	var configPath []string;

	rootFolder := "/home/cm2/"
	ConfigFolder := rootFolder + "/config/container/"
	BinaryFolder := rootFolder + "/container/"
	PidFolder := rootFolder + "/pids/"
	//runFile := "ttnc"

	configPath = append(configPath, ConfigFolder + "/java/oracle/")

	configPath = append(configPath, ConfigFolder + "/go/test/")

	configPath = append(configPath, ConfigFolder + "/java/mysql/")
	configPath = append(configPath, ConfigFolder + "/network/http/")
	configPath = append(configPath, ConfigFolder + "/network/redis/")
	configPath = append(configPath, ConfigFolder + "/network/ldap/")

	configPath = append(configPath, PidFolder)

	var containerPath []string;

	containerPath = append(containerPath, BinaryFolder + "/java/")
	containerPath = append(containerPath, BinaryFolder + "/go/")
	containerPath = append(containerPath, BinaryFolder + "/network/")



	b, _:=ioutil.ReadFile(cp + "config/example.json")

	for i, p := range configPath {
		pp := filepath.Join(p)
		os.MkdirAll(pp, os.ModePerm)

		if i % 2 == 0 {
			ioutil.WriteFile(pp + "/id.json", b, os.ModePerm)
		}

	}


	bbb, _:=ioutil.ReadFile(cp + "config/ttnc")

	for _, p := range containerPath {
		pp := filepath.Join(p)
		os.MkdirAll(pp, os.ModePerm)

		pp += "/"
		pp += "/ttnc"
		ioutil.WriteFile(pp, bbb, os.ModePerm)

	}

}

func TestInit(t *testing.T) {
	//InitContainer()
}

func TestDirs(t *testing.T) {

	//cs := IsStartContainer()
	//
	//for _, c := range cs {
	//	var ccl []string
	//	ExistConfigFileDir(ConfigFolder, c, &ccl)
	//	t.Log(ccl)
	//
	//}

}

func TestSplitPath(t *testing.T) {

	ff :="/home/snoop/develop/path/go/src/loafle.com/overflow/crawler_go/config/ffff"
	a := filepath.Dir(ff)
	t.Log(a)
	a = filepath.Clean(a)
	t.Log(a)
	a = filepath.Base(a)

	t.Log(a)


}

func TestInitCM(t *testing.T) {
	GetInstance().init()

	GetInstance().stopContainerAll()
}

func TestCallStatus(t *testing.T) {
	//c := "java"
	//GetInstance().runAndInitContainer(&c)
	//
	//callStatus(&c)
	//
	//GetInstance().stopContainer(&c)
}

func TestCallAdd(t *testing.T) {
	////
	//c := "java"
	//cn := "HEALTH_"+"MYSQL"
	//id := "id.json"
	//
	//GetInstance().runAndInitContainer(&c)
	//
	//callAdd(&c, &cn, &id)
	//
	//GetInstance().stopContainer(&c)

}

func TestCallRemove22(t *testing.T) {

	//c := "java"
	//cn := "HEALTH_"+"MYSQL"
	//id := "id.json"
	//
	//GetInstance().runAndInitContainer(&c)
	//
	//callRemove(&c, &cn, &id)
	//
	//GetInstance().stopContainer(&c)


}

func TestRunRun(t *testing.T) {
	c := "java"
	GetInstance().runContainer(&c)
	GetInstance().runContainer(&c)
	GetInstance().runContainer(&c)


	GetInstance().stopContainer(&c)

}

func TestActiveCrawler(t *testing.T) {

	//c := "java"
	//GetInstance().runContainer(&c)
	//
	//aa := GetInstance().activeCrawler(&c)
	//
	//t.Log(aa)
	//
	//GetInstance().stopContainer(&c)

}

func TestErer(t *testing.T) {

	var asdf []string

	asdf = nil

	t.Log(len(asdf))

}
//
//func TestStartContainer(t *testing.T) {
//
//	cs := GetStartContainer()
//
//	t.Log(cs)
//
//}

//
//func GetStartContainer() []string {
//
//	files, _ := ioutil.ReadDir(ConfigFolder)
//
//	var cs []string
//
//	for _,file := range files {
//
//		if file.IsDir() {
//			b := existConfigFile(ConfigFolder, file.Name())
//			if b {
//				cs = append(cs, file.Name())
//			}
//		}
//
//	}
//	return cs
//}

func TestJson(t *testing.T) {

	c := config_manager.Config{}

	c.Crawler.Name = "wmi_crawler"
	c.Crawler.Container = "java_proxy";

	c.Id =  "WMI_TEST_ID_001"


	b, _ := json.Marshal(c)

	fmt.Println(string(b))

}

func TestJsonMa(t *testing.T) {

	b, _ := ioutil.ReadFile("./test/example.json")

	c := config_manager.Config{}

	json.Unmarshal(b, &c)

	t.Log(c)

}


func TestMock(t *testing.T) {

	//GetInstance().ConfigMgr = &ConfigManagerTest{}
	//
	//GetInstance().init()

}

func TestRPC(t *testing.T) {


	//c := conMgr.NewConfigManager()
	//c.LoadGlobalConfigTTT("/home/snoop/develop/path/go/src/loafle.com/overflow/config_manager_go/test_agent/global.yaml")
	//c.LoadCrawlerConfigAllTTT()
	//
	//observer.Notify(messages.CONFIGMANAGER_LOADED,c)
	//
	//
	//wg := sync.WaitGroup{}
	//
	//wg.Add(1)
	//
	//go func() {
	//	time.Sleep(20 * time.Second)
	//	wg.Done()
	//}()
	//
	//
	//wg.Wait()

}

func TestGRPC(t *testing.T) {
	b := callStatusAddress("localhost:50052")

	t.Log(b)
}

type ConfigManagerTest struct {

}

func (c *ConfigManagerTest)GetGlobalConfig() *config_manager.GlobalConfig {

	g := config_manager.GlobalConfig{}


	rootFolder := "/home/cm2/"
	g.Paths.ConfigFolder = rootFolder + "/config/container/"
	g.Paths.BinaryFolder = rootFolder + "/container/"
	g.Paths.PidFolder = rootFolder + "/pids/"


	return &g
}
func (c *ConfigManagerTest)GetCrawlerById(id string) *config_manager.Config {
	return nil
}
func (c *ConfigManagerTest)GetCrawlers() map[string] *config_manager.Config {

	var m map[string] *config_manager.Config

	b, _ := ioutil.ReadFile("./test/example.json")

	ccc := config_manager.Config{}

	json.Unmarshal(b, &ccc)

	m["wmi_crawler"] = &ccc

	return m
}