use crate::ui::ui_helpers::StatusIndicator;
use eframe::emath::Vec2;
use eframe::epaint::Color32;
use common::prelude::Register;
use emulator_core::EmulatorHandle;
use emulator_core::processor::RunningState;
use crate::ui::ui_helpers::PaddedWidget;

pub struct Controller {
    emulator: EmulatorHandle,
    visible: bool,
}

impl Controller {
    pub fn new(state: EmulatorHandle) -> Self {
        Self {
            emulator: state,
            visible: false,
        }
    }

    pub fn ui(&mut self, ui: &mut egui::Ui) {

        self.emulator.request_update();

        egui::Frame::new()
            .inner_margin(10.0)
            .show(ui, |ui| {
                ui.horizontal(|ui| {

                    let status = self.emulator.status();
                    let padding = Vec2::new(14.0, 8.0);

                    match status {
                        RunningState::Paused | RunningState::Halted | RunningState::Breakpoint(_) => {
                            if PaddedWidget::button("▶ Run").color(Color32::GREEN).ui(ui).clicked() {
                                self.emulator.resume()
                            }
                        },

                        RunningState::Running => {
                            if PaddedWidget::button("⏸ Pause").color(Color32::YELLOW).ui(ui).clicked() {
                                self.emulator.pause()
                            }
                        }

                        RunningState::Shutdown => {
                            ui.add_enabled_ui(false, |ui| {
                                PaddedWidget::button("▶ Run").color(Color32::GREEN).ui(ui);
                            });
                        }
                    }

                    if PaddedWidget::button("⟳ Reset All").color(Color32::YELLOW).ui(ui).clicked() {
                        self.emulator.reset();
                    }

                    // Step
                    // if ui.button("Step").clicked() {
                    //     state
                    //         .cmd_sender
                    //         .send(Command::Step(self.step_amount))
                    //         .unwrap_or_else(|_| {
                    //             state.error_log.push("Failed to send command".to_string());
                    //         });
                    // }

                    // if ui
                    //     .text_edit_singleline(&mut self.step_amount_input)
                    //     .changed()
                    // {
                    //     self.step_amount = if let Ok(amount) = self.step_amount_input.parse() {
                    //         amount
                    //     } else {
                    //         state
                    //             .error_log
                    //             .push("Unable to parse step amount".to_string());
                    //         1
                    //     }
                    // }


                    status.clone().ui(ui, padding);

                    ui.separator();

                    let pcx = self.emulator.get_register(Register::Pcx);
                    let clock = self.emulator.clock();
                    let time = self.emulator.time().as_micros();

                    let mips = if time != 0 {
                        clock as u128 / time
                    } else {
                        0
                    };

                    PaddedWidget::label(format!("Clock: {clock}")).ui(ui);
                    PaddedWidget::label( format!("Pcx: 0x{pcx:08X}")).ui(ui);
                    PaddedWidget::label( format!("Elapsed: {} micros", time)).ui(ui);
                    PaddedWidget::label(format!("Freq: {}MHz", mips)).ui(ui);
                });
            });



    }
}
