package app

import (
	"fmt"
	"net"

	"dnsupdater/provider/manager"

	"dnsupdater/ip"
	"dnsupdater/ip/resolver"
)

type App struct {
	iplookup ip.NetInterfaceIPResolver

	// Updater manager
	ProviderManager *manager.Manager
}

func NewApp(config *Config) (*App, error) {
	providerMgr := manager.New()
	// providerMgr.Register("digitalocean", digitalocean.New(config.Services.DigitalOcean.Token))
	err := providerMgr.RegisterFromConfig(config.Providers)
	if err != nil {
		return nil, err
	}

	l := resolver.Get(config.Services.IPLookup)

	if l == nil {
		return nil, fmt.Errorf("Failed to load lookup service: %s", config.Services.IPLookup)
	}

	return &App{
		ProviderManager: providerMgr,
		iplookup:        ip.NewCache(ip.LookupWrapper(l)).Get,
	}, nil
}

func (a App) GetIP(iface_name string) (net.IP, error) {
	return a.iplookup(iface_name)
}
