package crawler_manager

import (
	"path/filepath"
	"google.golang.org/grpc"
	"log"
	g "loafle.com/overflow/crawler_go/grpc"
	"context"


	"strings"
)


func callAdd(container *string, crawlerName *string, id *string) bool  {

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

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

	cc := g.NewConfigClient(conn)

	in := &g.Input{}

	in.Id = *id
	in.Name = g.Crawlers(g.Crawlers_value[*crawlerName])

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

	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/loafle.com/overflow/crawler_go/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, crawlerName *string, id *string) {

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

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

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

	cc := g.NewConfigClient(conn)

	inR := &g.Input{}
	inR.Id = *id
	inR.Name = g.Crawlers(g.Crawlers_value[*crawlerName])

	outRem, errRem := cc.Remove(context.Background(), inR)
	if errRem != nil {
		log.Println(errRem)
	}
	log.Println("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.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()

	c := g.NewStatusClient(conn)

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

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

	return out.Check

}