use std::borrow::Cow;

use crate::prelude::renderer_types::{TextureResource, Vertex};
use crate::prelude::wgpu::{self, Device, RenderPass};
use wgpu::util::DeviceExt;
use wgpu::{BindGroup, BindGroupLayout, RenderPipeline, ShaderModule, TextureFormat};

use crate::platform::types::CameraUniform;
use crate::render::BackendState;

use crate::renderer::traits::{Shader, SpritePipeline};

pub struct CirclePipeline {
    pipeline: RenderPipeline,
    camera_bind_group: BindGroup,
    circle_bind_group_layout: BindGroupLayout,
    device: Device,
}

impl SpritePipeline for CirclePipeline {
    fn render(
        &self,
        pass: &mut RenderPass,
        _texture: Option<&TextureResource>,
        shader_data: Option<&[u8]>,
    ) {
        pass.set_pipeline(&self.pipeline);
        pass.set_bind_group(0, &self.camera_bind_group, &[]);
        // Create per-batch uniform buffer/bind group for circle params
        let Some(shader_data) = shader_data else {
            // No circle uniforms provided; nothing to bind
            return;
        };
        let circle_buffer = self
            .device
            .create_buffer_init(&wgpu::util::BufferInitDescriptor {
                label: Some("circle_uniform_buffer"),
                contents: shader_data,
                usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
            });
        let circle_bind_group = self.device.create_bind_group(&wgpu::BindGroupDescriptor {
            layout: &self.circle_bind_group_layout,
            entries: &[wgpu::BindGroupEntry {
                binding: 0,
                resource: circle_buffer.as_entire_binding(),
            }],
            label: Some("circle_bind_group"),
        });
        pass.set_bind_group(1, &circle_bind_group, &[]);
    }
}

pub struct CircleShader {
    device: Device,
    shader: ShaderModule,
    camera_bind_group_layout: BindGroupLayout,
    circle_bind_group_layout: BindGroupLayout,
}

impl CircleShader {
    pub const KEY: &'static str = "_roast2d_circle";
    pub const SHADER: &'static str = include_str!("../../../assets/shaders/circle.wgsl");

    pub fn new(state: &BackendState) -> Self {
        let device = state.device.clone();
        let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
            label: Some(Self::KEY),
            source: wgpu::ShaderSource::Wgsl(Cow::Borrowed(Self::SHADER)),
        });

        let camera_bind_group_layout =
            device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
                entries: &[wgpu::BindGroupLayoutEntry {
                    binding: 0,
                    visibility: wgpu::ShaderStages::VERTEX,
                    ty: wgpu::BindingType::Buffer {
                        ty: wgpu::BufferBindingType::Uniform,
                        has_dynamic_offset: false,
                        min_binding_size: wgpu::BufferSize::new(
                            std::mem::size_of::<CameraUniform>() as _,
                        ),
                    },
                    count: None,
                }],
                label: Some("camera_bind_group_layout"),
            });

        let circle_bind_group_layout =
            device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
                label: Some("Circle Bind Group Layout"),
                entries: &[wgpu::BindGroupLayoutEntry {
                    binding: 0,
                    visibility: wgpu::ShaderStages::FRAGMENT,
                    ty: wgpu::BindingType::Buffer {
                        ty: wgpu::BufferBindingType::Uniform,
                        has_dynamic_offset: false,
                        min_binding_size: wgpu::BufferSize::new(std::mem::size_of::<
                            crate::platform::types::CircleUniform,
                        >() as _),
                    },
                    count: None,
                }],
            });

        Self {
            shader,
            camera_bind_group_layout,
            circle_bind_group_layout,
            device,
        }
    }
}

impl Shader for CircleShader {
    fn key(&self) -> &'static str {
        Self::KEY
    }

    fn new_pipeline(
        &self,
        format: TextureFormat,
        depth_format: Option<TextureFormat>,
        camera_buffer: &wgpu::Buffer,
        transparent: bool,
    ) -> Box<dyn SpritePipeline> {
        let camera_bind_group = self.device.create_bind_group(&wgpu::BindGroupDescriptor {
            layout: &self.camera_bind_group_layout,
            entries: &[wgpu::BindGroupEntry {
                binding: 0,
                resource: camera_buffer.as_entire_binding(),
            }],
            label: Some("camera_bind_group"),
        });

        let pipeline_layout = self
            .device
            .create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
                label: Some("Circle Render Pipeline Layout"),
                bind_group_layouts: &[
                    &self.camera_bind_group_layout,
                    &self.circle_bind_group_layout,
                ],
                push_constant_ranges: &[],
            });

        let pipeline = self
            .device
            .create_render_pipeline(&wgpu::RenderPipelineDescriptor {
                cache: None,
                label: Some("Circle Render Pipeline"),
                layout: Some(&pipeline_layout),
                vertex: wgpu::VertexState {
                    module: &self.shader,
                    entry_point: Some("vs_main"),
                    buffers: &[Vertex::desc()],
                    compilation_options: Default::default(),
                },
                fragment: Some(wgpu::FragmentState {
                    module: &self.shader,
                    entry_point: Some("fs_circle"),
                    targets: &[Some(wgpu::ColorTargetState {
                        format,
                        blend: Some(wgpu::BlendState::ALPHA_BLENDING),
                        write_mask: wgpu::ColorWrites::ALL,
                    })],
                    compilation_options: Default::default(),
                }),
                primitive: wgpu::PrimitiveState {
                    topology: wgpu::PrimitiveTopology::TriangleList,
                    strip_index_format: None,
                    front_face: wgpu::FrontFace::Ccw,
                    cull_mode: Some(wgpu::Face::Back),
                    polygon_mode: wgpu::PolygonMode::Fill,
                    unclipped_depth: false,
                    conservative: false,
                },
                depth_stencil: depth_format.map(|format| wgpu::DepthStencilState {
                    format,
                    depth_write_enabled: !transparent,
                    depth_compare: wgpu::CompareFunction::Less,
                    stencil: wgpu::StencilState::default(),
                    bias: wgpu::DepthBiasState::default(),
                }),
                multisample: wgpu::MultisampleState::default(),
                multiview: None,
            });

        let pipeline = CirclePipeline {
            pipeline,
            camera_bind_group,
            circle_bind_group_layout: self.circle_bind_group_layout.clone(),
            device: self.device.clone(),
        };

        Box::new(pipeline)
    }
}
