redgold 0.1.23

p2p database / compute engine & portfolio target models
use eframe::egui;
use eframe::egui::{ComboBox, Context, TextEdit, Ui};
use itertools::{Either, Itertools};
use redgold_keys::util::mnemonic_support::WordsPass;
use crate::gui::app_loop::LocalState;
use crate::gui::common::{copy_to_clipboard, editable_text_input_copy, medium_data_item};
use crate::gui::tables::text_table;


pub fn key_source(ui: &mut Ui, ls: &mut LocalState) -> bool {

    let mut has_changed = false;
    // Combo box to choose mnemonic
    ui.horizontal(|ui| {

        ui.label("Key Source");
        let string = ls.wallet_state.selected_key_name.clone();
        ComboBox::from_label("")
            .selected_text(string.clone())
            .show_ui(ui, |ui| {
                for style in ls.local_stored_state.key_names() {
                    ui.selectable_value(&mut ls.wallet_state.selected_key_name, style.clone(), style.to_string());
                }
            });
        if ls.wallet_state.selected_key_name != ls.wallet_state.last_selected_key_name {
            has_changed = true;
            ls.wallet_state.last_selected_key_name = string.clone();
            ls.wallet_state.active_hot_mnemonic = None;
            ls.wallet_state.active_hot_private_key_hex = None;
            ls.wallet_state.mnemonic_or_key_checksum = "".to_string();
            // TODO: Really this could be refactored in an Enum that has multiple direct value
            // structs, but for now we'll just use the Either type
            // also the state storage should account for that as well.
            let opt = ls.local_stored_state.by_key(&string).map(|key| {
                match key {
                    Either::Left(mnemonic) => {
                        ls.wallet_state.active_hot_mnemonic = Some(mnemonic.mnemonic.clone());
                    }
                    Either::Right(private_key) => {
                        ls.wallet_state.active_hot_private_key_hex = Some(private_key.key_hex);
                    }
                }
            });
            if opt.is_none() {
                ls.wallet_state.active_hot_mnemonic = Some(ls.wallet_state.hot_mnemonic_default.clone());
            }
            ls.wallet_state.update_hot_mnemonic_or_key_info();
        }
        // add_new_key_button(ls, ui);
    });
    has_changed
}

pub fn add_new_key_button(ls: &mut LocalState, ui: &mut Ui) {
    if ui.button("Add Hot Mnemonic / Private Key").clicked() {
        ls.wallet_state.add_new_key_window = true;
    }
}