package types

import (
	"encoding/binary"
	"time"

	sdk "github.com/cosmos/cosmos-sdk/types"
	"github.com/cosmos/cosmos-sdk/types/address"
	"github.com/cosmos/cosmos-sdk/types/kv"
)

const (
	// ModuleName is the name of the module
	ModuleName = "gov"

	// StoreKey is the store key string for gov
	StoreKey = ModuleName

	// RouterKey is the message route for gov
	RouterKey = ModuleName
)

// Keys for governance store
// Items are stored with the following key: values
//
// - 0x00<proposalID_Bytes>: Proposal
//
// - 0x01<endTime_Bytes><proposalID_Bytes>: activeProposalID
//
// - 0x02<endTime_Bytes><proposalID_Bytes>: inactiveProposalID
//
// - 0x03: nextProposalID
//
// - 0x04<proposalID_Bytes>: []byte{0x01} if proposalID is in the voting period
//
// - 0x10<proposalID_Bytes><depositorAddrLen (1 Byte)><depositorAddr_Bytes>: Deposit
//
// - 0x20<proposalID_Bytes><voterAddrLen (1 Byte)><voterAddr_Bytes>: Voter
//
// - 0x30: Params
var (
	ProposalsKeyPrefix            = []byte{0x00}
	ActiveProposalQueuePrefix     = []byte{0x01}
	InactiveProposalQueuePrefix   = []byte{0x02}
	ProposalIDKey                 = []byte{0x03}
	VotingPeriodProposalKeyPrefix = []byte{0x04}
	QuorumCheckQueuePrefix        = []byte{0x05}
	ActiveProposalsNumberKey      = []byte{0x06}
	LastMinDepositKey             = []byte{0x07}
	InactiveProposalsNumberKey    = []byte{0x08}
	LastMinInitialDepositKey      = []byte{0x09}

	DepositsKeyPrefix = []byte{0x10}

	VotesKeyPrefix = []byte{0x20}

	// ParamsKey is the key to query all gov params
	ParamsKey = []byte{0x30}

	// KeyConstitution is the key string used to store the chain's constitution
	KeyConstitution = []byte{0x40}

	// KeyParticipationEMA is the key string used to store the governance participation EMA
	KeyParticipationEMA                      = []byte{0x50}
	KeyConstitutionAmendmentParticipationEMA = []byte{0x60}
	KeyLawParticipationEMA                   = []byte{0x70}
)

var lenTime = len(sdk.FormatTimeBytes(time.Now()))

// GetProposalIDBytes returns the byte representation of the proposalID
func GetProposalIDBytes(proposalID uint64) (proposalIDBz []byte) {
	proposalIDBz = make([]byte, 8)
	binary.BigEndian.PutUint64(proposalIDBz, proposalID)
	return
}

// GetProposalIDFromBytes returns proposalID in uint64 format from a byte array
func GetProposalIDFromBytes(bz []byte) (proposalID uint64) {
	return binary.BigEndian.Uint64(bz)
}

// ProposalKey gets a specific proposal from the store
func ProposalKey(proposalID uint64) []byte {
	return append(ProposalsKeyPrefix, GetProposalIDBytes(proposalID)...)
}

// VotingPeriodProposalKey gets if a proposal is in voting period.
func VotingPeriodProposalKey(proposalID uint64) []byte {
	return append(VotingPeriodProposalKeyPrefix, GetProposalIDBytes(proposalID)...)
}

// ActiveProposalByTimeKey gets the active proposal queue key by endTime
func ActiveProposalByTimeKey(endTime time.Time) []byte {
	return append(ActiveProposalQueuePrefix, sdk.FormatTimeBytes(endTime)...)
}

// ActiveProposalQueueKey returns the key for a proposalID in the activeProposalQueue
func ActiveProposalQueueKey(proposalID uint64, endTime time.Time) []byte {
	return append(ActiveProposalByTimeKey(endTime), GetProposalIDBytes(proposalID)...)
}

// InactiveProposalByTimeKey gets the inactive proposal queue key by endTime
func InactiveProposalByTimeKey(endTime time.Time) []byte {
	return append(InactiveProposalQueuePrefix, sdk.FormatTimeBytes(endTime)...)
}

// InactiveProposalQueueKey returns the key for a proposalID in the inactiveProposalQueue
func InactiveProposalQueueKey(proposalID uint64, endTime time.Time) []byte {
	return append(InactiveProposalByTimeKey(endTime), GetProposalIDBytes(proposalID)...)
}

// QuorumCheckByTimeKey gets the quorum check queue key by endTime
func QuorumCheckByTimeKey(endTime time.Time) []byte {
	return append(QuorumCheckQueuePrefix, sdk.FormatTimeBytes(endTime)...)
}

