use bevy::prelude::*;
use derive_more::{Deref, DerefMut};

#[derive(Resource, Default)]
pub struct Counter {
    count: u32,
}

impl Counter {
    pub fn increase(&mut self, delta: u32) {
        self.count += delta;
    }

    pub fn reset(&mut self) {
        self.count = 0;
    }

    pub fn get_count(&self) -> u32 {
        self.count
    }
}

#[derive(Resource, Default)]
pub struct BubbleKilledTotalCount(pub Counter);

#[derive(Resource, Default, Deref, DerefMut)]
pub struct BubbleExplodedCountThisFrame(pub Counter);

#[derive(Resource, Default, Deref, DerefMut)]
pub struct BubbleSuckedCountThisFrame(pub Counter);

#[derive(Resource, Default, Deref, DerefMut)]
pub struct KirbyHitCountThisFrame(pub Counter);

fn reset_frame_counters(
    mut bubble_sucked_this_frame: ResMut<BubbleSuckedCountThisFrame>,
    mut bubble_exploded_this_frame: ResMut<BubbleExplodedCountThisFrame>,
    mut kirby_hit_count_this_frame: ResMut<KirbyHitCountThisFrame>,
    mut total_killed: ResMut<BubbleKilledTotalCount>,
) {
    total_killed.0.count += bubble_sucked_this_frame.count + bubble_exploded_this_frame.count;
    bubble_sucked_this_frame.reset();
    bubble_exploded_this_frame.reset();
    kirby_hit_count_this_frame.reset();
}

pub struct CounterPlugin;

impl Plugin for CounterPlugin {
    fn build(&self, app: &mut App) {
        app.insert_resource(BubbleSuckedCountThisFrame::default())
            .insert_resource(BubbleExplodedCountThisFrame::default())
            .insert_resource(KirbyHitCountThisFrame::default())
            .insert_resource(BubbleKilledTotalCount::default())
            .add_systems(FixedPostUpdate, reset_frame_counters);
    }
}
