package cethttp

import (
	"fmt"
	"log"
	"net/http"
	"strconv"
	"sync"

	"github.com/gorilla/mux"
	"github.com/senaduka/cetvorke/database"
)

func StartServer(waitgroup *sync.WaitGroup) {
	defer waitgroup.Done()

	r := mux.NewRouter()

	r.HandleFunc("/p/{postID}/{slug}", postHandler)
	r.HandleFunc("/", indexHandler)
	r.HandleFunc("/a/", archiveHandler)
	r.HandleFunc("/y/{year}/", yearHandler)
	r.HandleFunc("/y/{year}/m/{month}/", monthHandler)

	err := http.ListenAndServe(":8080", r)
	if err != nil {
		log.Fatal("error:", err)
	}
}

func indexHandler(w http.ResponseWriter, r *http.Request) {
	links, err := database.GetRecentLinks()
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	response := "<h1>IslamBosna</h1>\n<h2>Najnoviji članci</h2>\n"

	for _, link := range links {
		response += "<p>" + link.HTMLLink() + "</p>\n"
	}

	response = html5Page(response)

	w.Write([]byte(response))
}

func postHandler(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	fmt.Println(vars)
	postID, err := strconv.Atoi(vars["postID"])

	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	post, err := database.GetPost(postID)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	response := post.HTMLPage()

	response = html5Page(response)

	w.Write([]byte(response))
}

func archiveHandler(w http.ResponseWriter, r *http.Request) {
	links, title, err := database.GetAllYearLinks()
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	response := "<h1>" + title + "</h1>\n"

	for _, link := range links {
		response += "<p>" + link.HTMLArchiveLink() + "</p>\n"
	}

	response = html5Page(response)

	w.Write([]byte(response))
}

func yearHandler(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	year, err := strconv.Atoi(vars["year"])

	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	links, title, err := database.GetMonthLinksByYear(year)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	response := "<h1>" + title + "</h1>\n"

	for _, link := range links {
		response += "<p>" + link.HTMLArchiveMonthLink() + "</p>\n"
	}

	response = html5Page(response)

	w.Write([]byte(response))
}

func monthHandler(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	year, err := strconv.Atoi(vars["year"])

	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	month, err := strconv.Atoi(vars["month"])
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	links, title, err := database.GetLinksByMonth(year, month)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	response := "<h1>" + title + "</h1>\n"

	for _, link := range links {
		response += "<p>" + link.HTMLLink() + "</p>\n"
	}

	response = html5Page(response)
	w.Write([]byte(response))
}
