#include "gmock/gmock.h"
#include "gtest/gtest.h"

#include "../src/UniversalIdentifier.hpp"

#include "mocks.h"
#include "helpers.h"


namespace
{

using namespace xmreg;


//string addr_56Vbjcz {"56VbjczrFCVZiLn66S3Qzv8QfmtcwkdXgM5cWGsXAPxoQeMQ79md51PLPCijvzk1iHbuHi91pws5B7iajTX9KTtJ4Z6HAo6"};
//string viewkey_56Vbjcz {"f747f4a4838027c9af80e6364a941b60c538e67e9ea198b6ec452b74c276de06"};
//string spendkey_56Vbjcz {"509a9761fde8856fc38e79ca705d85f979143524f178f8e2e0eb539fc050e905"};
//// ./xmr2csv --stagenet -b /home/mwo/stagenet/node_01/stagenet/lmdb/ -a 56VbjczrFCVZiLn66S3Qzv8QfmtcwkdXgM5cWGsXAPxoQeMQ79md51PLPCijvzk1iHbuHi91pws5B7iajTX9KTtJ4Z6HAo6 -v f747f4a4838027c9af80e6364a941b60c538e67e9ea198b6ec452b74c276de06
//string known_outputs_csv_2_56bCoE {"./res/outputs_stagenet_2_56Vbjcz.csv"};


inline bool
operator==(const Output::info& lhs, const JsonTx::output& rhs)
{
    return lhs.amount == rhs.amount
            && lhs.pub_key == rhs.pub_key
            && lhs.idx_in_tx == rhs.index;
}

inline bool
operator!=(const Output::info& lhs, const JsonTx::output& rhs)
{
    return !(lhs == rhs);
}

inline bool
operator==(const vector<Output::info>& lhs, const vector<JsonTx::output>& rhs)
{
    if (lhs.size() != rhs.size())
        return false;

    for (size_t i = 0; i < lhs.size(); i++)
    {
        if (lhs[i] != rhs[i])
            return false;
    }

    return true;
}

TEST(MODULAR_IDENTIFIER, OutputsRingCT)
{
    auto jtx = construct_jsontx("ddff95211b53c194a16c2b8f37ae44b643b8bd46b4cb402af961ecabeb8417b2");

    ASSERT_TRUE(jtx);

    auto identifier = make_identifier(jtx->tx,
          make_unique<Output>(&jtx->sender.address,
                              &jtx->sender.viewkey));

    identifier.identify();

    ASSERT_EQ(identifier.get<0>()->get().size(),
              jtx->sender.outputs.size());

    ASSERT_TRUE(identifier.get<0>()->get() == jtx->sender.outputs);

    ASSERT_EQ(identifier.get<0>()->get_total(),
              jtx->sender.change);
}


TEST(MODULAR_IDENTIFIER, OutputsRingCTCoinbaseTx)
{
    auto jtx = construct_jsontx("f3c84fe925292ec5b4dc383d306d934214f4819611566051bca904d1cf4efceb");

    ASSERT_TRUE(jtx);

    auto identifier = make_identifier(jtx->tx,
          make_unique<Output>(&jtx->recipients.at(0).address,
                              &jtx->recipients.at(0).viewkey));

    identifier.identify();

    ASSERT_TRUE(identifier.get<0>()->get()
                == jtx->recipients.at(0).outputs);

    ASSERT_EQ(identifier.get<0>()->get_total(),
              jtx->recipients.at(0).amount);
}

TEST(MODULAR_IDENTIFIER, MultiOutputsRingCT)
{
    auto jtx = construct_jsontx("d7dcb2daa64b5718dad71778112d48ad62f4d5f54337037c420cb76efdd8a21c");

    ASSERT_TRUE(jtx);

    for (auto const& jrecipient: jtx->recipients)
    {
        auto identifier = make_identifier(jtx->tx,
              make_unique<Output>(&jrecipient.address,
                                  &jrecipient.viewkey));

       identifier.identify();

       EXPECT_TRUE(identifier.get<0>()->get()
                    == jrecipient.outputs);
    }
}


TEST(MODULAR_IDENTIFIER, LegacyPaymentID)
{
    auto jtx = construct_jsontx("d7dcb2daa64b5718dad71778112d48ad62f4d5f54337037c420cb76efdd8a21c");

    ASSERT_TRUE(jtx);

    auto identifier = make_identifier(jtx->tx,
          make_unique<LegacyPaymentID>(nullptr, nullptr));

   identifier.identify();

   EXPECT_TRUE(identifier.get<0>()->get()
                == jtx->payment_id);
}

TEST(MODULAR_IDENTIFIER, IntegratedPaymentID)
{
    auto jtx = construct_jsontx("ddff95211b53c194a16c2b8f37ae44b643b8bd46b4cb402af961ecabeb8417b2");

    ASSERT_TRUE(jtx);

    auto identifier = make_identifier(jtx->tx,
          make_unique<IntegratedPaymentID>(
                         &jtx->recipients[0].address,
                         &jtx->recipients[0].viewkey));

   identifier.identify();

   EXPECT_TRUE(identifier.get<0>()->get()
                == jtx->payment_id8e);
}


TEST(MODULAR_IDENTIFIER, RealInputRingCT)
{
    auto jtx = construct_jsontx("d7dcb2daa64b5718dad71778112d48ad62f4d5f54337037c420cb76efdd8a21c");

    ASSERT_TRUE(jtx);

    MockMicroCore mcore;

    EXPECT_CALL(mcore, get_output_tx_and_index(_, _, _))
            .WillRepeatedly(
                Invoke(&*jtx, &JsonTx::get_output_tx_and_index));

    EXPECT_CALL(mcore, get_tx(_, _))
            .WillRepeatedly(
                Invoke(&*jtx, &JsonTx::get_tx));

    auto identifier = make_identifier(jtx->tx,
          make_unique<RealInput>(
                    &jtx->sender.address,
                    &jtx->sender.viewkey,
                    &jtx->sender.spendkey,
                    &mcore));

   identifier.identify();

   for (auto const& input_info: identifier.get<0>()->get())
       cout << input_info << endl;

   EXPECT_TRUE(identifier.get<0>()->get().size() == 2);
}

//// private testnet wallet 9wq792k9sxVZiLn66S3Qzv8QfmtcwkdXgM5cWGsXAPxoQeMQ79md51PLPCijvzk1iHbuHi91pws5B7iajTX9KTtJ4bh2tCh
//// viewkey f747f4a4838027c9af80e6364a941b60c538e67e9ea198b6ec452b74c276de06
//// spendkey 509a9761fde8856fc38e79ca705d85f979143524f178f8e2e0eb539fc050e905
//// seed: deftly large tirade gumball android leech sidekick opened iguana voice gels focus poaching itches network espionage much jailed vaults winter oatmeal eleven science siren winter
//string addr_9wq792k {"9wq792k9sxVZiLn66S3Qzv8QfmtcwkdXgM5cWGsXAPxoQeMQ79md51PLPCijvzk1iHbuHi91pws5B7iajTX9KTtJ4bh2tCh"};
//string viewkey_9wq792k {"f747f4a4838027c9af80e6364a941b60c538e67e9ea198b6ec452b74c276de06"};
//string spendkey_9wq792k {"509a9761fde8856fc38e79ca705d85f979143524f178f8e2e0eb539fc050e905"};

//// ./xmr2csv --testnet -b /home/mwo/testnet/node_01/testnet/lmdb/ -a 9wq792k9sxVZiLn66S3Qzv8QfmtcwkdXgM5cWGsXAPxoQeMQ79md51PLPCijvzk1iHbuHi91pws5B7iajTX9KTtJ4bh2tCh -v f747f4a4838027c9af80e6364a941b60c538e67e9ea198b6ec452b74c276de06
//string known_outputs_csv_9wq792k {"./res/outputs_testnet_9wq792k9.csv"};

//TEST(MODULAR_IDENTIFIER, IncomingPreRingctTransaction)
//{
//    // private testnet tx_hash 01e3d29a5b6b6e1999b9e7e48b9abe101bb93055b701b23d98513fb0646e7570
//    // the tx has only one output for 10 xmr, so we check
//    // if we can identify it.
//    string tx_hex {"0100020280e08d84ddcb0107041e6508060e1df9733757fb1563579b3a0ecefb4063dc66a75ad6bae3d0916ab862789c31f6aa0280e08d84ddcb01075f030f17031713f43315e59fbd6bacb3aa1a868edc33a0232f5031f6c573d6d7b23e752b6beee10580d0b8e1981a02e04e4d7d674381d16bf50bdc1581f75f7b2df591b7a40d73efd8f5b3c4be2a828088aca3cf02025c3fb06591f216cab833bb6deb2e173e0a9a138addf527eaff80912b8f4ff47280f882ad1602779945c0e6cbbc7def201e170a38bb9822109ddc01f330a5b00d12eb26b6fd4880e0bcefa757024f05eb53b5ebd60e502abd5a2fcb74b057b523acccd191df8076e27083ef619180c0caf384a302022dffb5574d937c1add2cf88bd374af40d577c7cc1a22c3c8805afd49840a60962101945fd9057dd42ef52164368eab1a5f430268e029f83815897d4f1e08b0e00873dbf5b4127be86ba01f23f021a40f29e29110f780fa0be36041ecf00b6c7b480edb090ba58dea640ca19aa4921a56c4b6dcbf11897c53db427dfdbc2f3072480b6c02f409b45c5d2401a19d8cfb13d9124d858d2774d9945f448f19e1e243e004588368813689c1c1be4e1242c633b7a2eb4936546fda57c99dac7fab81e40d0512e39d04ce285f96ac80f6d91ee39157189d73e198fa6b397bd34d8685dbf20e3f869043b686c291f8d4401f673978c683841ec39c45ce06564ccf87c68a080ad17bcce3d7240cd8d57ecbb190fef27578679cdd39ea3c868ab65d6d1d2c20062e921ceea054f755ceef8cd24e54078f9a5bedea2ca59d90ad277bd250c90605b3dd832aa15a4eb01080210ade74638f1558e203c644aa608147c9f596ce3c03023e99f9ca5b3cae41cbd230bc20f2b87f1e06967c852115abc7e56566ddaf09b5774568375fa0f27b45d946cfb2859f1c7a3ad6b7a8e097e955a6ee77c6db0b083dbda85b317dcd77e4be29079420bf683a91ac94feb0f788d5e3dfe72bef028768d76f9ebffd4cb2fd4a314e293df51cb43f12091632e93f4f97fdab7ab60dd50611233fbb1048dccd6478184b914710c894d8116620fcfd09d73ef304c90af210a4724c9e8adb2c47396a67944d6fe827a9b06f7a3c3b6cd2946f328cc306e0a0d194443734cc90fb94ccdb74f8fa7a19690793ddc2b0b06e52d0d4d8530ac227d58a2936fbbf18bbbc2af03443a44ff2a844be527371fedc03c50cce200e8e2b4fdb501e2fba103aafc2487be7faaa83f3894bdcfad873a6697ad930500bc56e28139ef4c6d9b8ee06390b4bcb1b6bfcc6e3136be89e3bdccff50d104906d354569aedfd8b2a5cb62b8738218760a9ebbc5dff3de038ab2e0369f28e3d0d921d28b388acdf69988b5c77120de5317be614da7c774f1f815a7137625da90f0342ca5df7bbc8515066c3d8fa37f1d69727f69e540ff66578bd0e6adf73fa074ce25809e47f06edc9d8ac9f49b4f02b8fd48ef8b03d7a6e823c6e2fc105ee0384a5a3a4bfefc41cf7240847e50121233de0083bbd904903b9879ecdd5a3b701a2196e13e438cf3980ab0b85c5e4e3595c46f034cb393b1e291e3e288678c90e9aac0abe0723520d47e94584ff65dfec8d4d1b1d2c378f87347f429a2178b10ad530bfe406441d7b21c1f0ea04920c9715434b16e6f5c561eab4e8b31040a30b280fc0e3ebc71d1d85a6711591487a50e4ca1362aae564c6e332b97da65c0c07"};

//    TX_AND_ADDR_VIEWKEY_FROM_STRING(tx_hex, addr_9wq792k, viewkey_9wq792k,
//                                    network_type::TESTNET);

//    auto identifier = make_identifier(tx,
//          make_unique<Output>(&address, &viewkey));

//    identifier.identify();

//    ASSERT_EQ(identifier.get<Output>()->get().size(), 1);

//    ASSERT_EQ(identifier.get<Output>()->get_total(), 10000000000000);
//}


//TEST(MODULAR_IDENTIFIER, OutgingPreRingctTransaction)
//{
//    // private testnet tx_hash 0889658a54ddfaa61f62fdc0393adc498e1227195d413de45c5f0e0da3f5068d
//    // the tx has only one output for 10 xmr, so we check
//    // if we can identify it.
//    string tx_hex {"0100020280e08d84ddcb0107b902e401081b2c10418ba25b21dc2e9fd3d6d8dd69c14888ab0ed429f8cc86a7edba20bdd2bde61bab0280c0caf384a30207d10194012438201e78879c4ee5f780a4fcabc5ce20716080392fc4715db6334f956e210283beed920f0580c0caf384a302020ffede2e6e4a3d613f6ebcca9d3337d54c5b65f7095cc04faf6c9408206b463c80f882ad16021b48cdc46b84e348596f35a0fb4f0107da3a7e5f8b486be6a36fc90ff052117e80d0b8e1981a02e6de1aa8176921a8d5f8ef02a12004f5183f77a601c347f84e7be18c400572e18088aca3cf02022f061ade6afb847d57d29609255089b477d2c33c34d06e13ec5982d18eb785c580c0f9decfae010211c7792d1a8d8c23bbd0e2c4c79b28a711fa4d6ff64906013ddd2b8bca42a6d444022100000000000000000000000000000000000000000000006f70656e6d6f6e65726f0189bb4ab10d9ae0b84096fe0f4b95da56e3949b71c6eca5f284a4a02c83ed174675d6b1d2e529f5e4199bb738a47e7f50fc7acbf86a3908eb0c3023013ea9e808974dd6e0c0fe2c9853d316d63a35206628790b60997a22cefb9383762ba05a0fb11987072d29d76b172700ea6b9cb61ddbdea60372b35de748f9a7fc0d18f30a02b76e272c76446d828b6cef9a2df96f867792da27f08ca116dca121f73bcf09fe4fff1a1b8a099fffafefac87f10ecdaf02f48e0b903ced2d737bcb803f0c0525588ed99e71b21c35878ce0697990bf768bc35e0e6841bf1cf91c1fc116ee0a01343ec7e52ded6bdb89571b40590834d4c04715b4eb28102dad54b455dd6d03380e9eac3c058159e7fa4f5d3da30b3eda24c7e49a87e1523e5b478efdb8020fa03a4788d214a7ec64454366525ebf66ef8692a0db97a2ff96e1ad926e315105104c7ef19f59aff25265f54489f7e0fdde6205e6f62b4beb6b0d5a2a233a4807e5460bd83dfa3929b56bd84705cee12ce7bcaca09539bd128fc9e7513a3e63022297c65ef1ac04bb1807b85fa3ef38342aa8342ec33dfe4979a1590d372218034649bbb7975d6790e04105cef27a6337996896758f4fa6a962425fc802dd790114020c4f14efe604f00b8f829d6c82bd2c9719a60c39565944998d43f299070ab78325c8a20557e03dd2e50e516503d16bd436b5af949a978097e0ce347a700c13c5ce0d039ea7bfc7cc8e975c0710e74b6b0930ffd597eaf80c0fdc18711e0e6c8e1aed3c2bd4ff035df0745e69d30ec0dff351c0d780cda8873c010419d209c19d6fc4c366911170892c6e696709069e6469018ff2c61f5f42b283bd4c590a3e5f71f7934635c1e7cca1cbf0ec91517ce65673c1b6c6c961625537b2e93208db342fb9c2f246c6d636adf66bece8bae648be290140452d63c6d87fb3ff990c86725d7bc4968cac48f10d7b6a4d35b63e3040c42f6e76e7224fa308a042b10807cd36ffe28daca3e2a755645be3f588565f9ee79b19b7f8c572a872bb61fd08c9d6473dfe2a5ae4f100744ed91e2e8817ef417309c4d0f4a5023272b87b8b00c08ee85c037417b5241e6715db7b0e932ef7e08cba00914bf485a46690f0370016d3e2ca265accec86c295cd02c13d67fa6bcc1f51803d537eaef804bdeaef05316c79554916dc5afb6ac1581436a26ac5cd687d83b4ccbbfae8e230eac2aa0c2b87f2db0b70cea9ad4cf9cb3218c4aa37da05245bc401a1ea4afcdcd833d40fadc38c6d38bd75fe63a364a0bed1c9b6656b4f803b944e5947d1c398a9eed200"};

//    TX_AND_ADDR_VIEWKEY_FROM_STRING(tx_hex, addr_9wq792k, viewkey_9wq792k,
//                                    network_type::TESTNET);

//    SPENDKEY_FROM_STRING(spendkey_9wq792k);

//    string ring_member_data_hex {"[encoded data, 4740 characters]"};

//    GET_RING_MEMBER_OUTPUTS(ring_member_data_hex);

//    GET_KNOWN_OUTPUTS(known_outputs_csv_9wq792k);

//    MOCK_MCORE_GET_OUTPUT_KEY();

//    auto identifier = make_identifier(tx,
//          make_unique<Input>(&address, &viewkey,
//                             &known_outputs, mcore.get()),
//          make_unique<RealInput>(&address, &viewkey,
//                                 &spendkey, mcore.get())
//    );

//    identifier.identify();

//    ASSERT_EQ(identifier.get<Input>()->get().size(), 2);
//    ASSERT_EQ(identifier.get<Input>()->get_total(), 17000000000000ull);


//    ASSERT_EQ(identifier.get<RealInput>()->get().size(), 2);
//    ASSERT_EQ(identifier.get<RealInput>()->get_total(), 17000000000000ull);

//}

//TEST(MODULAR_IDENTIFIER, DefaultConstruction)
//{
//    string tx_hex {"[encoded data, 44422 characters]"};

//    TX_AND_ADDR_VIEWKEY_FROM_STRING(tx_hex, addr_56Vbjcz, viewkey_56Vbjcz,
//                                    network_type::STAGENET);

//    GET_KNOWN_OUTPUTS(known_outputs_csv_2_56bCoE);

//    // the ring members data from the above tx
//    //
//    // this hex is obtained using onion explorer runnign with --enbale-as-hex flag
//    // it is hex, serializied representation of of map of key_images
//    // with corresponding ring members output_data_t info structure
//    // we do this so that we just mack all the calls, instead of
//    // accessing real blockchain for this data.
//    string ring_member_data_hex {"[encoded data, 39648 characters]"};

//    GET_RING_MEMBER_OUTPUTS(ring_member_data_hex);

//    MOCK_MCORE_GET_OUTPUT_KEY();

//    std::cout << "\n\n" << std::endl;

//    auto identifier = make_identifier(tx,
//          make_unique<Output>(&address, &viewkey),
//          make_unique<Input>(&address, &viewkey, &known_outputs,
//                             mcore.get()),
//          make_unique<LegacyPaymentID>(&address, &viewkey),
//          make_unique<IntegratedPaymentID>(&address, &viewkey));

//    identifier.identify();

//    ASSERT_EQ(identifier.get<Output>()->get().size(), 1);

//    std::cout << "\n\n" << std::endl;
//}

}
