package config

import (
	"os"
	"strconv"
	"time"

	"github.com/drone/envsubst"
	"github.com/pkg/errors"
	"gopkg.in/yaml.v3"
)

type InterpolatedString string

func (is *InterpolatedString) UnmarshalYAML(value *yaml.Node) error {
	var str string

	if err := value.Decode(&str); err != nil {
		return errors.WithStack(err)
	}

	str, err := envsubst.Eval(str, getEnv)
	if err != nil {
		return errors.WithStack(err)
	}

	*is = InterpolatedString(str)

	return nil
}

type InterpolatedInt int

func (ii *InterpolatedInt) UnmarshalYAML(value *yaml.Node) error {
	var str string

	if err := value.Decode(&str); err != nil {
		return errors.Wrapf(err, "could not decode value '%v' (line '%d') into string", value.Value, value.Line)
	}

	str, err := envsubst.Eval(str, getEnv)
	if err != nil {
		return errors.WithStack(err)
	}

	intVal, err := strconv.ParseInt(str, 10, 32)
	if err != nil {
		return errors.Wrapf(err, "could not parse int '%v',  line '%d'", str, value.Line)
	}

	*ii = InterpolatedInt(int(intVal))

	return nil
}

type InterpolatedFloat float64

func (ifl *InterpolatedFloat) UnmarshalYAML(value *yaml.Node) error {
	var str string

	if err := value.Decode(&str); err != nil {
		return errors.Wrapf(err, "could not decode value '%v' (line '%d') into string", value.Value, value.Line)
	}

	str, err := envsubst.Eval(str, getEnv)
	if err != nil {
		return errors.WithStack(err)
	}

	floatVal, err := strconv.ParseFloat(str, 32)
	if err != nil {
		return errors.Wrapf(err, "could not parse float '%v',  line '%d'", str, value.Line)
	}

	*ifl = InterpolatedFloat(floatVal)

	return nil
}

type InterpolatedBool bool

func (ib *InterpolatedBool) UnmarshalYAML(value *yaml.Node) error {
	var str string

	if err := value.Decode(&str); err != nil {
		return errors.Wrapf(err, "could not decode value '%v' (line '%d') into string", value.Value, value.Line)
	}

	str, err := envsubst.Eval(str, getEnv)
	if err != nil {
		return errors.WithStack(err)
	}

	boolVal, err := strconv.ParseBool(str)
	if err != nil {
		return errors.Wrapf(err, "could not parse bool '%v',  line '%d'", str, value.Line)
	}

	*ib = InterpolatedBool(boolVal)

	return nil
}

var getEnv = os.Getenv

type InterpolatedMap struct {
	Data map[string]any
}

func (im *InterpolatedMap) UnmarshalYAML(value *yaml.Node) error {
	var data map[string]any

	if err := value.Decode(&data); err != nil {
		return errors.Wrapf(err, "could not decode value '%v' (line '%d') into map", value.Value, value.Line)
	}

	interpolated, err := im.interpolateRecursive(data)
	if err != nil {
		return errors.WithStack(err)
	}

	im.Data = interpolated.(map[string]any)

	return nil
}

func (im InterpolatedMap) interpolateRecursive(data any) (any, error) {
	switch typ := data.(type) {
	case map[string]any:
		for key, value := range typ {
			value, err := im.interpolateRecursive(value)
			if err != nil {
				return nil, errors.WithStack(err)
			}

			typ[key] = value
		}

	case string:
		value, err := envsubst.Eval(typ, getEnv)
		if err != nil {
			return nil, errors.WithStack(err)
		}

		data = value

	case []any:
		for idx := range typ {
			value, err := im.interpolateRecursive(typ[idx])
			if err != nil {
				return nil, errors.WithStack(err)
			}

			typ[idx] = value
		}
	}

	return data, nil
}

type InterpolatedStringSlice []string

func (iss *InterpolatedStringSlice) UnmarshalYAML(value *yaml.Node) error {
	var data []string

	if err := value.Decode(&data); err != nil {
		return errors.Wrapf(err, "could not decode value '%v' (line '%d') into map", value.Value, value.Line)
	}

	for index, value := range data {
		value, err := envsubst.Eval(value, getEnv)
		if err != nil {
			return errors.WithStack(err)
		}

		data[index] = value
	}

	*iss = data

	return nil
}

type InterpolatedDuration time.Duration

func (id *InterpolatedDuration) UnmarshalYAML(value *yaml.Node) error {
	var str string

	if err := value.Decode(&str); err != nil {
		return errors.Wrapf(err, "could not decode value '%v' (line '%d') into string", value.Value, value.Line)
	}

	str, err := envsubst.Eval(str, getEnv)
	if err != nil {
		return errors.WithStack(err)
	}

	duration, err := time.ParseDuration(str)
	if err != nil {
		nanoseconds, err := strconv.ParseInt(str, 10, 64)
		if err != nil {
			return errors.Wrapf(err, "could not parse duration '%v',  line '%d'", str, value.Line)
		}

		duration = time.Duration(nanoseconds)
	}

	*id = InterpolatedDuration(duration)

	return nil
}

func (id *InterpolatedDuration) MarshalYAML() (interface{}, error) {
	duration := time.Duration(*id)

	return duration.String(), nil
}

func NewInterpolatedDuration(d time.Duration) *InterpolatedDuration {
	id := InterpolatedDuration(d)
	return &id
}