// Package client contains helper function to deal with the different client
// protocols.
package client

import (
	"crypto/tls"
	"crypto/x509"
	"fmt"
	gohttp "net/http"

	"github.com/go-git/go-git/v5/plumbing/transport"
	"github.com/go-git/go-git/v5/plumbing/transport/file"
	"github.com/go-git/go-git/v5/plumbing/transport/git"
	"github.com/go-git/go-git/v5/plumbing/transport/http"
	"github.com/go-git/go-git/v5/plumbing/transport/ssh"
)

// Protocols are the protocols supported by default.
var Protocols = map[string]transport.Transport{
	"http":  http.DefaultClient,
	"https": http.DefaultClient,
	"ssh":   ssh.DefaultClient,
	"git":   git.DefaultClient,
	"file":  file.DefaultClient,
}

var insecureClient = http.NewClient(&gohttp.Client{
	Transport: &gohttp.Transport{
		TLSClientConfig: &tls.Config{
			InsecureSkipVerify: true,
		},
	},
})

// InstallProtocol adds or modifies an existing protocol.
func InstallProtocol(scheme string, c transport.Transport) {
	if c == nil {
		delete(Protocols, scheme)
		return
	}

	Protocols[scheme] = c
}

// NewClient returns the appropriate client among of the set of known protocols:
// http://, https://, ssh:// and file://.
// See `InstallProtocol` to add or modify protocols.
func NewClient(endpoint *transport.Endpoint) (transport.Transport, error) {
	return getTransport(endpoint)
}

func getTransport(endpoint *transport.Endpoint) (transport.Transport, error) {
	if endpoint.Protocol == "https" {
		if endpoint.InsecureSkipTLS {
			return insecureClient, nil
		}

		if len(endpoint.CaBundle) != 0 {
			rootCAs, _ := x509.SystemCertPool()
			if rootCAs == nil {
				rootCAs = x509.NewCertPool()
			}
			rootCAs.AppendCertsFromPEM(endpoint.CaBundle)
			return http.NewClient(&gohttp.Client{
				Transport: &gohttp.Transport{
					TLSClientConfig: &tls.Config{
						RootCAs: rootCAs,
					},
				},
			}), nil
		}
	}

	f, ok := Protocols[endpoint.Protocol]
	if !ok {
		return nil, fmt.Errorf("unsupported scheme %q", endpoint.Protocol)
	}

	if f == nil {
		return nil, fmt.Errorf("malformed client for scheme %q, client is defined as nil", endpoint.Protocol)
	}
	return f, nil
}
