package meepdaimgr

import (
	"encoding/json"

	log "github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-logger"
)

func convertPolygonToJson(area *Polygon) string {
	if area == nil { // This is not an error
		return ""
	}
	jsonInfo, err := json.Marshal(*area)
	if err != nil {
		log.Error(err.Error())
		return ""
	}
	return string(jsonInfo)
}

func convertJsonToPolygon(jsonInfo string) *Polygon {
	var obj Polygon
	if jsonInfo != "" { // Empty string is processed as an empty array
		err := json.Unmarshal([]byte(jsonInfo), &obj)
		if err != nil {
			log.Error(err.Error())
			return nil
		}
	}
	return &obj
}

func convertCivicAddressElementToJson(civicAddressElement *CivicAddressElement) string {
	if civicAddressElement == nil { // This is not an error
		return ""
	}
	jsonInfo, err := json.Marshal(*civicAddressElement)
	if err != nil {
		log.Error(err.Error())
		return ""
	}
	return string(jsonInfo)
}

func convertJsonToCivicAddressElement(jsonInfo string) *CivicAddressElement {
	var obj CivicAddressElement
	if jsonInfo != "" { // Empty string is processed as an empty array
		err := json.Unmarshal([]byte(jsonInfo), &obj)
		if err != nil {
			log.Error(err.Error())
			return nil
		}
	}
	return &obj
}

func NilToEmptyString(s *string) string {
	if s == nil {
		return ""
	}

	return *s
}

func EmptyToNilString(s *string) *string {
	if s != nil && *s == "" {
		return nil
	}

	return s
}

func NilToEmptyUri(s *Uri) Uri {
	if s == nil {
		return ""
	}

	return *s
}

func EmptyToNilUri(s *Uri) *Uri {
	if s != nil && *s == "" {
		return nil
	}

	return s
}
