package mercury

import (
	"encoding/json"
	"fmt"
	"net/http"
	"sort"
	"strings"
	"time"

	"github.com/BurntSushi/toml"
	"github.com/golang/gddo/httputil"
	"go.sour.is/pkg/lg"
)

type root struct{}

func NewHTTP() *root {
	return &root{}
}

func (s *root) RegisterHTTP(mux *http.ServeMux) {
	mux.HandleFunc("/v1/mercury-index", s.getIndex)
	mux.HandleFunc("/v1/mercury-config", s.getConfig)
	// mux.HandleFunc("POST /v1/mercury-config", s.postConfig)
	mux.Handle("/", http.FileServer(http.Dir("./mercury/public")))
}

func (s *root) getConfig(w http.ResponseWriter, r *http.Request) {
	if r.Method == http.MethodPost {
		s.postConfig(w, r)
		return
	}

	ctx, span := lg.Span(r.Context())
	defer span.End()

	var id Ident = nobody{}

	if !id.IsActive() {
		span.RecordError(fmt.Errorf("NO_AUTH"))
		http.Error(w, "NO_AUTH", http.StatusUnauthorized)
		return
	}

	rules, err := Registry.GetRules(ctx, id)
	if err != nil {
		span.RecordError(err)
		http.Error(w, "ERR: "+err.Error(), http.StatusInternalServerError)
		return
	}
	space := r.URL.Query().Get("space")
	if space == "" {
		space = "*"
	}

	ns := ParseNamespace(space)
	ns = rules.ReduceSearch(ns)

	lis, err := Registry.GetConfig(ctx, ns.String(), "", "")
	if err != nil {
		http.Error(w, "ERR: "+err.Error(), http.StatusInternalServerError)
		return
	}

	lis, err = Registry.accessFilter(rules, lis)
	if err != nil {
		span.RecordError(err)
		http.Error(w, "ERR: "+err.Error(), http.StatusInternalServerError)
		return
	}

	sort.Sort(lis)
	var content string

	switch httputil.NegotiateContentType(r, []string{
		"text/plain",
		"application/environ",
		"application/ini",
		"application/json",
		"application/toml",
	}, "text/plain") {
	case "text/plain":
		content = lis.String()
	case "application/environ":
		content = lis.EnvString()
	case "application/ini":
		content = lis.IniString()
	case "application/json":
		json.NewEncoder(w).Encode(lis)
	case "application/toml":
		w.WriteHeader(200)
		m := make(map[string]map[string][]string)
		for _, o := range lis {
			if _, ok := m[o.Space]; !ok {
				m[o.Space] = make(map[string][]string)
			}
			for _, v := range o.List {
				m[o.Space][v.Name] = append(m[o.Space][v.Name], v.Values...)
			}
		}
		err := toml.NewEncoder(w).Encode(m)
		if err != nil {
			// log.Error(err)
			http.Error(w, "ERR", http.StatusInternalServerError)
			return
		}
	}

	fmt.Fprint(w, content)
}

func (s *root) postConfig(w http.ResponseWriter, r *http.Request) {
	ctx, span := lg.Span(r.Context())
	defer span.End()

	var id Ident = nobody{}

	if !id.IsActive() {
		span.RecordError(fmt.Errorf("NO_AUTH"))
		http.Error(w, "NO_AUTH", http.StatusUnauthorized)
		return
	}

	var config SpaceMap
	var err error
	switch r.Header.Get("Content-Type") {
	case "text/plain":
		config, err = ParseText(r.Body)
		r.Body.Close()
	case "application/x-www-form-urlencoded":
		r.ParseForm()
		config, err = ParseText(strings.NewReader(r.Form.Get("content")))
	case "multipart/form-data":
		r.ParseMultipartForm(1 << 20)
		config, err = ParseText(strings.NewReader(r.Form.Get("content")))
	}
	if err != nil {
		span.RecordError(err)
		http.Error(w, "PARSE_ERR", 400)
		return
	}

	{
		rules, err := Registry.GetRules(ctx, id)
		if err != nil {
			span.RecordError(err)
			http.Error(w, "ERR: "+err.Error(), http.StatusInternalServerError)
			return
		}

		notify, err := Registry.GetNotify(ctx, "updated")
		if err != nil {
			span.RecordError(err)
			http.Error(w, "ERR", http.StatusInternalServerError)
			return
		}
		_ = rules
		var notifyActive = make(map[string]struct{})
		var filteredConfigs Config
		for ns, c := range config {
			if !rules.GetRoles("NS", ns).HasRole("write") {
				span.AddEvent(fmt.Sprint("SKIP", ns))
				continue
			}

			span.AddEvent(fmt.Sprint("SAVE", ns))
			for _, n := range notify.Find(ns) {
				notifyActive[n.Name] = struct{}{}
			}
			filteredConfigs = append(filteredConfigs, c)
		}

		err = Registry.WriteConfig(ctx, filteredConfigs)
		if err != nil {
			span.RecordError(err)
			http.Error(w, "ERR", http.StatusInternalServerError)
			return
		}

		span.AddEvent(fmt.Sprint("SEND NOTIFYS ", notifyActive))
		for _, n := range notify {
			if _, ok := notifyActive[n.Name]; ok {
				err = Registry.SendNotify(ctx, n)
				if err != nil {
					span.RecordError(err)
					http.Error(w, "ERR", http.StatusInternalServerError)
					return
				}
			}
		}
		span.AddEvent("DONE!")
	}

	w.WriteHeader(202)
	fmt.Fprint(w, "OK")
}

func (s *root) getIndex(w http.ResponseWriter, r *http.Request) {
	ctx, span := lg.Span(r.Context())
	defer span.End()
	
	var id Ident = nobody{}

	timer := time.Now()
	defer func() { fmt.Println(time.Since(timer)) }()

	if !id.IsActive() {
		span.RecordError(fmt.Errorf("NO_AUTH"))
		http.Error(w, "NO_AUTH", http.StatusUnauthorized)
		return
	}

	rules, err := Registry.GetRules(ctx, id)
	if err != nil {
		span.RecordError(err)
		http.Error(w, "ERR: "+err.Error(), http.StatusInternalServerError)
		return
	}

	span.AddEvent(fmt.Sprint(rules))

	space := r.URL.Query().Get("space")
	if space == "" {
		space = "*"
	}

	ns := ParseNamespace(space)
	ns = rules.ReduceSearch(ns)
	span.AddEvent(ns.String())

	lis, err := Registry.GetIndex(ctx, ns.String(), "")
	if err != nil {
		span.RecordError(err)
		http.Error(w, "ERR: "+err.Error(), http.StatusInternalServerError)
		return
	}

	sort.Sort(lis)

	switch httputil.NegotiateContentType(r, []string{
		"text/plain",
		"application/json",
	}, "text/plain") {
	case "text/plain":
		_, err = fmt.Fprint(w, lis.StringList())
		span.RecordError(err)
	case "application/json":
		err = json.NewEncoder(w).Encode(lis)
		span.RecordError(err)
	}
}
