// Copyright 2019 Sorint.lab
//
// 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 api

import (
	"net/http"

	"github.com/rs/zerolog"
	"github.com/sorintlab/errors"

	"agola.io/agola/internal/services/gateway/action"
	"agola.io/agola/internal/util"
	gwapitypes "agola.io/agola/services/gateway/api/types"
)

type OAuth2CallbackHandler struct {
	log zerolog.Logger
	ah  *action.ActionHandler
}

func NewOAuth2CallbackHandler(log zerolog.Logger, ah *action.ActionHandler) *OAuth2CallbackHandler {
	return &OAuth2CallbackHandler{log: log, ah: ah}
}

func (h *OAuth2CallbackHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	res, err := h.do(w, r)
	if util.HTTPError(w, err) {
		h.log.Err(err).Send()
		return
	}

	if err := util.HTTPResponse(w, http.StatusOK, res); err != nil {
		h.log.Err(err).Send()
	}
}

func (h *OAuth2CallbackHandler) do(w http.ResponseWriter, r *http.Request) (*gwapitypes.RemoteSourceAuthResult, error) {
	ctx := r.Context()
	query := r.URL.Query()
	code := query.Get("code")
	state := query.Get("state")

	cresp, err := h.ah.HandleOauth2Callback(ctx, code, state)
	if err != nil {
		return nil, errors.WithStack(err)
	}

	var response interface{}
	switch cresp.RequestType {
	case action.RemoteSourceRequestTypeCreateUserLA:
		authresp := cresp.Response.(*action.CreateUserLAResponse)
		response = &gwapitypes.CreateUserLAResponse{
			LinkedAccount: &gwapitypes.LinkedAccount{
				ID:                  authresp.LinkedAccount.ID,
				RemoteUserID:        authresp.LinkedAccount.RemoteUserID,
				RemoteUserName:      authresp.LinkedAccount.RemoteUserName,
				RemoteUserAvatarURL: authresp.LinkedAccount.RemoteUserAvatarURL,
				RemoteSourceID:      authresp.LinkedAccount.RemoteUserID,
			},
		}

	case action.RemoteSourceRequestTypeLoginUser:
		authresp := cresp.Response.(*action.LoginUserResponse)

		http.SetCookie(w, authresp.Cookie)
		http.SetCookie(w, authresp.SecondaryCookie)

		response = &gwapitypes.LoginUserResponse{
			User: createUserResponse(authresp.User),
		}

	case action.RemoteSourceRequestTypeAuthorize:
		authresp := cresp.Response.(*action.AuthorizeResponse)
		response = &gwapitypes.AuthorizeResponse{
			RemoteUserInfo: &gwapitypes.UserInfo{
				ID:        authresp.RemoteUserInfo.ID,
				LoginName: authresp.RemoteUserInfo.LoginName,
				Email:     authresp.RemoteUserInfo.Email,
			},
			RemoteSourceName: authresp.RemoteSourceName,
		}

	case action.RemoteSourceRequestTypeRegisterUser:
		response = &gwapitypes.RegisterUserResponse{}
	}

	res := &gwapitypes.RemoteSourceAuthResult{
		RequestType: string(cresp.RequestType),
		Response:    response,
	}

	return res, nil
}
