use crate::render::wgpu::rendergraph::{PassExecutionContext, PassNode};

#[derive(Default)]
pub struct PickKeepalivePass;

impl PickKeepalivePass {
    pub fn new() -> Self {
        Self
    }
}

impl PassNode<crate::ecs::world::World> for PickKeepalivePass {
    fn name(&self) -> &str {
        "pick_keepalive_pass"
    }

    fn reads(&self) -> Vec<&str> {
        vec![]
    }

    fn writes(&self) -> Vec<&str> {
        vec![]
    }

    fn reads_writes(&self) -> Vec<&str> {
        vec!["entity_id", "depth"]
    }

    fn prepare(
        &mut self,
        _device: &wgpu::Device,
        _queue: &wgpu::Queue,
        _world: &crate::ecs::world::World,
    ) {
    }

    fn execute<'r, 'e>(
        &mut self,
        context: PassExecutionContext<'r, 'e, crate::ecs::world::World>,
    ) -> crate::render::wgpu::rendergraph::Result<
        Vec<crate::render::wgpu::rendergraph::SubGraphRunCommand<'r>>,
    > {
        let (entity_id_view, entity_id_load, entity_id_store) =
            context.get_color_attachment("entity_id")?;
        let (depth_view, depth_load, depth_store) = context.get_depth_attachment("depth")?;

        let render_pass = context
            .encoder
            .begin_render_pass(&wgpu::RenderPassDescriptor {
                label: Some("Pick Keepalive Pass"),
                color_attachments: &[Some(wgpu::RenderPassColorAttachment {
                    view: entity_id_view,
                    resolve_target: None,
                    ops: wgpu::Operations {
                        load: entity_id_load,
                        store: entity_id_store,
                    },
                    depth_slice: None,
                })],
                depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
                    view: depth_view,
                    depth_ops: Some(wgpu::Operations {
                        load: depth_load,
                        store: depth_store,
                    }),
                    stencil_ops: None,
                }),
                timestamp_writes: None,
                occlusion_query_set: None,
                multiview_mask: None,
            });
        drop(render_pass);

        Ok(context.into_sub_graph_commands())
    }
}
