use trevors_chip8_core::parts::CPU::fill_chip8_instructions;
use trevors_chip8_toy::support;
use winit::{event::WindowEvent, event_loop::EventLoop};

use imgui::*;
/*
pub fn display_instruction_table(current_index: u32, ui: &Ui<App>) -> u32 {
        let to_display = fill_chip8_instructions();
    let mut return_index = current_index;
    let current_instruction = to_display.get(return_index as usize).unwrap();
    ui.text("Instructions");
    ui.set_next_item_width(100f32);
    ui.with_group(|| {
        ui.text(format!("{}", current_instruction.description).as_str());
        if current_instruction.arguments.is_empty() {
            ui.text("No Parameters");
        } else {
            for current_parameter in current_instruction.arguments {
                ui.text(format!("{}", current_parameter.mask).as_str());
            }
        }
        ui.text(format!("{:#}", current_instruction.arguments).as_str());
        ui.text("This is where cool stuff happens part 2");
    });
    ui.same_line();
    ui.with_group(|| {
        for (index, current) in to_display.iter().enumerate() {
            if ui
                .selectable_config(current.id.clone())
                .selected(index == return_index as usize)
                .build()
            {
                return_index = index as u32;
            }
        }
    });
    return_index
}
*/

fn chip8_instructions(initial_index: u32, ui: &mut Ui) -> u32 {
    let to_display = fill_chip8_instructions();
    let mut return_index = initial_index;
    let instruction_to_display = to_display.get(return_index as usize).unwrap();
    let instruction_window = ui
        .window("Instructions")
        .size([400.0, 400.0], Condition::FirstUseEver)
        .build(|| {
            ui.text("This should be my current instruction example");
            ui.same_line();
            ui.set_next_item_width(-1.0f32);
            for (index, instruction) in to_display.iter().enumerate() {
                if ui
                    .selectable_config(instruction.id.to_string())
                    .selected(index as u32 == return_index)
                    .build()
                {
                    return_index = index as u32;
                    println!("RETURN INDEX {return_index}");
                }
            }
        });
    return_index
}

fn main() {
    let mut value = 0;
    let choices = ["test test this is 1", "test test this is 2"];

    let mut selected_instruction = 0;

    support::simple_init(file!(), move |_, ui| {
        selected_instruction = chip8_instructions(selected_instruction, ui);

        ui.window("Hello world")
            .size([300.0, 110.0], Condition::FirstUseEver)
            .build(|| {
                ui.text_wrapped("Hello world!");
                if ui.button(choices[value]) {
                    value += 1;
                    value %= 2;
                }

                ui.button("This...is...imgui-rs!");
                ui.separator();
                let mouse_pos = ui.io().mouse_pos;
                ui.text(format!(
                    "Mouse Position: ({:.1},{:.1})",
                    mouse_pos[0], mouse_pos[1]
                ));
            });
    });
}
/*
fn main() {
    let event_loop = EventLoop::new().unwrap();

    let mut main = AppHandler::<App>::default();
    main.attributes().title = String::from("Example-420");

    event_loop.run_app(&mut main).unwrap();
}

#[derive(Default)]
pub struct App {
    pub selected_instruction_index: u32,
}

impl App {}

impl Application for App {
    type UserEvent = ();
    type Data = ();
    fn new(_: Args<()>) -> App {
        App {
            selected_instruction_index: 0,
        }
    }
    fn window_event(&mut self, args: Args<()>, _event: WindowEvent, res: EventResult) {
        if res.window_closed {
            args.event_loop.exit();
        }
    }
}

impl imgui::UiBuilder for App {
    fn do_ui(&mut self, ui: &imgui::Ui<Self>) {
        #[cfg(feature = "docking")]
        {
            ui.dock_space_over_viewport(0, imgui::DockNodeFlags::None);
        }

        let x = ui.collapsing_header_config("collapsing header");

        ui.with_group(|| {
            self.selected_instruction_index =
                display_instruction_table(self.selected_instruction_index, ui);
        });

        ui.show_demo_window(None);
    }
}
*/
