use crate::canvas::canvas_frame::Drawable;
use crate::util::vertex::{VertexTypeContainer, ColorVertex3D};

///
#[derive(Debug, Clone)]
pub struct Rect {
    position: (f32, f32),
    size: (f32, f32),
    color: (f32, f32, f32, f32),
    depth: f32,
}

/// Container class which implements drawable.
impl Rect {
    ///
    pub fn new(position: (f32, f32),
               size: (f32, f32),
               depth: u32,
               color: (f32, f32, f32, f32)) -> Rect {
        let normalized_depth = (depth as f32 / 255.0);

        Rect {
            position: position,
            size: size,
            color: color,
            depth: normalized_depth,
        }
    }

    fn generate_vertices(window_size: (u32, u32),
                         position: (f32, f32),
                         size: (f32, f32),
                         depth: f32,
                         color: (f32, f32, f32, f32)) -> Vec<ColorVertex3D> {

        let ss_position = (
            position.0 / window_size.0 as f32 - 1.0,
            position.1 / window_size.1 as f32 - 1.0
        );

        let ss_size = (
            size.0 / window_size.0 as f32,
            size.1 / window_size.1 as f32
        );

        vec![
            ColorVertex3D {
                v_position: [ss_position.0, ss_position.1, depth],                   // top left
                color: [color.0, color.1, color.2, color.3],
            },
            ColorVertex3D {
                v_position: [ss_position.0, ss_position.1 + ss_size.1, depth],          // bottom left
                color: [color.0, color.1, color.2, color.3],
            },
            ColorVertex3D {
                v_position: [ss_position.0 + ss_size.0, ss_position.1 + ss_size.1, depth], // bottom right
                color: [color.0, color.1, color.2, color.3],
            },
            ColorVertex3D {
                v_position: [ss_position.0, ss_position.1, depth],                   // top left
                color: [color.0, color.1, color.2, color.3],
            },
            ColorVertex3D {
                v_position: [ss_position.0 + ss_size.0, ss_position.1 + ss_size.1, depth], // bottom right
                color: [color.0, color.1, color.2, color.3],
            },
            ColorVertex3D {
                v_position: [ss_position.0 + ss_size.0, ss_position.1, depth],          // top right
                color: [color.0, color.1, color.2, color.3],
            },
        ]
    }
}

impl Drawable for Rect {
    fn get(&self, window_size: (u32, u32)) -> Vec<VertexTypeContainer> {
        vec![
            VertexTypeContainer::ColorType(
                Rect::generate_vertices(window_size, self.position, self.size, self.depth, self.color)
            )
        ]
    }
}
