package main

import (
	"encoding/gob"
	"log"
	"net/http"
	"os"

	b "streifling.com/jason/cpolis/cmd/backend"
	c "streifling.com/jason/cpolis/cmd/calls"
	f "streifling.com/jason/cpolis/cmd/frontend"
)

func init() {
	gob.Register(b.User{})
}

func main() {
	config, err := b.HandleConfig()
	if err != nil {
		log.Fatalln(err)
	}

	logFile, err := os.OpenFile(config.LogFile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
	if err != nil {
		log.Fatalln(err)
	}
	defer logFile.Close()
	log.SetOutput(logFile)

	db, err := b.OpenDB(config.DBName)
	if err != nil {
		log.Fatalln(err)
	}
	defer db.Close()

	key, err := b.LoadKey(config.KeyFile)
	if err != nil {
		key, err = b.NewKey()
		if err != nil {
			log.Fatalln(err)
		}
		b.SaveKey(key, config.KeyFile)
	}
	store := b.NewCookieStore(key)

	mux := http.NewServeMux()
	mux.Handle("/web/static/", http.StripPrefix("/web/static/",
		http.FileServer(http.Dir(config.WebDir+"/static/"))))
	mux.HandleFunc("/", f.HomePage(config, db, store))

	mux.HandleFunc("GET /article/all-published", f.ShowPublishedArticles(config, db, store))
	mux.HandleFunc("GET /article/all-rejected", f.ShowRejectedArticles(config, db, store))
	mux.HandleFunc("GET /article/all-unpublished", f.ShowUnpublishedArticles(config, db, store))
	mux.HandleFunc("GET /article/delete/{id}", f.DeleteArticle(config, db, store))
	mux.HandleFunc("GET /article/publish/{id}", f.PublishArticle(config, db, store))
	mux.HandleFunc("GET /article/reject/{id}", f.RejectArticle(config, db, store))
	mux.HandleFunc("GET /article/review-deletion/{id}", f.ReviewArticleForDeletion(config, db, store))
	mux.HandleFunc("GET /article/review-rejected/{id}", f.ReviewRejectedArticle(config, db, store))
	mux.HandleFunc("GET /article/review-unpublished/{id}", f.ReviewUnpublishedArticle(config, db, store))
	mux.HandleFunc("GET /article/serve/{id}", c.ServeArticle(config, db))
	mux.HandleFunc("GET /article/write", f.WriteArticle(config, db, store))
	mux.HandleFunc("GET /hub", f.ShowHub(config, db, store))
	mux.HandleFunc("GET /image/serve/{pic}", c.ServeImage(config, store))
	mux.HandleFunc("GET /issue/this", f.ShowCurrentArticles(config, db, store))
	mux.HandleFunc("GET /logout", f.Logout(config, store))
	mux.HandleFunc("GET /pdf/get-list", c.ServePDFList(config))
	mux.HandleFunc("GET /pdf/serve/{id}", c.ServePDF(config))
	mux.HandleFunc("GET /rss/serve", c.ServeRSS(config))
	mux.HandleFunc("GET /tag/create", f.CreateTag(config, store))
	mux.HandleFunc("GET /user/create", f.CreateUser(config, store))
	mux.HandleFunc("GET /user/delete/{id}", f.DeleteUser(config, db, store))
	mux.HandleFunc("GET /user/edit/{id}", f.EditUser(config, db, store))
	mux.HandleFunc("GET /user/edit/self", f.EditSelf(config, db, store))
	mux.HandleFunc("GET /user/show-all/delete", f.ShowAllUsers(config, db, store, "delete"))
	mux.HandleFunc("GET /user/show-all/edit", f.ShowAllUsers(config, db, store, "edit"))

	mux.HandleFunc("POST /article/resubmit/{id}", f.ResubmitArticle(config, db, store))
	mux.HandleFunc("POST /article/submit", f.SubmitArticle(config, db, store))
	mux.HandleFunc("POST /article/upload-image", f.UploadArticleImage(config, store))
	mux.HandleFunc("POST /issue/publish", f.PublishLatestIssue(config, db, store))
	mux.HandleFunc("POST /issue/upload-image", f.UploadIssueImage(config, store))
	mux.HandleFunc("POST /login", f.Login(config, db, store))
	mux.HandleFunc("POST /tag/add", f.AddTag(config, db, store))
	mux.HandleFunc("POST /user/add", f.AddUser(config, db, store))
	mux.HandleFunc("POST /user/add-first", f.AddFirstUser(config, db, store))
	mux.HandleFunc("POST /user/update/{id}", f.UpdateUser(config, db, store))
	mux.HandleFunc("POST /user/update/self", f.UpdateSelf(config, db, store))

	log.Fatalln(http.ListenAndServe(config.Port, mux))
}