use std::{fmt::Display};
use std::error::Error;

use wgpu::RenderPass;

use crate::tacmap::camera::Camera;
use crate::wgpuctx::{WgpuCtx, pipeline::RenderPipelineBuilder};

#[derive(Debug, Clone)]
pub struct NeedsRebuildError;

impl Display for NeedsRebuildError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "RenderState needs to be rebuilt before updating!")
    }
}

impl Error for NeedsRebuildError {}

pub struct GridRenderer
{
    pipeline: wgpu::RenderPipeline,
}

impl GridRenderer
{
    pub fn new(wgpuctx: &WgpuCtx)
    -> Self
    {
        let shader = wgpuctx.create_shader(
            wgpu::include_wgsl!(concat!(
                    env!("CARGO_MANIFEST_DIR"),
                    "/assets/shaders/tacmap/grid.wgsl")
        ));

        let render_pipeline = RenderPipelineBuilder::new(&shader)
            .add_bindgroup(&Camera::bindgroup_layout(wgpuctx))
            .cull_mode(None)
            .build(Some("Tactical map grid pipeline"), wgpuctx);

        Self {
            pipeline: render_pipeline
        }
    }

    pub fn render(
        &self,
        pass: &mut RenderPass,
        camera: &Camera
    )
    {
        pass.set_pipeline(&self.pipeline);
        pass.set_bind_group(0, camera.bindgroup(), &[]);

        pass.draw(0..6, 0..1);
    }
} //impl GridRenderer
