// Copyright 2014 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
	"flag"
	"log"
	"net/http"
	"runtime"
	"time"

	graceful "gopkg.in/tylerb/graceful.v1"

	"amuz.es/go/mnemonics/middleware/db"
	"amuz.es/go/mnemonics/middleware/logging"
	"amuz.es/go/mnemonics/middleware/recovery"
	"amuz.es/go/mnemonics/middleware/secure"
	"amuz.es/go/mnemonics/middleware/session"
	"amuz.es/go/mnemonics/middleware/static"
	"amuz.es/go/mnemonics/middleware/template"

	"amuz.es/go/mnemonics/route"
	"amuz.es/go/mnemonics/util"
	"github.com/gin-gonic/gin"
)

var argIp = flag.String("listen_ip", "", "IP to listen on, defaults to all IPs")
var argPort = flag.Int("port", 8080, "port to listen")
var maxProcs = flag.Int("max_procs", 0, "max number of CPUs that can be used simultaneously. Less than 1 for default (number of cores).")
var dbFile = flag.String("db", "/tmp/wiki.db", "Path to the BoltDB file")

func main() {
	flag.Parse()
	setMaxProcs()

	// Initialize db.
	var db db.DB
	if err := db.Open(*dbFile, 0600); err != nil {
		log.Fatal(err)
	}
	defer db.Close()

	store := session.NewCookieStore([]byte(util.GetRandomString(128)))
	//gin.SetMode(gin.ReleaseMode)
	r := gin.New()
	// middleware settings

	r.Use(logging.Ginrus(time.RFC3339, true))
	//	r.Use(recovery.Recovery())

	// not found handler
	r.NoRoute(recovery.RecoveryHttpError(http.StatusNotFound))
	r.NoMethod(recovery.RecoveryHttpError(http.StatusMethodNotAllowed))

	suburl := "/account"
	// suburl process
	path := r.Group(suburl)
	path.Use(recovery.RecoveryHTML())
	// handlers
	api := path.Group("/api")
	api.Use(recovery.RecoveryJSON())

	api.Use(secure.Secure(secure.Options{
		AllowedHosts: []string{},
		SSLRedirect:  false,
		SSLHost:      "",
		//		SSLProxyHeaders:       map[string]string{"X-Forwarded-Proto": "https"},
		SSLProxyHeaders:       map[string]string{},
		STSSeconds:            315360000,
		STSIncludeSubdomains:  true,
		FrameDeny:             true,
		ContentTypeNosniff:    true,
		BrowserXssFilter:      true,
		ContentSecurityPolicy: "default-src 'self'",
	}))
	api.Use(session.Sessions("mnemonics", store))

	api.GET("/auth", route.Authenticate)
	path.GET("/login", route.Login)
	path.GET("/me", route.Me)
	path.GET("/self", route.Self)
	path.GET("/metric", route.Metric)
	path.GET("/health", func(c *gin.Context) {
		c.String(200, "OK! - mnemonics services are fully operational!")
	})
	//	path.GET("/p", route.Me)
	path.GET("/favicon.ico", func(c *gin.Context) {
		c.Redirect(http.StatusMovedPermanently, "/static/favicon/images/favicon.ico")
	})

	path.GET("/manifest.json", func(c *gin.Context) {
		c.Redirect(http.StatusMovedPermanently, "/static/favicon/manifest.json")
	})

	path.GET("/browserconfig.xml", func(c *gin.Context) {
		c.Redirect(http.StatusMovedPermanently, "/static/favicon/browserconfig.xml")
	})

	path.GET("/", route.Index)

	if gin.IsDebugging() {
		// static route
		path.StaticFS("/static", static.NewDebug("asset/static"))
		r.HTMLRender = template.NewDebug("asset/template")
	} else {
		// template engine setting
		path.StaticFS("/static", static.NewRelease(""))
		r.HTMLRender = template.NewRelease("")
	}

	util.Log().Error("bootstrapped application")

	//	r.Run() // listen and server on 0.0.0.0:8080

	graceful.Run(":8080", 10*time.Second, r)

	util.Log().Error("bye!")
}

func setMaxProcs() {
	// TODO(vmarmol): Consider limiting if we have a CPU mask in effect.
	// Allow as many threads as we have cores unless the user specified a value.
	var numProcs int
	if *maxProcs < 1 {
		numProcs = runtime.NumCPU()
	} else {
		numProcs = *maxProcs
	}
	runtime.GOMAXPROCS(numProcs)

	// Check if the setting was successful.
	actualNumProcs := runtime.GOMAXPROCS(0)
	if actualNumProcs != numProcs {
		util.Log().Warnf("Specified max procs of %v but using %v", numProcs, actualNumProcs)
	}
}
