package gov_test

import (
	"bytes"
	"log"
	"sort"
	"testing"

	"github.com/stretchr/testify/require"

	"cosmossdk.io/depinject"
	sdklog "cosmossdk.io/log"
	"cosmossdk.io/math"

	"git.cw.tr/mukan-network/mukan-sdk/crypto/keys/ed25519"
	cryptotypes "git.cw.tr/mukan-network/mukan-sdk/crypto/types"
	"git.cw.tr/mukan-network/mukan-sdk/runtime"
	"git.cw.tr/mukan-network/mukan-sdk/testutil/configurator"
	simtestutil "git.cw.tr/mukan-network/mukan-sdk/testutil/sims"
	sdk "git.cw.tr/mukan-network/mukan-sdk/types"
	_ "git.cw.tr/mukan-network/mukan-sdk/x/auth"
	authkeeper "git.cw.tr/mukan-network/mukan-sdk/x/auth/keeper"
	authtypes "git.cw.tr/mukan-network/mukan-sdk/x/auth/types"
	_ "git.cw.tr/mukan-network/mukan-sdk/x/bank"
	bankkeeper "git.cw.tr/mukan-network/mukan-sdk/x/bank/keeper"
	_ "git.cw.tr/mukan-network/mukan-sdk/x/consensus"
	_ "git.cw.tr/mukan-network/mukan-sdk/x/distribution"
	distrkeeper "git.cw.tr/mukan-network/mukan-sdk/x/distribution/keeper"
	"git.cw.tr/mukan-network/mukan-sdk/x/gov/keeper"
	"git.cw.tr/mukan-network/mukan-sdk/x/gov/types"
	v1 "git.cw.tr/mukan-network/mukan-sdk/x/gov/types/v1"
	"git.cw.tr/mukan-network/mukan-sdk/x/gov/types/v1beta1"
	_ "git.cw.tr/mukan-network/mukan-sdk/x/params"
	_ "git.cw.tr/mukan-network/mukan-sdk/x/staking"
	stakingkeeper "git.cw.tr/mukan-network/mukan-sdk/x/staking/keeper"
	stakingtypes "git.cw.tr/mukan-network/mukan-sdk/x/staking/types"
)

var (
	valTokens           = sdk.TokensFromConsensusPower(42, sdk.DefaultPowerReduction)
	TestProposal        = v1beta1.NewTextProposal("Test", "description")
	TestDescription     = stakingtypes.NewDescription("T", "E", "S", "T", "Z")
	TestCommissionRates = stakingtypes.NewCommissionRates(math.LegacyZeroDec(), math.LegacyZeroDec(), math.LegacyZeroDec())
)

// mkTestLegacyContent creates a MsgExecLegacyContent for testing purposes.
func mkTestLegacyContent(t *testing.T) *v1.MsgExecLegacyContent {
	t.Helper()

	msgContent, err := v1.NewLegacyContent(TestProposal, authtypes.NewModuleAddress(types.ModuleName).String())
	require.NoError(t, err)

	return msgContent
}

// SortAddresses - Sorts Addresses
func SortAddresses(addrs []sdk.AccAddress) {
	byteAddrs := make([][]byte, len(addrs))

	for i, addr := range addrs {
		byteAddrs[i] = addr.Bytes()
	}

	SortByteArrays(byteAddrs)

	for i, byteAddr := range byteAddrs {
		addrs[i] = byteAddr
	}
}

// implement `Interface` in sort package.
type sortByteArrays [][]byte

func (b sortByteArrays) Len() int {
	return len(b)
}

func (b sortByteArrays) Less(i, j int) bool {
	// bytes package already implements Comparable for []byte.
	switch bytes.Compare(b[i], b[j]) {
	case -1:
		return true
	case 0, 1:
		return false
	default:
		log.Panic("not fail-able with `bytes.Comparable` bounded [-1, 1].")
		return false
	}
}

func (b sortByteArrays) Swap(i, j int) {
	b[j], b[i] = b[i], b[j]
}

// SortByteArrays - sorts the provided byte array
func SortByteArrays(src [][]byte) [][]byte {
	sorted := sortByteArrays(src)
	sort.Sort(sorted)
	return sorted
}

var pubkeys = []cryptotypes.PubKey{
	ed25519.GenPrivKey().PubKey(),
	ed25519.GenPrivKey().PubKey(),
	ed25519.GenPrivKey().PubKey(),
}

type suite struct {
	AccountKeeper      authkeeper.AccountKeeper
	BankKeeper         bankkeeper.Keeper
	GovKeeper          *keeper.Keeper
	StakingKeeper      *stakingkeeper.Keeper
	DistributionKeeper distrkeeper.Keeper
	App                *runtime.App
}

func createTestSuite(t *testing.T) suite {
	t.Helper()

	res := suite{}

	app, err := simtestutil.SetupWithConfiguration(
		depinject.Configs(
			configurator.NewAppConfig(
				configurator.ParamsModule(),
				configurator.AuthModule(),
				configurator.StakingModule(),
				configurator.BankModule(),
				configurator.GovModule(),
				configurator.ConsensusModule(),
				configurator.DistributionModule(),
			),
			depinject.Supply(sdklog.NewNopLogger()),
		),
		simtestutil.DefaultStartUpConfig(),
		&res.AccountKeeper,
		&res.BankKeeper,
		&res.GovKeeper,
		&res.DistributionKeeper,
		&res.StakingKeeper,
	)
	require.NoError(t, err)

	res.App = app
	return res
}
