package fetchlink

import (
	"encoding/json"
	"fmt"
	"strings"

	"be.ems/src/framework/logger"
	"be.ems/src/framework/utils/fetch"
	"be.ems/src/modules/network_element/model"
)

// NeTraceInfo 网元跟踪任务信息
func NeTraceInfo(neInfo model.NeInfo, traceId string) (map[string]any, error) {
	// 跟踪任务信息
	neUrl := fmt.Sprintf("http://%s:%d/api/rest/traceManagement/v1/subscriptions?id=%s", neInfo.IP, neInfo.Port, traceId)
	resBytes, err := fetch.Get(neUrl, nil, 30_000)
	if err != nil {
		logger.Warnf("NeTraceInfo Get \"%s\"", neUrl)
		logger.Errorf("NeTraceInfo %s", err.Error())
		return nil, fmt.Errorf("NeService Trace Info API Error")
	}

	// 序列化结果
	var resData map[string]any
	err = json.Unmarshal(resBytes, &resData)
	if err != nil {
		logger.Errorf("NeTraceInfo Unmarshal %s", err.Error())
		return nil, err
	}
	return resData, nil
}

// NeTraceAdd 网元跟踪任务新增
func NeTraceAdd(neInfo model.NeInfo, data map[string]any) (map[string]any, error) {
	// 跟踪任务创建
	neUrl := fmt.Sprintf("http://%s:%d/api/rest/traceManagement/v1/subscriptions", neInfo.IP, neInfo.Port)
	resBytes, err := fetch.PostJSON(neUrl, data, nil)
	var resData map[string]any
	if err != nil {
		errStr := err.Error()
		logger.Warnf("NeTraceAdd POST \"%s\"", neUrl)
		if !(strings.HasPrefix(errStr, "201") || strings.HasPrefix(errStr, "400")) {
			logger.Errorf("NeTraceAdd %s", errStr)
			return nil, fmt.Errorf("NeService Trace Add API Error")
		}
	}

	// 200 成功无数据时
	if len(resBytes) == 0 {
		return resData, nil
	}

	// 序列化结果
	err = json.Unmarshal(resBytes, &resData)
	if err != nil {
		logger.Errorf("NeTraceAdd Unmarshal %s", err.Error())
		return nil, err
	}
	return resData, nil
}

// NeTraceEdit 网元跟踪任务编辑
func NeTraceEdit(neInfo model.NeInfo, data map[string]any) (map[string]any, error) {
	// 网元参数配置新增（array）
	neUrl := fmt.Sprintf("http://%s:%d/api/rest/traceManagement/v1/subscriptions", neInfo.IP, neInfo.Port)
	resBytes, err := fetch.PutJSON(neUrl, data, nil)
	var resData map[string]any
	if err != nil {
		errStr := err.Error()
		logger.Warnf("NeTraceEdit PUT \"%s\"", neUrl)
		if strings.HasPrefix(errStr, "201") || strings.HasPrefix(errStr, "204") {
			return resData, nil
		}
		logger.Errorf("NeTraceEdit %s", errStr)
		return nil, fmt.Errorf("NeService Trace Edit API Error")
	}

	// 200 成功无数据时
	if len(resBytes) == 0 {
		return resData, nil
	}

	// 序列化结果
	err = json.Unmarshal(resBytes, &resData)
	if err != nil {
		logger.Errorf("NeTraceEdit Unmarshal %s", err.Error())
		return nil, err
	}
	return resData, nil
}

// NeTraceDelete 网元跟踪任务删除
func NeTraceDelete(neInfo model.NeInfo, traceId string) (map[string]any, error) {
	// 网元参数配置删除（array）
	neUrl := fmt.Sprintf("http://%s:%d/api/rest/traceManagement/v1/subscriptions?id=%s", neInfo.IP, neInfo.Port, traceId)
	resBytes, err := fetch.Delete(neUrl, nil)
	var resData map[string]any
	if err != nil {
		errStr := err.Error()
		logger.Warnf("NeTraceDelete Delete \"%s\"", neUrl)
		if strings.HasPrefix(errStr, "201") || strings.HasPrefix(errStr, "204") {
			return resData, nil
		}
		logger.Errorf("NeTraceDelete %s", errStr)
		return nil, fmt.Errorf("NeService Trace Delete API Error")
	}

	// 200 成功无数据时
	if len(resBytes) == 0 {
		return resData, nil
	}

	// 序列化结果
	err = json.Unmarshal(resBytes, &resData)
	if err != nil {
		logger.Errorf("NeTraceDelete Unmarshal %s", err.Error())
		return nil, err
	}
	return resData, nil
}
