pub mod contact;
mod current_system_widget;
mod contacts_widget;
mod fleets_widget;
mod schedule_widget;
mod time_control_widget;

use std::{borrow::Borrow, cell::RefCell};

use crate::GameState;
use crate::fleet::FleetsManager;
use crate::solar_system::{Kilometers, SolarSystem, SystemId};
use crate::timeman::Second;
use crate::ui::contact::MapContact;
use crate::ui::contacts_widget::{ContactsListWidget, FocusedContactWidget};
use crate::ui::current_system_widget::CurrentSystemWidget;
use crate::ui::fleets_widget::{FleetsControlPanel, FleetsListWidget};
use crate::ui::time_control_widget::TimeControlWidget;

#[derive(Default, Eq, PartialEq)]
pub enum LeftPanelMenuMode 
{
    #[default]
    Contacts,
    Fleets
}

#[derive(Default)]
pub struct State
{
    pub current_system: Option<SystemId>,
    pub camera_target: Option<MapContact>,
    pub selected_contact: Option<MapContact>,

    pub manual_tick: Second,
    pub auto_tick: (bool, Second),

    pub left_panel_menu: LeftPanelMenuMode,
    pub fleets_control_panel: FleetsControlPanel
}

pub fn set_theme(ctx: &egui::Context)
{
    ctx.set_theme(egui::Theme::Dark);
    ctx.style_mut_of(egui::Theme::Dark, |style| {
        let bg_color = egui::Color32::from_gray(64);
        style.visuals.panel_fill = bg_color;
        style.visuals.window_fill =bg_color;

        style.visuals.window_stroke = egui::Stroke::NONE;
        
        style.visuals.menu_corner_radius = egui::CornerRadius::ZERO;
        style.visuals.widgets.noninteractive.corner_radius = egui::CornerRadius::ZERO;
        style.visuals.widgets.inactive.corner_radius = egui::CornerRadius::ZERO;
        style.visuals.widgets.hovered.corner_radius = egui::CornerRadius::ZERO;
        style.visuals.widgets.active.corner_radius = egui::CornerRadius::ZERO;
        style.visuals.widgets.open.corner_radius = egui::CornerRadius::ZERO;

        style.visuals.widgets.hovered.bg_fill = egui::Color32::from_gray(128);

        style.visuals.widgets.inactive.bg_fill = egui::Color32::from_gray(96);
        style.visuals.widgets.inactive.weak_bg_fill = egui::Color32::from_gray(96);
        style.visuals.widgets.inactive.fg_stroke = egui::Stroke::new(2.0, egui::Color32::from_gray(196));
        
        style.visuals.widgets.active.bg_fill = egui::Color32::from_gray(128);
        style.visuals.widgets.active.fg_stroke = egui::Stroke::new(2.0, egui::Color32::from_gray(255));

        style.visuals.selection.bg_fill = egui::Color32::from_gray(96);
        style.visuals.selection.stroke.color = egui::Color32::WHITE;
        style.visuals.override_text_color = Some(egui::Color32::WHITE);
        style.override_text_style = Some(egui::TextStyle::Monospace);

        style.spacing.window_margin = egui::Margin::same(12);

        style.visuals.widgets.noninteractive.bg_stroke = egui::Stroke::NONE;

        style.interaction.selectable_labels = false;
    });
}

impl State
{
    pub fn paint(
        &mut self,
        game_state: &RefCell<GameState>,
        ui: &mut egui::Ui)
    {

        egui::Panel::top("Top panel")
            .resizable(false)
            .show_inside(ui, 
        |panel_ui| {
            self.show_topbar(&mut game_state.borrow_mut(), panel_ui);
        });

        let remaining_rect = ui.max_rect();
        let square_size = remaining_rect.height();
        let panel_width = ((remaining_rect.width() - square_size) / 2.0).floor().max(200.0);
        
        egui::Panel::left("Left panel")
            .resizable(false)
            .exact_size(panel_width)
            .show_inside(ui,
        |panel_ui| {
            self.show_left_panel(&mut game_state.borrow_mut(), panel_ui);
        });

        egui::Panel::right("Right panel")
            .resizable(false)
            .exact_size(panel_width)
            .show_inside(ui,
        |panel_ui| {

        });
}

    pub fn show_topbar(
        &mut self,
        game_state: &mut GameState,
        ui: &mut egui::Ui)
    {
        ui.horizontal(|horizontal| {
            let star_systems = game_state.solar_systems();
            let fleets_man = game_state.fleets();

            horizontal.add(CurrentSystemWidget::new(star_systems, &mut self.current_system));

            if let Some(system_id) = self.current_system {
                let current_system = &star_systems[system_id];

                horizontal.add(egui::Separator::default().spacing(12.0));
                horizontal.add(FocusedContactWidget::new(
                        self.camera_target.clone(), 
                        current_system, fleets_man));
            }

            //Horrible hack to allow for right-alignment.
            //I chose to use EGUI so i'll stick with EGUI!
            horizontal.add_space(horizontal.available_width()-224.0);
            let tcw = TimeControlWidget::new(&mut self.manual_tick, &mut self.auto_tick)
                .show_ui(horizontal);

            if let Some(tick) = tcw.inner {
                game_state.timeman_mut().advance(tick);
            }
        });
    }

    pub fn show_left_panel(
        &mut self,
        game_state: &mut GameState,
        ui: &mut egui::Ui)
    {
        if self.current_system.is_none() {
            return;
        }

        ui.vertical(|vertical| {
            let star_systems = game_state.solar_systems();
            let fleets = game_state.fleets();
            let current_system = &star_systems[self.current_system.unwrap()];
            
            vertical.horizontal(|horizontal| {
                if horizontal.selectable_label(
                        self.left_panel_menu == LeftPanelMenuMode::Contacts,
                        "Contacts")
                    .clicked() {
                    self.left_panel_menu = LeftPanelMenuMode::Contacts;
                }
                if horizontal.selectable_label(
                        self.left_panel_menu == LeftPanelMenuMode::Fleets,
                        "Fleets")
                    .clicked() {
                    self.left_panel_menu = LeftPanelMenuMode::Fleets;
                }
            });

            match self.left_panel_menu {
                LeftPanelMenuMode::Contacts => {
                    let camera_refocus = ContactsListWidget::new(
                        &mut self.selected_contact,
                        current_system,
                        fleets)
                        .show_ui(vertical);
                    if camera_refocus.inner.is_some() {
                        self.camera_target = camera_refocus.inner;
                    }
                },
                LeftPanelMenuMode::Fleets => {
                    let flw_resp = 
                        FleetsListWidget::new(fleets, &mut self.selected_contact)
                        .show_ui(vertical);


                    let fcp_resp = self.fleets_control_panel.show(
                        &self.selected_contact,
                        fleets, current_system,
                        vertical);

                    if let Some(refocus) = flw_resp.refocus {
                        self.current_system = Some(refocus.0);
                        self.camera_target = Some(refocus.1);
                    }

                    if let Some(new_fleet) = fcp_resp.new_fleet {
                        game_state.new_fleet_from_modal(new_fleet, current_system.id(), self.selected_contact);
                    }
                }
            };
        });
    }
}