// QuorumCheckQueueKey returns the key for a proposalID in the quorumCheckQueue
func QuorumCheckQueueKey(proposalID uint64, endTime time.Time) []byte {
	return append(QuorumCheckByTimeKey(endTime), GetProposalIDBytes(proposalID)...)
}

// GetActiveProposalsNumberBytes returns the byte representation of the activeProposalsNumber
func GetActiveProposalsNumberBytes(activeProposalsNumber uint64) (activeProposalsNumberBz []byte) {
	activeProposalsNumberBz = make([]byte, 8)
	binary.BigEndian.PutUint64(activeProposalsNumberBz, activeProposalsNumber)
	return
}

// GetActiveProposalsNumberFromBytes returns activeProposalsNumber in uint64 format from a byte array
func GetActiveProposalsNumberFromBytes(bz []byte) (activeProposalsNumber uint64) {
	return binary.BigEndian.Uint64(bz)
}

// GetInactiveProposalsNumberBytes returns the byte representation of the inactiveProposalsNumber
func GetInactiveProposalsNumberBytes(inactiveProposalsNumber uint64) (inactiveProposalsNumberBz []byte) {
	inactiveProposalsNumberBz = make([]byte, 8)
	binary.BigEndian.PutUint64(inactiveProposalsNumberBz, inactiveProposalsNumber)
	return
}

// GetInactiveProposalsNumberFromBytes returns inactiveProposalsNumber in uint64 format from a byte array
func GetInactiveProposalsNumberFromBytes(bz []byte) (inactiveProposalsNumber uint64) {
	return binary.BigEndian.Uint64(bz)
}

// DepositsKey gets the first part of the deposits key based on the proposalID
func DepositsKey(proposalID uint64) []byte {
	return append(DepositsKeyPrefix, GetProposalIDBytes(proposalID)...)
}

// DepositKey key of a specific deposit from the store
func DepositKey(proposalID uint64, depositorAddr sdk.AccAddress) []byte {
	return append(DepositsKey(proposalID), address.MustLengthPrefix(depositorAddr.Bytes())...)
}

// VotesKey gets the first part of the votes key based on the proposalID
func VotesKey(proposalID uint64) []byte {
	return append(VotesKeyPrefix, GetProposalIDBytes(proposalID)...)
}

// VoteKey key of a specific vote from the store
func VoteKey(proposalID uint64, voterAddr sdk.AccAddress) []byte {
	return append(VotesKey(proposalID), address.MustLengthPrefix(voterAddr.Bytes())...)
}

// Split keys function; used for iterators

// SplitProposalKey split the proposal key and returns the proposal id
func SplitProposalKey(key []byte) (proposalID uint64) {
	kv.AssertKeyLength(key[1:], 8)

	return GetProposalIDFromBytes(key[1:])
}

// SplitActiveProposalQueueKey split the active proposal key and returns the proposal id and endTime
func SplitActiveProposalQueueKey(key []byte) (proposalID uint64, endTime time.Time) {
	return splitKeyWithTime(key)
}

// SplitInactiveProposalQueueKey split the inactive proposal key and returns the proposal id and endTime
func SplitInactiveProposalQueueKey(key []byte) (proposalID uint64, endTime time.Time) {
	return splitKeyWithTime(key)
}

// SplitQuorumQueueKey split the quorum queue key and returns the proposal id and endTime
func SplitQuorumQueueKey(key []byte) (proposalID uint64, endTime time.Time) {
	return splitKeyWithTime(key)
}

// SplitKeyDeposit split the deposits key and returns the proposal id and depositor address
func SplitKeyDeposit(key []byte) (proposalID uint64, depositorAddr sdk.AccAddress) {
	return splitKeyWithAddress(key)
}

// SplitKeyVote split the votes key and returns the proposal id and voter address
func SplitKeyVote(key []byte) (proposalID uint64, voterAddr sdk.AccAddress) {
	return splitKeyWithAddress(key)
}

// private functions

func splitKeyWithTime(key []byte) (proposalID uint64, endTime time.Time) {
	kv.AssertKeyLength(key[1:], 8+lenTime)

	endTime, err := sdk.ParseTimeBytes(key[1 : 1+lenTime])
	if err != nil {
		panic(err)
	}

	proposalID = GetProposalIDFromBytes(key[1+lenTime:])
	return
}

func splitKeyWithAddress(key []byte) (proposalID uint64, addr sdk.AccAddress) {
	// Both Vote and Deposit store keys are of format:
	// <prefix (1 Byte)><proposalID (8 bytes)><addrLen (1 Byte)><addr_Bytes>
	kv.AssertKeyAtLeastLength(key, 10)
	proposalID = GetProposalIDFromBytes(key[1:9])
	kv.AssertKeyAtLeastLength(key, 11)
	addr = sdk.AccAddress(key[10:])
	return
}
