// DragonX Wallet - ImGui Edition
// Copyright 2024-2026 The Hush Developers
// Released under the GPLv3

#include "shield_dialog.h"
#include "../../app.h"
#include "../../config/version.h"
#include "../../rpc/rpc_client.h"
#include "../../rpc/rpc_worker.h"
#include "../../util/i18n.h"
#include "../notifications.h"
#include "../schema/ui_schema.h"
#include "../material/draw_helpers.h"
#include "imgui.h"

#include <vector>
#include <string>

namespace dragonx {
namespace ui {

// Static state
static bool s_open = false;
static ShieldDialog::Mode s_mode = ShieldDialog::Mode::ShieldCoinbase;
static char s_from_address[512] = "*";
static char s_to_address[512] = "";
static double s_fee = DRAGONX_DEFAULT_FEE;
static int s_utxo_limit = 50; // overridden by schema at runtime
static bool s_operation_pending = false;
static std::string s_operation_id;
static std::string s_status_message;
static int s_selected_zaddr_idx = -1;

void ShieldDialog::show(Mode mode)
{
    s_mode = mode;
    s_open = true;
    s_operation_pending = false;
    s_status_message.clear();
    s_operation_id.clear();
    
    if (mode == Mode::ShieldCoinbase) {
        strncpy(s_from_address, "*", sizeof(s_from_address));
    } else {
        s_from_address[0] = '\0';
    }
    s_to_address[0] = '\0';
    s_fee = DRAGONX_DEFAULT_FEE;
    s_utxo_limit = (int)schema::UI().drawElement("business", "utxo-limit").size;
    s_selected_zaddr_idx = -1;
}

void ShieldDialog::showShieldCoinbase(const std::string& fromAddress)
{
    show(Mode::ShieldCoinbase);
    strncpy(s_from_address, fromAddress.c_str(), sizeof(s_from_address) - 1);
}

void ShieldDialog::showMerge()
{
    show(Mode::MergeToAddress);
}

void ShieldDialog::render(App* app)
{
    if (!s_open) return;

    auto& S = schema::UI();
    auto win = S.window("dialogs.shield");
    auto addrLbl = S.label("dialogs.shield", "address-label");
    auto addrFrontLbl = S.label("dialogs.shield", "address-front-label");
    auto addrBackLbl = S.label("dialogs.shield", "address-back-label");
    auto feeInput = S.input("dialogs.shield", "fee-input");
    auto utxoInput = S.input("dialogs.shield", "utxo-limit-input");
    auto shieldBtn = S.button("dialogs.shield", "shield-button");
    auto cancelBtn = S.button("dialogs.shield", "cancel-button");

    const char* title = (s_mode == Mode::ShieldCoinbase) 
        ? TR("shield_title") 
        : TR("merge_title");

    if (material::BeginOverlayDialog(title, &s_open, win.width, 0.94f)) {
        const auto& state = app->getWalletState();
        
        // Description
        if (s_mode == Mode::ShieldCoinbase) {
            ImGui::TextWrapped("%s", TR("shield_description"));
        } else {
            ImGui::TextWrapped("%s", TR("merge_description"));
        }
        
        ImGui::Spacing();
        ImGui::Separator();
        ImGui::Spacing();

        // From address (for shield coinbase)
        if (s_mode == Mode::ShieldCoinbase) {
            ImGui::Text("%s", TR("shield_from_address"));
            ImGui::SetNextItemWidth(-1);
            ImGui::InputText("##FromAddr", s_from_address, sizeof(s_from_address));
            ImGui::TextDisabled("%s", TR("shield_wildcard_hint"));
            ImGui::Spacing();
        }

        // To address (z-address dropdown)
        ImGui::Text("%s", TR("shield_to_address"));
        
        // Get z-addresses for dropdown
        std::string to_display = s_to_address[0] ? s_to_address : TR("shield_select_z");
        if (to_display.length() > static_cast<size_t>(addrLbl.truncate)) {
            to_display = to_display.substr(0, addrFrontLbl.truncate) + "..." + to_display.substr(to_display.length() - addrBackLbl.truncate);
        }
        
        ImGui::SetNextItemWidth(-1);
        if (ImGui::BeginCombo("##ToAddr", to_display.c_str())) {
            for (size_t i = 0; i < state.z_addresses.size(); i++) {
                const auto& addr = state.z_addresses[i];
                std::string label = addr.address;
                if (label.length() > static_cast<size_t>(addrLbl.truncate)) {
                    label = label.substr(0, addrFrontLbl.truncate) + "..." + label.substr(label.length() - addrBackLbl.truncate);
                }
                
                bool selected = (s_selected_zaddr_idx == static_cast<int>(i));
                if (ImGui::Selectable(label.c_str(), selected)) {
                    s_selected_zaddr_idx = static_cast<int>(i);
                    strncpy(s_to_address, addr.address.c_str(), sizeof(s_to_address) - 1);
                }
                if (selected) {
                    ImGui::SetItemDefaultFocus();
                }
            }
            ImGui::EndCombo();
        }
        
        ImGui::Spacing();

        // Fee
        ImGui::Text("%s", TR("fee_label"));
        ImGui::SetNextItemWidth(feeInput.width);
        ImGui::InputDouble("##Fee", &s_fee, 0.0001, 0.001, "%.8f");
        if (s_fee < 0.0) s_fee = 0.0;          // no negative fee
        if (s_fee > 1.0) s_fee = 1.0;          // guard a fat-fingered huge fee (mirrors utxo clamp)
        ImGui::SameLine();
        ImGui::TextDisabled("DRGX");
        
        ImGui::Spacing();

        // UTXO limit
        ImGui::Text("%s", TR("shield_utxo_limit"));
        ImGui::SetNextItemWidth(utxoInput.width);
        ImGui::InputInt("##Limit", &s_utxo_limit);
        ImGui::SameLine();
        ImGui::TextDisabled("%s", TR("shield_max_utxos"));
        if (s_utxo_limit < 1) s_utxo_limit = 1;
        if (s_utxo_limit > 100) s_utxo_limit = 100;

        ImGui::Spacing();
        ImGui::Separator();
        ImGui::Spacing();

        // Status message
        if (!s_status_message.empty()) {
            if (s_operation_pending) {
                ImGui::TextColored(ImVec4(1.0f, 0.8f, 0.0f, 1.0f), "%s", s_status_message.c_str());
            } else {
                ImGui::TextWrapped("%s", s_status_message.c_str());
            }
            ImGui::Spacing();
        }

        // Buttons
        bool can_submit = !s_operation_pending && s_to_address[0] != '\0';
        
        if (!can_submit) ImGui::BeginDisabled();
        
        const char* btn_label = (s_mode == Mode::ShieldCoinbase) ? TR("shield_funds") : TR("merge_funds");
        if (material::StyledButton(btn_label, ImVec2(shieldBtn.width, 0), S.resolveFont(shieldBtn.font))) {
            s_operation_pending = true;
            s_status_message = "Submitting operation...";
            
            if (s_mode == Mode::ShieldCoinbase) {
                std::string from(s_from_address), to(s_to_address);
                double fee = s_fee;
                int limit = s_utxo_limit;
                if (app->worker()) {
                    app->worker()->post([app, rpc = app->rpc(), from, to, fee, limit]() -> rpc::RPCWorker::MainCb {
                        nlohmann::json result;
                        std::string error;
                        try {
                            rpc::RPCClient::TraceScope trace("Send tab / Shield coinbase");
                            result = rpc->call("z_shieldcoinbase", {from, to, fee, limit});
                        } catch (const std::exception& e) {
                            error = e.what();
                        }
                        return [app, result, error]() {
                            s_operation_pending = false;
                            if (error.empty()) {
                                s_operation_id = result.value("opid", "");
                                s_status_message = "Operation submitted: " + s_operation_id;
                                Notifications::instance().success(TR("shield_started"));
                                // Register with the shared poller so an async failure is
                                // surfaced (and balances refresh) even after this dialog closes.
                                app->trackOperation(s_operation_id);
                            } else {
                                s_status_message = "Error: " + error;
                                Notifications::instance().error("Shield failed: " + error);
                            }
                        };
                    });
                }
            } else {
                std::vector<std::string> fromAddrs;
                fromAddrs.push_back("ANY_TADDR");
                std::string to(s_to_address);
                double fee = s_fee;
                int limit = s_utxo_limit;
                if (app->worker()) {
                    app->worker()->post([app, rpc = app->rpc(), fromAddrs, to, fee, limit]() -> rpc::RPCWorker::MainCb {
                        nlohmann::json addrs = nlohmann::json::array();
                        for (const auto& addr : fromAddrs) addrs.push_back(addr);
                        nlohmann::json result;
                        std::string error;
                        try {
                            rpc::RPCClient::TraceScope trace("Send tab / Merge funds");
                            result = rpc->call("z_mergetoaddress", {addrs, to, fee, 0, limit});
                        } catch (const std::exception& e) {
                            error = e.what();
                        }
                        return [app, result, error]() {
                            s_operation_pending = false;
                            if (error.empty()) {
                                s_operation_id = result.value("opid", "");
                                s_status_message = "Operation submitted: " + s_operation_id;
                                Notifications::instance().success(TR("merge_started"));
                                // Register with the shared poller so an async failure is
                                // surfaced (and balances refresh) even after this dialog closes.
                                app->trackOperation(s_operation_id);
                            } else {
                                s_status_message = "Error: " + error;
                                Notifications::instance().error("Merge failed: " + error);
                            }
                        };
                    });
                }
            }
        }
        
        if (!can_submit) ImGui::EndDisabled();
        
        ImGui::SameLine();
        
        if (material::StyledButton(TR("cancel"), ImVec2(cancelBtn.width, 0), S.resolveFont(cancelBtn.font))) {
            s_open = false;
        }

        // Show operation status if we have an opid
        if (!s_operation_id.empty()) {
            ImGui::Spacing();
            ImGui::Separator();
            ImGui::Spacing();
            
            ImGui::Text(TR("shield_operation_id"), s_operation_id.c_str());
            
            if (material::StyledButton(TR("shield_check_status"), ImVec2(0,0), S.resolveFont(shieldBtn.font))) {
                std::string opid = s_operation_id;
                if (app->worker()) {
                    app->worker()->post([rpc = app->rpc(), opid]() -> rpc::RPCWorker::MainCb {
                        nlohmann::json result;
                        std::string error;
                        try {
                            rpc::RPCClient::TraceScope trace("Send tab / Shield operation status");
                            nlohmann::json ids = nlohmann::json::array();
                            ids.push_back(opid);
                            result = rpc->call("z_getoperationstatus", {ids});
                        } catch (const std::exception& e) {
                            error = e.what();
                        }
                        return [result, error]() {
                            if (error.empty() && result.is_array() && !result.empty()) {
                                auto& op = result[0];
                                std::string status = op.value("status", "unknown");
                                if (status == "success") {
                                    s_status_message = TR("shield_completed");
                                    Notifications::instance().success(TR("shield_merge_done"));
                                } else if (status == "failed") {
                                    std::string errMsg = op.value("error", nlohmann::json{}).value("message", "Unknown error");
                                    s_status_message = "Operation failed: " + errMsg;
                                    Notifications::instance().error("Operation failed: " + errMsg);
                                } else if (status == "executing") {
                                    s_status_message = TR("shield_in_progress");
                                } else {
                                    s_status_message = "Status: " + status;
                                }
                            } else if (!error.empty()) {
                                s_status_message = "Error checking status: " + error;
                            }
                        };
                    });
                }
            }
        }
        material::EndOverlayDialog();
    }
}

} // namespace ui
} // namespace dragonx
