package redis

import (
	"context"
	"crm-api/internal/app/ami/model"
	"crm-api/pkg/lfshook"
	"encoding/json"
	"fmt"
	"strings"
)

func ExtensionClean() {
	ctx := context.Background()
	keys, err := RedisInstance.Keys(ctx, "extension-*").Result()
	if err != nil {
		lfshook.NewLogger().Error("get redis keys error", err)
		return
	}
	// lfshook.NewLogger().Info("ready clean keys", keys)
	RedisInstance.Del(ctx, keys...)
}
func ExtensionDNDSet(endpointName, value string) {
	ctx := context.Background()
	RedisInstance.Set(ctx, fmt.Sprintf("extension-dnd-%s", endpointName), value, 0)
}

func ExtensionDNDGet(endpointName string) (value string) {
	ctx := context.Background()
	return RedisInstance.Get(ctx, fmt.Sprintf("extension-dnd-%s", endpointName)).Val()
}

func ExtensionDNDDel(endpointName string) {
	ctx := context.Background()
	RedisInstance.Del(ctx, fmt.Sprintf("extension-dnd-%s", endpointName))
}

func ExtensionSet(endpointName, uri, addr, roundtripusec string) {
	ctx := context.Background()
	RedisInstance.Set(ctx, fmt.Sprintf("extension-contact-uri-%s", endpointName), uri, 0)
	RedisInstance.Set(ctx, fmt.Sprintf("extension-contact-roundtripusec-%s", endpointName), roundtripusec, 0)
	addrKey := fmt.Sprintf("extension-contact-addr_delay-%s", endpointName)
	oldValue := RedisInstance.Get(ctx, addrKey).Val()
	if !strings.Contains(oldValue, addr) {
		newValue := fmt.Sprintf("%s%s[%s]\n", oldValue, addr, roundtripusec)
		//lfshook.NewLogger().Infof("new %+v, old %+v addr %+v", newValue, oldValue, addr)
		RedisInstance.Set(ctx, addrKey, newValue, 0)
	}
}

func ExtensionSetStatus(status *model.ExtensionStatus) {
	ctx := context.Background()
	statusStr, _ := json.Marshal(status)
	RedisInstance.Set(ctx, fmt.Sprintf("extension-status-%s", status.Exten), statusStr, 0)
}

func ExtensionGetStatus(extension string) (status *model.ExtensionStatus) {
	ctx := context.Background()
	status = &model.ExtensionStatus{
		StatusText: "Unavailable",
	}
	statusStr := RedisInstance.Get(ctx, fmt.Sprintf("extension-status-%s", extension)).Val()
	if len(statusStr) == 0 {
		//lfshook.NewLogger().Warnf("extension-status-%s redis value empty", extension)
		return
	}
	if err := json.Unmarshal([]byte(statusStr), status); err != nil {
		lfshook.NewLogger().Errorf("json unmarshall %s failure %+v", statusStr, err)
	}
	return
}

func ExtensionDel(endpointName, uri, addr, roundtripusec string) {
	ctx := context.Background()
	RedisInstance.Del(ctx, fmt.Sprintf("extension-contact-uri-%s", endpointName))
	RedisInstance.Del(ctx, fmt.Sprintf("extension-contact-roundtripusec-%s", endpointName))
	addrKey := fmt.Sprintf("extension-contact-addr_delay-%s", endpointName)
	oldValue := RedisInstance.Get(ctx, addrKey).Val()
	if oldValue != "" {
		addrs := strings.Split(oldValue, "\n")
		newAddrs := make([]string, 0)
		for _, item := range addrs {
			if !strings.Contains(item, addr) {
				newAddrs = append(newAddrs, item)
			}
		}
		//lfshook.NewLogger().Infof("new %+v, old %+v addr %+v", strings.Join(newAddrs, "\n"), oldValue, addr)
		RedisInstance.Set(ctx, addrKey, strings.Join(newAddrs, "\n"), 0)
	}
}

func ExtensionGet(endpointName string) (uri, addrDelay, roundtripusec, hookStatus string) {
	ctx := context.Background()
	uri = RedisInstance.Get(ctx, fmt.Sprintf("extension-contact-uri-%s", endpointName)).Val()
	addrDelay = RedisInstance.Get(ctx, fmt.Sprintf("extension-contact-addr_delay-%s", endpointName)).Val()
	roundtripusec = RedisInstance.Get(ctx, fmt.Sprintf("extension-contact-roundtripusec-%s", endpointName)).Val()
	hookStatus = RedisInstance.Get(ctx, fmt.Sprintf("hook-%s", endpointName)).Val()
	return
}
