package view

import (
	"encoding/json"
	"errors"
	"fmt"
	"io/ioutil"
	"local/dndex/storage"
	"local/dndex/storage/entity"
	"local/dndex/storage/operator"
	"net/http"
	"sort"

	"github.com/buger/jsonparser"
	"go.mongodb.org/mongo-driver/bson"
)

func who(g storage.Graph, w http.ResponseWriter, r *http.Request) error {
	namespace, err := getNamespace(r)
	if err != nil {
		http.NotFound(w, r)
		return nil
	}

	switch r.Method {
	case http.MethodGet:
		return whoGet(namespace, g, w, r)
	case http.MethodPut:
		return whoPut(namespace, g, w, r)
	case http.MethodPost:
		return whoPost(namespace, g, w, r)
	case http.MethodDelete:
		return whoDelete(namespace, g, w, r)
	case http.MethodPatch:
		return whoPatch(namespace, g, w, r)
	case http.MethodTrace:
		return whoTrace(namespace, g, w, r)
	default:
		http.NotFound(w, r)
		return nil
	}
}

func whoGet(namespace string, g storage.Graph, w http.ResponseWriter, r *http.Request) error {
	id, err := getID(r)
	if err != nil {
		return whoTrace(namespace, g, w, r)
	}
	_, light := r.URL.Query()["light"]

	ones, err := g.List(r.Context(), namespace, id)
	if err != nil {
		return err
	}
	if len(ones) == 0 {
		w.WriteHeader(http.StatusNotFound)
		return json.NewEncoder(w).Encode(entity.One{})
	}
	if len(ones) > 1 {
		return errors.New("more than one result found matching " + id)
	}
	one := ones[0]

	if !light && len(one.Connections) > 0 {
		ones, err := g.List(r.Context(), namespace, one.Peers()...)
		if err != nil {
			return err
		}
		for _, another := range ones {
			another.Relationship = one.Connections[another.Name].Relationship
			one.Connections[another.Name] = another
		}
	}

	enc := json.NewEncoder(w)
	enc.SetIndent("", "   ")
	return enc.Encode(one)
}

func whoPut(namespace string, g storage.Graph, w http.ResponseWriter, r *http.Request) error {
	id, err := getID(r)
	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		return json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
	}

	body, err := ioutil.ReadAll(r.Body)
	if err != nil {
		return err
	}
	operation := entity.One{}
	if err := json.Unmarshal(body, &operation); err != nil {
		return err
	}
	if operation.Name != "" && operation.Name != id {
		http.Error(w, `{"error":"names differ between URL and request body"}`, http.StatusBadRequest)
		return nil
	}
	if operation.Modified != 0 {
		http.Error(w, `{"error":"cannot specify modified in request body"}`, http.StatusBadRequest)
		return nil
	}
	b, err := bson.Marshal(operation)
	if err != nil {
		return err
	}
	op := bson.M{}
	if err := bson.Unmarshal(b, &op); err != nil {
		return err
	}
	for k := range op {
		if _, _, _, err := jsonparser.Get(body, k); err != nil {
			delete(op, k)
		}
	}
	if err := g.Update(r.Context(), namespace, entity.One{Name: id}, operator.SetMany{Value: op}); err != nil {
		return err
	}

	return whoGet(namespace, g, w, r)
}

func whoPost(namespace string, g storage.Graph, w http.ResponseWriter, r *http.Request) error {
	id, err := getID(r)
	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		return json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
	}

	one := entity.One{}
	if err := json.NewDecoder(r.Body).Decode(&one); err != nil {
		return err
	}
	if one.Name != "" && one.Name != id {
		http.Error(w, `{"error":"names differ between URL and request body"}`, http.StatusBadRequest)
		return nil
	}
	one.Name = id
	if err := g.Insert(r.Context(), namespace, one); err != nil {
		return err
	}

	return whoGet(namespace, g, w, r)
}

func whoDelete(namespace string, g storage.Graph, w http.ResponseWriter, r *http.Request) error {
	id, err := getID(r)
	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		return json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
	}

	if err := g.Delete(r.Context(), namespace, entity.One{Name: id}); err != nil {
		return err
	}

	return json.NewEncoder(w).Encode(map[string]string{"status": "ok"})
}

func whoPatch(namespace string, g storage.Graph, w http.ResponseWriter, r *http.Request) error {
	id, err := getID(r)
	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		return json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
	}

	one := entity.One{}
	if err := json.NewDecoder(r.Body).Decode(&one); err != nil {
		return err
	}
	if one.Name == "" {
		http.Error(w, `{"error":"no name provided"}`, http.StatusBadRequest)
		return nil
	}
	relationship := one.Relationship
	one.Relationship = ""
	if err := g.Insert(r.Context(), namespace, one); err != nil {
		return err
	}
	one.Relationship = relationship
	if err := g.Update(r.Context(), namespace, entity.One{Name: id}, operator.Set{Key: fmt.Sprintf("%s.%s", entity.Connections, one.Name), Value: one.Peer()}); err != nil {
		return err
	}

	return whoGet(namespace, g, w, r)
}

func whoTrace(namespace string, g storage.Graph, w http.ResponseWriter, r *http.Request) error {
	ones, err := g.List(r.Context(), namespace)
	if err != nil {
		return err
	}
	ones = sortOnes(ones, r)
	names := make([]string, len(ones))
	for i := range ones {
		names[i] = ones[i].Name
	}
	enc := json.NewEncoder(w)
	enc.SetIndent("", "   ")
	return enc.Encode(names)
}

func getID(r *http.Request) (string, error) {
	id := r.URL.Query().Get("id")
	if id == "" {
		return "", errors.New("no id provided")
	}
	return id, nil
}

func sortOnes(ones []entity.One, r *http.Request) []entity.One {
	sorting := r.URL.Query().Get("sort")
	if sorting == "" {
		sorting = entity.Modified
	}
	order := r.URL.Query().Get("order")
	if order == "" {
		order = "-1"
	}
	asc := order != "-1"
	sort.Slice(ones, func(i, j int) bool {
		if sorting == entity.Name {
			return (asc && ones[i].Name < ones[j].Name) || (!asc && ones[i].Name > ones[j].Name)
		}
		ib, _ := json.Marshal(ones[i])
		jb, _ := json.Marshal(ones[j])
		var im, jm bson.M
		json.Unmarshal(ib, &im)
		json.Unmarshal(jb, &jm)
		iv, _ := im[sorting]
		jv, _ := jm[sorting]
		is := fmt.Sprint(iv)
		js := fmt.Sprint(jv)
		return (asc && is < js) || (!asc && is > js)
	})
	return ones
}
