package crawler_manager

import (
	"context"
	g "git.loafle.net/overflow/overflow_probe/crawler/grpc"
	log "github.com/cihub/seelog"
	"google.golang.org/grpc"

	"encoding/json"
	"git.loafle.net/overflow/overflow_probe/agent_api/config_manager"
)

func callAdd(container *string, conf *config_manager.Config) bool {

	port := GetInstance().portMap[*container]

	conn, err := grpc.Dial(address+port, grpc.WithInsecure())
	if err != nil {
		//log.Fatalf("did not connect: %v", err)
		log.Error(err)
		return false
	}
	defer conn.Close()

	cc := g.NewConfigClient(conn)

	in := &g.InputAdd{}

	b, err := json.Marshal(conf)
	if err != nil {
		return false
	}

	in.Data = b
	in.Name = g.Crawlers(g.Crawlers_value[conf.Crawler.Name])

	out, err := cc.Add(context.Background(), in)
	if err != nil {
		log.Error(err)
	}
	//log.Println("callAdd:", out)
	log.Debug("callAdd:", out)

	return true

}

func callInitConfigOne(container *string, conf *config_manager.Config) bool {

	var cl []*config_manager.Config

	cl = append(cl, conf)

	return callInitConfig(container, cl)

}

func callInitConfig(container *string, cl []*config_manager.Config) bool {

	port := GetInstance().portMap[*container]

	conn, err := grpc.Dial(address+port, grpc.WithInsecure())
	if err != nil {
		//log.Fatalf("did not connect: %v", err)
		log.Error(err)
		return false
	}
	defer conn.Close()

	cc := g.NewConfigClient(conn)

	inArr := &g.InputArray{}

	for _, conf := range cl {

		in := &g.Init{}
		in.Name = g.Crawlers(g.Crawlers_value[conf.Crawler.Name])

		b, err := json.Marshal(conf)
		if err != nil {
			continue
		}

		in.Data = b
		inArr.In = append(inArr.In, in)
	}

	outInit, errInit := cc.Init(context.Background(), inArr)
	if errInit != nil {
		log.Debug(errInit)
		return false
	}
	log.Debug("callInit:", outInit)

	return true

}

//func callInit(container *string, paths *[]string) bool {
//
//	port := GetInstance().portMap[*container]
//
//	return callInitAddress(address + port, paths)
//}
//

//
//func callInitAddress(address string, paths *[]string) bool {
//	conn, err := grpc.Dial(address, grpc.WithInsecure())
//	if err != nil {
//		log.Fatalf("did not connect: %v", err)
//		return false
//	}
//	defer conn.Close()
//
//	cc := g.NewConfigClient(conn)
//
//	inArr := &g.InputArray{}
//	base := "HEALTH_"
//	for _, path := range *paths {
//
//		in := &g.Init{}
//		//in.Path = "/home/snoop/develop/path/go/src/git.loafle.net/overflow/overflow_probe/crawler/config/"
//		in.Path = path + "/"
//		bcn := filepath.Base(path)
//		bcn = strings.ToUpper(bcn)
//
//		in.Name = g.Crawlers(g.Crawlers_value[base+bcn])
//		//in.Name = g.Crawlers(g.Crawlers_value[g.Crawlers_HEALTH_REDIS.String()]) //test
//		inArr.In = append(inArr.In, in)
//	}
//
//	outInit, errInit := cc.Init(context.Background(), inArr)
//	if errInit != nil {
//		log.Println(errInit)
//		return false
//	}
//	log.Println("callInit:",outInit)
//
//	return true
//}

func callRemove(container *string, conf *config_manager.Config) {

	port := GetInstance().portMap[*container]

	conn, err := grpc.Dial(address+port, grpc.WithInsecure())

	if err != nil {
		//log.Fatalf("did not connect: %v", err)
		log.Error(err)
	}
	defer conn.Close()

	cc := g.NewConfigClient(conn)

	inR := &g.Input{}
	//inR.Id = *id  //FIXME
	inR.Name = g.Crawlers(g.Crawlers_value[conf.Crawler.Name])

	outRem, errRem := cc.Remove(context.Background(), inR)
	if errRem != nil {
		log.Debug(errRem)
	}
	log.Debug("callRemove:", outRem)

}

func callStatus(container *string) bool {

	port := GetInstance().portMap[*container]

	if port == "" {
		return false
	}

	return callStatusAddress(address + port)

}

func callStatusAddress(addr string) bool {

	conn, err := grpc.Dial(addr, grpc.WithInsecure())
	if err != nil {
		log.Error("did not connect: ", err)
	}
	defer conn.Close()

	c := g.NewStatusClient(conn)

	e := &g.Empty{}
	out, err := c.Status(context.Background(), e)

	if err != nil {
		log.Error("could not greet: ", err)
	}

	return out.Check

}
