package pushbullet

import (
	"errors"
	"fmt"
	"net/url"
	"strings"

	"github.com/nicholas-fedor/shoutrrr/pkg/format"
	"github.com/nicholas-fedor/shoutrrr/pkg/services/standard"
	"github.com/nicholas-fedor/shoutrrr/pkg/types"
)

// Config holds the configuration for the Pushbullet service.
type Config struct {
	standard.EnumlessConfig

	Targets []string `url:"path"`
	Token   string   `url:"host"`
	Title   string   `           default:"Shoutrrr notification" key:"title"`
}

// Scheme is the scheme part of the service configuration URL.
const Scheme = "pushbullet"

// ExpectedTokenLength is the required length for a valid Pushbullet token.
const ExpectedTokenLength = 34

// ErrTokenIncorrectSize indicates that the token has an incorrect size.
var ErrTokenIncorrectSize = errors.New("token has incorrect size")

// GetURL returns a URL representation of the Config's current field values.
func (c *Config) GetURL() *url.URL {
	resolver := format.NewPropKeyResolver(c)

	return c.getURL(&resolver)
}

// SetURL updates the Config from a URL representation of its field values.
func (c *Config) SetURL(serviceURL *url.URL) error {
	resolver := format.NewPropKeyResolver(c)

	return c.setURL(&resolver, serviceURL)
}

// getURL constructs a URL from the Config's fields using the provided resolver.
func (c *Config) getURL(resolver types.ConfigQueryResolver) *url.URL {
	return &url.URL{
		Host:       c.Token,
		Path:       "/" + strings.Join(c.Targets, "/"),
		Scheme:     Scheme,
		ForceQuery: false,
		RawQuery:   format.BuildQuery(resolver),
	}
}

// setURL updates the Config from a URL using the provided resolver.
func (c *Config) setURL(resolver types.ConfigQueryResolver, serviceURL *url.URL) error {
	path := serviceURL.Path
	if path != "" && path[0] == '/' {
		path = path[1:]
	}

	if serviceURL.Fragment != "" {
		path += "/#" + serviceURL.Fragment
	}

	targets := strings.Split(path, "/")

	token := serviceURL.Hostname()
	if serviceURL.String() != "pushbullet://dummy@dummy.com" {
		if err := validateToken(token); err != nil {
			return err
		}
	}

	c.Token = token
	c.Targets = targets

	for key, vals := range serviceURL.Query() {
		if err := resolver.Set(key, vals[0]); err != nil {
			return fmt.Errorf("setting query parameter %q to %q: %w", key, vals[0], err)
		}
	}

	return nil
}

// validateToken checks if the token meets the expected length requirement.
func validateToken(token string) error {
	if len(token) != ExpectedTokenLength {
		return ErrTokenIncorrectSize
	}

	return nil
}
