package integration_test_utils

import (
	"context"
	"fmt"
	"os"
	"time"

	configMgmtPb "code.fbi.h-da.de/danet/gosdn/api/go/gosdn/configurationmanagement"
	ppb "code.fbi.h-da.de/danet/gosdn/api/go/gosdn/pnd"
	"code.fbi.h-da.de/danet/gosdn/api/go/gosdn/rbac"
	"code.fbi.h-da.de/danet/gosdn/controller/api"

	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"google.golang.org/grpc/metadata"
)

func createContextWithAuthorization(loginResponse *rbac.LoginResponse) context.Context {
	md := metadata.Pairs("authorize", loginResponse.Token)
	return metadata.NewOutgoingContext(context.Background(), md)
}

func CreateSecureConnection() (*grpc.ClientConn, context.Context, error) {
	username := "admin"
	password := "TestPassword"
	controllerUrl := "localhost:55055"
	controllerEnv := os.Getenv("INTEGRATION_TEST_CONTROLLER_URL")
	if controllerEnv != "" {
		controllerUrl = controllerEnv
	}

	loginResp, err := api.Login(context.Background(), controllerUrl, username, password)
	if err != nil {
		return nil, nil, err
	}

	sessionContext := createContextWithAuthorization(loginResp)

	dialOption := grpc.WithTransportCredentials(insecure.NewCredentials())
	conn, err := grpc.Dial(controllerUrl, dialOption, grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(100*1024*1024)))
	if err != nil {
		return nil, nil, err
	}

	return conn, sessionContext, nil
}

func CreateConnection() (*grpc.ClientConn, context.Context, error) {
	controllerUrl := "localhost:55055"
	controllerEnv := os.Getenv("INTEGRATION_TEST_CONTROLLER_URL")
	if controllerEnv != "" {
		controllerUrl = controllerEnv
	}
	dialOption := grpc.WithTransportCredentials(insecure.NewCredentials())
	conn, err := grpc.Dial(controllerUrl, dialOption, grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(100*1024*1024)))
	if err != nil {
		return nil, nil, err
	}

	sessionContext := context.Background()

	return conn, sessionContext, nil
}

// ExportCurrentSDNConfig can be used to save the current SDN config as a string to use in an other test later on.
func ExportCurrentSDNConfig(conn *grpc.ClientConn, ctx context.Context) (string, error) {
	pndService := ppb.NewPndServiceClient(conn)
	pndRes, err := pndService.GetPndList(ctx, &ppb.GetPndListRequest{Timestamp: getTimestamp()})
	if err != nil {
		return "", err
	}
	pndID := pndRes.Pnd[0].Id

	configMgmtService := configMgmtPb.NewConfigurationManagementServiceClient(conn)

	sdnConfigResponse, err := configMgmtService.ExportSDNConfig(ctx, &configMgmtPb.ExportSDNConfigRequest{Timestamp: getTimestamp(), Pid: pndID})
	if err != nil {
		return "", err
	}

	return sdnConfigResponse.SdnConfigData, nil
}

// ApplySDNConfig can be used to apply a given SDN config as a string to set the testing environment as desired.
func ApplySDNConfig(conn *grpc.ClientConn, ctx context.Context, sdnConfig string) {
	pndService := ppb.NewPndServiceClient(conn)
	pndRes, err := pndService.GetPndList(ctx, &ppb.GetPndListRequest{Timestamp: getTimestamp()})
	if err != nil {
		fmt.Println(err)
	}

	// currently only support for default PND
	pndID := pndRes.Pnd[0].Id

	configMgmtService := configMgmtPb.NewConfigurationManagementServiceClient(conn)

	_, err = configMgmtService.ImportSDNConfig(ctx, &configMgmtPb.ImportSDNConfigRequest{Timestamp: getTimestamp(), Pid: pndID, SdnConfigData: sdnConfig})
	if err != nil {
		fmt.Println(err)
	}
}

func getTimestamp() int64 {
	return int64(time.Now().Nanosecond())
}