package service

import (
	"fmt"
	"strings"

	"be.ems/src/framework/config"
	"github.com/tsmask/go-oam/framework/utils/parse"
)

// GetOMCYaml 获取OMC网元配置文件
func (r NeConfig) GetOMCYaml(paramName string) []map[string]any {
	if paramName == "notificationEmail" {
		notificationEmailData := config.Get("notification.email").(map[string]any)
		for k, v := range notificationEmailData {
			if k == "password" && v != nil {
				notificationEmailData[k] = parse.SafeContent(fmt.Sprint(v))
			}
		}
		return []map[string]any{notificationEmailData}
	}
	if paramName == "notificationSMSC" {
		notificationSMSCData := config.Get("notification.smsc").(map[string]any)
		for k, v := range notificationSMSCData {
			if k == "password" && v != nil {
				notificationSMSCData[k] = parse.SafeContent(fmt.Sprint(v))
			}
		}
		return []map[string]any{notificationSMSCData}
	}
	if paramName == "trace" {
		traceData := config.Get("trace").(map[string]any)
		return []map[string]any{traceData}
	}
	return []map[string]any{}
}

// ModifyOMCYaml 修改OMC网元配置文件
func (r NeConfig) ModifyOMCYaml(paramName, loc string, paramData any) error {
	neConfig := r.FindByNeTypeAndParamName("OMC", paramName)
	if neConfig.ParamType == "list" {
		if paramName == "notificationEmail" {
			notificationEmailData := config.Get("notification.email").(map[string]any)
			for k, v := range paramData.(map[string]any) {
				keyLower := strings.ToLower(k)
				// 改程序内
				notificationEmailData[keyLower] = v
				// 改文件
				switch keyLower {
				case "enabled":
					pattern := `enabled: (true|false) # email enable`
					replacement := fmt.Sprintf(`enabled: %v # email enable`, v)
					return config.SedReplace(pattern, replacement)
				case "list":
					pattern := `list: ".*" # toEmail`
					replacement := fmt.Sprintf(`list: "%v" # toEmail`, v)
					return config.SedReplace(pattern, replacement)
				case "title":
					pattern := `title: ".*" # email title`
					replacement := fmt.Sprintf(`title: "%v" # email title`, v)
					return config.SedReplace(pattern, replacement)
				case "smtp":
					pattern := `smtp: ".*" # email smtp`
					replacement := fmt.Sprintf(`smtp: "%v" # email smtp`, v)
					return config.SedReplace(pattern, replacement)
				case "port":
					pattern := `port: [0-9]+ # email port`
					replacement := fmt.Sprintf(`port: %v # email port`, v)
					return config.SedReplace(pattern, replacement)
				case "user":
					pattern := `user: ".*" # email user`
					replacement := fmt.Sprintf(`user: "%v" # email user`, v)
					return config.SedReplace(pattern, replacement)
				case "password":
					pattern := `password: ".*" # email password`
					replacement := fmt.Sprintf(`password: "%v" # email password`, v)
					return config.SedReplace(pattern, replacement)
				}
			}
		}
		if paramName == "notificationSMSC" {
			notificationSMSCData := config.Get("notification.smsc").(map[string]any)
			for k, v := range paramData.(map[string]any) {
				keyLower := strings.ToLower(k)
				// 改程序内
				notificationSMSCData[keyLower] = v
				// 改文件
				switch keyLower {
				case "enabled":
					pattern := `enabled: (true|false) # smsc enable`
					replacement := fmt.Sprintf(`enabled: %v # smsc enable`, v)
					return config.SedReplace(pattern, replacement)
				case "list":
					pattern := `list: ".*" # toMobile`
					replacement := fmt.Sprintf(`list: "%v" # toMobile`, v)
					return config.SedReplace(pattern, replacement)
				case "addr":
					pattern := `addr: ".*" # smsc addr`
					replacement := fmt.Sprintf(`addr: "%v" # smsc addr`, v)
					return config.SedReplace(pattern, replacement)
				case "systemid":
					pattern := `systemid: ".*" # smsc system id`
					replacement := fmt.Sprintf(`systemid: "%v" # smsc system id`, v)
					return config.SedReplace(pattern, replacement)
				case "systemtype":
					pattern := `systemtype: ".*" # smsc system type`
					replacement := fmt.Sprintf(`systemtype: "%v" # smsc system type`, v)
					return config.SedReplace(pattern, replacement)
				case "password":
					pattern := `password: ".*" # smsc password`
					replacement := fmt.Sprintf(`password: "%v" # smsc password`, v)
					return config.SedReplace(pattern, replacement)
				case "coding":
					pattern := `coding: [0-9]+ # smsc codingMap`
					replacement := fmt.Sprintf(`coding: %v # smsc codingMap`, v)
					return config.SedReplace(pattern, replacement)
				case "servicenumber":
					pattern := `servicenumber: ".*" # smsc service number`
					replacement := fmt.Sprintf(`servicenumber: "%v" # smsc service number`, v)
					return config.SedReplace(pattern, replacement)

				}
			}
		}
		if paramName == "trace" {
			traceData := config.Get("trace").(map[string]any)
			for k, v := range paramData.(map[string]any) {
				keyLower := strings.ToLower(k)
				// 改程序内
				traceData[keyLower] = v
				// 改文件
				switch keyLower {
				case "enabled":
					pattern := `enabled: (true|false) # trace enabled`
					replacement := fmt.Sprintf(`enabled: %v # trace enabled`, v)
					return config.SedReplace(pattern, replacement)
				case "host":
					pattern := `host: ".*" # trace host`
					replacement := fmt.Sprintf(`host: "%v" # trace host`, v)
					return config.SedReplace(pattern, replacement)
				case "port":
					pattern := `port: [0-9]+ # trace port`
					replacement := fmt.Sprintf(`port: %v # trace port`, v)
					return config.SedReplace(pattern, replacement)
				}
			}
			// 重开跟踪任务信令数据通道UDP
			// service.NewTraceTask.CreateUDP(true) 方法导致循环引用，抛给上层调用
			return nil
		}
	}
	if neConfig.ParamType == "array" {
		// TODO
	}
	return fmt.Errorf("not found method paramName")
}
