package smtp

import (
	"context"
	"fmt"
	"io"
	"log"
	"sync/atomic"

	"github.com/emersion/go-smtp"

	"ur.gs/crockery/internal/store"
)

type Server interface {
	Run()

	io.Closer
}

func NewServer(cancel context.CancelFunc, datastore store.Interface, submission bool) Server {
	out := &concrete{
		cancel: cancel,
		store:  datastore,
	}

	out.server = smtp.NewServer(out)
	out.server.Domain = datastore.Domain()
	out.server.TLSConfig = datastore.TLSConfig()

	if submission {
		out.handler = &Sender{}
		out.server.Addr = ":587"
		out.submission = true // Only allow login on submission ports
	} else {
		out.handler = &Receiver{}
		out.server.Addr = ":25"
	}

	return out
}

type concrete struct {
	name       string
	cancel     context.CancelFunc
	store      store.Interface
	server     *smtp.Server
	handler    Handler
	submission bool

	// Session IDs
	sid uint64
}

func (c *concrete) Run() {
	if err := c.server.ListenAndServe(); err != nil {
		log.Printf("Error serving SMTP %s: %v", c.server.Addr, err)
	} else {
		log.Printf("Stopped listening on SMTP %s", c.server.Addr)
	}

	c.cancel()
}

// backend implementation for go-smtp
func (c *concrete) Login(user, pass string) (smtp.User, error) {
	if !c.submission {
		return nil, fmt.Errorf("Login is disabled")
	}

	account, err := c.store.FindAccountWithPassword(user, pass)
	if err != nil {
		// Lo the real error, but don't show it to the end user
		log.Printf("Opening %s session for %s failed: %s", c.name, user, err)
		return nil, fmt.Errorf("Login failed")
	}

	session := &Session{
		ID:      atomic.AddUint64(&c.sid, uint64(1)),
		Account: account,
		Handler: c.handler,
	}

	log.Printf("Beginning %s session %d for %s", c.name, session.ID, user)
	// FIXME: TODO: Track ongoing sessions for termination or notifications

	return session, nil
}

func (c *concrete) AnonymousLogin() (smtp.User, error) {
	if c.submission {
		return nil, smtp.AuthRequiredErr
	}

	session := &Session{
		ID:      atomic.AddUint64(&c.sid, uint64(1)),
		Account: nil,
		Handler: c.handler,
	}

	log.Printf("Beginning anonymous %s session %d", c.name, session.ID)
	// FIXME: TODO: Track ongoing sessions for termination or notifications

	return session, nil
}

func (c *concrete) Close() error {
	c.cancel() // FIXME: this doesn't touch the server

	return nil
}
