use crate::UniquePubkey;
use std::{collections::BTreeSet, path::PathBuf};
use thiserror::Error;
use xor_name::XorName;
pub type Result<T, E = Error> = std::result::Result<T, E>;
#[derive(Debug, Error)]
pub enum Error {
#[error("Attempted to reload a wallet from disk, but the disk wallet is not the same as the current wallet. Wallet path: {0}")]
CurrentAndLoadedKeyMismatch(PathBuf),
#[error("Double spend attempted with cashnotes: {0:?}")]
DoubleSpendAttemptedForCashNotes(BTreeSet<UniquePubkey>),
#[error("Invalid address type")]
InvalidAddressType,
#[error("Total price exceed possible token amount")]
TotalPriceTooHigh,
#[error("Failed to send tokens due to {0}")]
CouldNotSendMoney(String),
#[error("Failed to sign a transaction: {0}")]
CouldNotSignTransaction(String),
#[error("Failed to receive transfer due to {0}")]
CouldNotReceiveMoney(String),
#[error("Failed to verify transfer validity in the network, a burnt SpendAttempt was found")]
BurntSpend,
#[error("Failed to verify transfer's parents in the network, transfer could be invalid or a parent double spent")]
UnexpectedParentSpends(crate::SpendAddress),
#[error("All the redeemed CashNotes are already spent")]
AllRedeemedCashnotesSpent,
#[error("Failed to verify transfer validity in the network {0}")]
CouldNotVerifyTransfer(String),
#[error("Failed to fetch spend from network: {0}")]
FailedToGetSpend(String),
#[error("Failed to send spend for processing: {0}")]
SpendProcessing(String),
#[error("Unconfirmed transactions still persist even after retries")]
UnconfirmedTxAfterRetries,
#[error("Main pub key doesn't match the key found when loading wallet from path: {0:#?}")]
PubKeyMismatch(std::path::PathBuf),
#[error("Main pub key not found: {0:#?}")]
PubkeyNotFound(std::path::PathBuf),
#[error("Main secret key not found: {0:#?}")]
MainSecretKeyNotFound(std::path::PathBuf),
#[error("Encrypted main secret key not found: {0:#?}")]
EncryptedMainSecretKeyNotFound(std::path::PathBuf),
#[error("Encrypted main secret key requires a password")]
EncryptedMainSecretKeyRequiresPassword,
#[error("Failed to serialize encrypted secret key: {0}")]
FailedToSerializeEncryptedKey(String),
#[error("Failed to deserialize encrypted secret key: {0}")]
FailedToDeserializeEncryptedKey(String),
#[error("Failed to encrypt secret key: {0}")]
FailedToEncryptKey(String),
#[error("Failed to decrypt secret key: {0}")]
FailedToDecryptKey(String),
#[error("Failed to parse bls key")]
FailedToParseBlsKey,
#[error("Could not decode hex string to key")]
FailedToDecodeHexToKey,
#[error("Could not serialize main key to hex: {0}")]
FailedToHexEncodeKey(String),
#[error("Could not encode cashnote to hex")]
FailedToHexEncodeCashNote,
#[error("Failed to decypher transfer with our key, maybe it was not for us")]
FailedToDecypherTransfer,
#[error("No ongoing payment found for address {0:?}")]
NoPaymentForAddress(XorName),
#[error("The payment quote made for {0:?} has expired")]
QuoteExpired(XorName),
#[error("DAG error: {0}")]
Dag(String),
#[error("Transfer error: {0}")]
Transfer(#[from] crate::TransferError),
#[error("Bls error: {0}")]
Bls(#[from] bls::error::Error),
#[error("MsgPack serialisation error:: {0}")]
Serialisation(#[from] rmp_serde::encode::Error),
#[error("MsgPack deserialisation error:: {0}")]
Deserialisation(#[from] rmp_serde::decode::Error),
#[error("I/O error: {0}")]
Io(#[from] std::io::Error),
#[error("Wallet password is incorrect")]
WalletPasswordIncorrect,
#[error("Wallet password required")]
WalletPasswordRequired,
#[error("Wallet password expired")]
WalletPasswordExpired,
#[error("Wallet is already encrypted")]
WalletAlreadyEncrypted,
}