use std::sync::Arc;
use crate::canvas::managed::handles::{CanvasImageHandle};
use crate::compute::managed::handles::{CompuKernelHandle, CompuBufferHandle};
use crate::drawables::compu_sprite::CompuSprite;
use crate::canvas::canvas_frame::Drawable;
use crate::util::vertex::VertexTypeContainer;

pub struct CompuFrame {
    // Vec<(Buffer, Kernel)>
    pub pure_compute: Vec<(
        Arc<CompuBufferHandle>,
        Arc<CompuKernelHandle>)>,

    // Vec<(Buffer, Image, Kernel)>
    pub swapped_to_image: Vec<(
        Arc<CompuBufferHandle>,
        Arc<CanvasImageHandle>,
        Arc<CompuKernelHandle>)>,

    // Vec<(Input Buffer, Output Buffer, Kernel)>
    pub swapped_to_buffer: Vec<(
        Arc<CompuBufferHandle>,
        Arc<CompuBufferHandle>,
        Arc<CompuKernelHandle>)>,

    window_size: (u32, u32),
}

impl CompuFrame {
    pub fn new(window_size: (u32, u32)) -> CompuFrame {
        CompuFrame {
            pure_compute: vec![],
            swapped_to_image: vec![],
            swapped_to_buffer: vec![],
            window_size: window_size,
        }
    }

    pub fn add(&mut self, buffer: Arc<CompuBufferHandle>, kernel: Arc<CompuKernelHandle>) {
        self.pure_compute.push((buffer, kernel));
    }

    /*
       INPUT_BUFFER -> input -> kernel -> output
                           v------------------^
       OUTPUT_BUFFER -> input X kernel X output
    */
    pub fn add_chained(&mut self,
                       input_buffer: Arc<CompuBufferHandle>,
                       output_buffer: Arc<CompuBufferHandle>,
                       kernel: Arc<CompuKernelHandle>) {
        self.swapped_to_buffer.push((input_buffer, output_buffer, kernel));
    }

    pub fn add_with_image_swap(&mut self,
                               buffer: Arc<CompuBufferHandle>,
                               kernel: Arc<CompuKernelHandle>,
                               sprite: &CompuSprite) {

        let compu_sprites = sprite.get(self.window_size);

        if compu_sprites.len() == 1 {
            if let VertexTypeContainer::ImageType(a, b) = compu_sprites.first().unwrap() {
                self.swapped_to_image.push((buffer, b.clone(), kernel))
            };
        }

    }
}