use std::convert::Infallible;

use alloy_rlp::{RlpEncodable, RlpMaxEncodedLen};
use context::result::HaltReason;
use context::result::{EVMError, ExecutionResult, InvalidTransaction};
use database::{EmptyDB, PlainAccount, State};
use hash_db::Hasher;
use plain_hasher::PlainHasher;
use revm::primitives::{keccak256, Address, Log, B256, U256};
use triehash::sec_trie_root;

pub struct TestValidationResult {
    pub logs_root: B256,
    pub state_root: B256,
}

pub fn compute_test_roots(
    exec_result: &Result<ExecutionResult<HaltReason>, EVMError<Infallible, InvalidTransaction>>,
    db: &State<EmptyDB>,
) -> TestValidationResult {
    TestValidationResult {
        logs_root: log_rlp_hash(exec_result.as_ref().map(|r| r.logs()).unwrap_or_default()),
        state_root: state_merkle_trie_root(db.cache.trie_account()),
    }
}

pub fn log_rlp_hash(logs: &[Log]) -> B256 {
    let mut out = Vec::with_capacity(alloy_rlp::list_length(logs));
    alloy_rlp::encode_list(logs, &mut out);
    keccak256(&out)
}

pub fn state_merkle_trie_root<'a>(
    accounts: impl IntoIterator<Item = (Address, &'a PlainAccount)>,
) -> B256 {
    trie_root(accounts.into_iter().map(|(address, acc)| {
        (
            address,
            alloy_rlp::encode_fixed_size(&TrieAccount::new(acc)),
        )
    }))
}

#[derive(RlpEncodable, RlpMaxEncodedLen)]
struct TrieAccount {
    nonce: u64,
    balance: U256,
    root_hash: B256,
    code_hash: B256,
}

impl TrieAccount {
    fn new(acc: &PlainAccount) -> Self {
        Self {
            nonce: acc.info.nonce,
            balance: acc.info.balance,
            root_hash: sec_trie_root::<KeccakHasher, _, _, _>(
                acc.storage
                    .iter()
                    .filter(|(_k, &v)| !v.value.is_zero())
                    .map(|(k, v)| {
                        (
                            k.to_be_bytes::<32>(),
                            alloy_rlp::encode_fixed_size(&v.value),
                        )
                    }),
            ),
            code_hash: acc.info.code_hash,
        }
    }
}

#[inline]
pub fn trie_root<I, A, B>(input: I) -> B256
where
    I: IntoIterator<Item = (A, B)>,
    A: AsRef<[u8]>,
    B: AsRef<[u8]>,
{
    sec_trie_root::<KeccakHasher, _, _, _>(input)
}

#[derive(Default, Debug, Clone, PartialEq, Eq, Hash)]
pub struct KeccakHasher;

impl Hasher for KeccakHasher {
    type Out = B256;
    type StdHasher = PlainHasher;
    const LENGTH: usize = 32;

    #[inline]
    fn hash(x: &[u8]) -> Self::Out {
        keccak256(x)
    }
}
