// Copyright (C) 2021 Storj Labs, Inc.
// See LICENSE for copying information.

package rpc

import (
	"context"
	"math/rand"
	"os"
	"strconv"
	"sync/atomic"
	"time"

	"storj.io/common/storj"
)

// quicRolloutPercent is the node rollout between 0 and 100.
// -1 means no satellites either.
const quicRolloutPercentDefault = -1

var quicRolloutPercent int

func init() {
	if percent, err := strconv.Atoi(os.Getenv("STORJ_QUIC_ROLLOUT_PERCENT")); err == nil {
		quicRolloutPercent = percent
	} else {
		quicRolloutPercent = quicRolloutPercentDefault
	}
}

var (
	rngState atomic.Uint64
	rngInc   = uint64(time.Now().UnixNano())
)

func intn(n int) int {
	return rand.New(rand.NewSource(int64(rngState.Add(rngInc)))).Intn(n)
}

type quicRolloutPercentKey struct{}

// WithQUICRolloutPercent sets the QUIC rollout percent for any dials that
// use the returned context.
func WithQUICRolloutPercent(ctx context.Context, percent int) context.Context {
	return context.WithValue(ctx, quicRolloutPercentKey{}, percent)
}

// QUICRolloutPercent returns the currently configured QUIC rollout percent
// for the given context.
func QUICRolloutPercent(ctx context.Context) int {
	if ctxpercent, ok := ctx.Value(quicRolloutPercentKey{}).(int); ok {
		return ctxpercent
	}
	return quicRolloutPercent
}

func checkQUICRolloutState(ctx context.Context, id storj.NodeID) bool {
	percent := QUICRolloutPercent(ctx)

	if percent >= 100 {
		return true
	}

	if percent < 0 {
		return false
	}

	if quicRolloutSatelliteIDs[id] {
		return true
	}

	if intn(100) < percent {
		return true
	}

	return false
}

// setQUICRollout set up forced TCP if not yet set, based on rollout rules.
func setQUICRollout(ctx context.Context, nodeURL storj.NodeURL) context.Context {
	forced := ctx.Value(hybridConnectorForcedKind{})
	if forced != nil && forced != "" {
		return ctx
	}
	if !checkQUICRolloutState(ctx, nodeURL.ID) {
		ctx = WithForcedKind(ctx, "tcp")
	}
	return ctx
}

var quicRolloutSatelliteIDs = map[storj.NodeID]bool{
	{0xa2, 0x8b, 0x4f, 0x4, 0xe1, 0xb, 0xae, 0x85, 0xd6, 0x7f, 0x4c,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