/*
Copyright © 2023 Philipp Wolfer <phw@uploadedlobster.com>

Scotty is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later version.

Scotty is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
Scotty. If not, see <https://www.gnu.org/licenses/>.
*/

package backends

import (
	"context"

	"go.uploadedlobster.com/scotty/storage"
	"golang.org/x/oauth2"
)

type databaseTokenSource struct {
	new     oauth2.TokenSource
	db      storage.Database
	service string
	tok     *oauth2.Token
}

func (s *databaseTokenSource) Token() (tok *oauth2.Token, err error) {
	tok = s.tok
	if tok == nil {
		tok, _ = s.loadToken()
	}

	if tok != nil && tok.Valid() {
		return tok, nil
	}

	if tok, err = s.new.Token(); err != nil {
		return nil, err
	}

	err = s.saveToken(tok)
	s.tok = tok
	if err != nil {
		return nil, err
	}

	return tok, err
}

func (c *databaseTokenSource) saveToken(tok *oauth2.Token) error {
	return c.db.SetOAuth2Token(c.service, tok)
}

func (c *databaseTokenSource) loadToken() (*oauth2.Token, error) {
	return c.db.GetOAuth2Token(c.service)
}

func NewDatabaseTokenSource(db storage.Database, service string, config *oauth2.Config, tok *oauth2.Token) oauth2.TokenSource {
	return &databaseTokenSource{
		db:      db,
		new:     config.TokenSource(context.Background(), tok),
		service: service,
		tok:     tok,
	}
}