package caddyhugo

import (
	"encoding/base64"
	"errors"
	"fmt"
	"net/http"
	"os"
	"path"
	"path/filepath"
	"strings"

	"git.stephensearles.com/stephen/caddy-hugo2/assets"
	"github.com/mholt/caddy"
	"github.com/mholt/caddy/caddyhttp/httpserver"
	"github.com/spf13/afero"
)

func (ch *CaddyHugo) ServeHTTPWithNext(next httpserver.Handler, c *caddy.Controller, w http.ResponseWriter, r *http.Request) (int, error) {
	if !ch.Match(r) {
		p := path.Join(ch.Dir, "public", r.URL.Path)
		http.ServeFile(w, r, p)
		return 200, nil
	}

	if !ch.Auth(r) {
		return http.StatusUnauthorized, errors.New("not authorized")
	}

	if strings.HasPrefix(r.URL.Path, "/hugo/publish") {
		err := ch.Publish()
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return http.StatusInternalServerError, nil
		}

		http.Redirect(w, r, "/", http.StatusFound)
		return http.StatusFound, nil
	}
	if strings.HasPrefix(r.URL.Path, "/hugo/simplemde.css") {
		w.Write(assets.MustAsset("simplemde/dist/simplemde.min.css"))
		return http.StatusOK, nil
	}
	if strings.HasPrefix(r.URL.Path, "/hugo/simplemde.js") {
		w.Write(assets.MustAsset("simplemde/debug/simplemde.js"))
		return http.StatusOK, nil
	}
	if strings.HasPrefix(r.URL.Path, "/hugo/vue.js") {
		w.Write(assets.MustAsset("js/vue.js"))
		return http.StatusOK, nil
	}
	if strings.HasPrefix(r.URL.Path, "/hugo/moment.js") {
		w.Write(assets.MustAsset("js/moment.js"))
		return http.StatusOK, nil
	}
	if strings.HasPrefix(r.URL.Path, "/hugo/font-awesome.css") {
		w.Write(assets.MustAsset("css/font-awesome.min.css"))
		return http.StatusOK, nil
	}
	if strings.HasPrefix(r.URL.Path, "/hugo/admin") {
		return ch.Admin().ServeHTTP(w, r)
	}
	if strings.HasPrefix(r.URL.Path, "/hugo/author") {
		return ch.AuthorHome().ServeHTTP(w, r)
	}
	if strings.HasPrefix(r.URL.Path, "/hugo/edit/") {
		return ch.Edit(c).ServeHTTP(w, r)
	}
	if strings.HasPrefix(r.URL.Path, "/hugo/draft/") {
		return ch.serveDraft(w, r)
	}
	if strings.HasPrefix(r.URL.Path, "/hugo/media") {
		return ch.serveMediaPage(w, r)
	}
	if strings.HasPrefix(r.URL.Path, "/hugo/upload") {
		return ch.uploadMedia(w, r)
	}
	if strings.HasPrefix(r.URL.Path, "/media/") {
		return ch.serveMedia(w, r)
	}

	http.NotFound(w, r)
	return 404, nil
}

func (ch *CaddyHugo) ServeNewContent(w http.ResponseWriter, r *http.Request) (int, error) {
	name := r.FormValue("name")
	ctype := r.FormValue("type")

	filename, err := ch.NewContent(name, ctype)
	if err != nil {
		fmt.Println("error creating new content:", err)
		return http.StatusInternalServerError, err
	}

	// serve redirect
	http.Redirect(w, r, filepath.Join("/hugo/edit/", "content", filename), http.StatusFound)
	return http.StatusFound, nil
}
func (ch *CaddyHugo) Middleware(c *caddy.Controller) httpserver.Middleware {
	return func(next httpserver.Handler) httpserver.Handler {
		return httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
			return ch.ServeHTTPWithNext(next, c, w, r)
		})
	}
}

func (ch *CaddyHugo) Auth(r *http.Request) bool {
	return true
}

func (ch *CaddyHugo) Match(r *http.Request) bool {
	if strings.HasPrefix(r.URL.Path, "/media/") {
		return true
	}

	if r.URL.Path == "/hugo" {
		return true
	}

	return strings.HasPrefix(r.URL.Path, "/hugo/")
}

func (ch *CaddyHugo) Admin() httpserver.Handler {
	return httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
		err := ch.adminTmpl.Execute(w, ch.TmplData(r, nil))
		if err != nil {
			fmt.Println(err)
			return http.StatusInternalServerError, err
		}

		return http.StatusOK, nil

	})
}

func (ch *CaddyHugo) AuthorHome() httpserver.Handler {
	return httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
		td := ch.TmplData(r, nil)
		err := ch.authorTmpl.Execute(w, td)
		if err != nil {
			fmt.Println(err)
			return http.StatusInternalServerError, err
		}

		return http.StatusOK, nil
	})
}

func (ch *CaddyHugo) Edit(c *caddy.Controller) httpserver.Handler {
	return httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
		if r.URL.Path == "/hugo/edit/new" {
			return ch.ServeNewContent(w, r)
		}

		if r.Header.Get("Upgrade") == "websocket" {
			return ch.DeltaWebsocket(w, r)
		}

		doc, err := ch.editSession(docNameFromEditRequest(r))
		if err != nil {
			fmt.Println(err)
			return http.StatusNotFound, err
		}

		err = ch.editTmpl.Execute(w, ch.TmplData(r, doc))
		if err != nil {

			fmt.Println(err)
			return http.StatusInternalServerError, err
		}

		return http.StatusOK, nil
	})
}

func (ch *CaddyHugo) serveDraft(w http.ResponseWriter, r *http.Request) (int, error) {
	pathSegments := strings.SplitN(r.URL.Path, "/", 5)
	if len(pathSegments) < 4 {

		return http.StatusNotFound, nil
	}

	encoded := pathSegments[3]

	nameBytes, err := base64.RawURLEncoding.DecodeString(encoded)
	if err != nil {
		return http.StatusNotFound, err
	}

	name := string(nameBytes)

	ch.mtx.Lock()
	defer ch.mtx.Unlock()

	docref, ok := ch.docs[ch.docFilename(name)]
	if !ok {
		return http.StatusNotFound, fmt.Errorf("draft not found")
	}

	r.URL.Path = strings.ToLower(r.URL.Path)

	prefix := "/hugo/draft/" + encoded
	r.URL.Path = path.Join("public", r.URL.Path[len(prefix):])

	page := ch.HugoSites.GetContentPage(ch.docFilename(name))
	if page == nil {
		fmt.Fprintf(w, "can't find %q to display a draft", name)
		return 404, nil
	}

	r.URL.Path = page.RelPermalink()
	http.FileServer(aferoHTTP{afero.NewBasePathFs(docref.tmpfs, path.Join(ch.Dir, "public"))}).ServeHTTP(w, r)

	return 200, nil
}

type aferoHTTP struct {
	afero.Fs
}

func (a aferoHTTP) Open(name string) (http.File, error) {
	af, err := a.Fs.Open(name)
	if os.IsExist(err) {
		err = os.ErrExist
	}
	if os.IsNotExist(err) {
		err = os.ErrNotExist
	}
	return af, err
}
