// 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"
	"net/http"
	"runtime"
	"time"

	"amuz.es/go/mnemonics/route"
	"amuz.es/go/mnemonics/util"
	"github.com/gin-gonic/gin"
	"gopkg.in/tylerb/graceful.v1"
)

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).")

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

	//gin.SetMode(gin.ReleaseMode)
	r := gin.New()
	// middleware settings
	r.Use(util.Ginrus(time.RFC3339, true))
	r.Use(gin.Recovery())

	// not found handler
	r.NoRoute(func(c *gin.Context) {
		code := http.StatusNotFound
		c.HTML(code, "page_404.html", util.Context{
			"statusCode":    code,
			"statusMessage": http.StatusText(code),
			"path":          c.Request.URL.Path,
			"method":        c.Request.Method})
	})

	// suburl process
	path := r.Group("/")

	// handlers
	path.GET("/api", route.Api)
	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("/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("/login", route.Login)
	path.GET("/", route.Index)

	if gin.IsDebugging() {
		// static route
		path.StaticFS("/static", util.StaticDebug("asset/static"))
		r.HTMLRender = util.TemplateSourceDebug("asset/template")
	} else {
		// template engine setting
		path.StaticFS("/static", util.StaticRelease(""))
		r.HTMLRender = util.TemplateSourceRelease("")
	}

	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)
	}
}
