use crate::GameState;
use crate::player::{Player, PlayerAction};
use crate::util::{single, single_mut};
use bevy::{prelude::*, window::PrimaryWindow};
use bevy_egui::{EguiContexts, EguiPlugin, egui};
use std::f32::consts::TAU;

use super::objects::Interact;

pub(super) fn plugin(app: &mut App) {
    app.add_plugins(EguiPlugin)
        .init_resource::<InteractionOpportunity>()
        .add_systems(
            Update,
            (update_interaction_opportunities, display_interaction_prompt)
                .chain()
                .run_if(in_state(GameState::Playing)),
        );
}

#[derive(Debug, Clone, Eq, PartialEq, Resource, Default)]
struct InteractionOpportunity(Option<Entity>);

fn update_interaction_opportunities(
    player_query: Query<&GlobalTransform, With<Player>>,
    parents: Query<&Parent>,
    target_query: Query<
        (Entity, &GlobalTransform),
        (Without<Player>, Without<Camera>, With<Interact>),
    >,
    mut interaction_opportunity: ResMut<InteractionOpportunity>,
) {
    interaction_opportunity.0 = None;
    let player_transform = single!(player_query);

    let (target_entity, target_transform) = single!(target_query);

    let player_translation = player_transform.translation();
    let target_translation = target_transform.translation();
    if player_translation.distance(target_translation) <= 2.0 {
        interaction_opportunity.0.replace(target_entity);
    }
}

fn is_facing_target(
    player: Vec3,
    target: Vec3,
    camera_transform: Transform,
    camera: &Camera,
) -> bool {
    let camera_to_player = camera_transform.forward();
    let player_to_target = target - player;
    let angle = camera_to_player.angle_between(player_to_target);
    angle < TAU / 8.
}

fn display_interaction_prompt(
    interaction_opportunity: Res<InteractionOpportunity>,
    mut egui_contexts: EguiContexts,
    action: Query<&PlayerAction, With<Player>>,
    primary_windows: Query<&Window, With<PrimaryWindow>>,
) {
    let Some(opportunity) = interaction_opportunity.0 else {
        return;
    };
    let window = single!(primary_windows);

    egui::Window::new("Interaction")
        .collapsible(false)
        .title_bar(false)
        .auto_sized()
        .fixed_pos(egui::Pos2::new(window.width() / 2., window.height() / 2.))
        .show(egui_contexts.ctx_mut(), |ui| {
            ui.label("E: Pick Up");
        });

    let action = single!(action);
}
