package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"golang.org/x/oauth2"
	"golang.org/x/oauth2/facebook"
	"golang.org/x/oauth2/google"
	"net/http"
)

var config *oauth2.Config

func getConfig(snsType SNSType) *oauth2.Config {

	if snsType == FB_TYPE {
		return &oauth2.Config{
			ClientID:     FB_CLIENT_ID,
			ClientSecret: FB_CLIENT_SECRET,
			RedirectURL:  REDIRECT_URL,
			Scopes:       []string{"email", "public_profile"},
			Endpoint:     facebook.Endpoint,
		}
	} else if snsType == GG_TYPE {

		return &oauth2.Config{
			ClientID:     GG_CLIENT_ID,
			ClientSecret: GG_CLIENT_SECRET,
			RedirectURL:  REDIRECT_URL,
			Scopes:       []string{"https://www.googleapis.com/auth/userinfo.email"},
			Endpoint:     google.Endpoint,
		}
	}

	return nil
}
func Home(c *gin.Context) {
	config = getConfig(GG_TYPE)
	codeUrl := config.AuthCodeURL("")
	http.Redirect(c.Writer, c.Request, codeUrl, http.StatusTemporaryRedirect)

}

func SNSCallback(c *gin.Context) {
	c.Writer.Header().Set("Content-Type", "text/html; charset=utf-8")

	code := c.Request.FormValue("code")

	info := GetSNSInfo(config, code, GG_TYPE)
	c.Writer.Write([]byte(fmt.Sprintf("Usern emailis %s and email is %s<br>", info.Name, info.Email)))
}
func main() {
	r := gin.Default()

	r.GET("/", Home)
	r.GET("/SNSCallback", SNSCallback)

	r.Run()
}
