use sfml::graphics::{Texture, Sprite, IntRect, Drawable, RenderTarget, RenderStates};
use sfml::system::Vector2u;
use sfml::graphics::Transformable;

//pub struct Thing<'a> {
//
//    loader: WorkpieceLoader,
//    workpiece: Workpiece<'a>
//
//}
//
//impl<'a> Thing<'a> {
//
//    pub fn new(pixels: Vec<u8>) -> Thing<'a> {
//
//        let mut workpieceloader = WorkpieceLoader::new(String::from("resources/images/funky-bird.jpg"));
//        workpieceloader.load_first_stage(pixels);
//
//        let mut workpiece = Workpiece::new();
//        workpiece.load_first_stage(&mut workpieceloader.first_stage_texture);
//
//        Thing {
//            loader: workpieceloader,
//            workpiece: workpiece
//        }
//    }
//}

/*


    first_thing  = Thing1::new();
    second_thing = Thing1::new();

    let mut container_thing = Thing2::new();

    container_thing.field = &mut first_thing;
    container_thing.field = &mut second_thing;

    first_thing.field = 10;





*/

#[derive(Clone)]
pub struct WorkpieceLoader {
    pub swatch_texture: Texture,
    pub first_stage_texture: Texture,
    pub xy: Vector2u,
    pub vec: Vec<Texture>,
}

impl WorkpieceLoader {

    pub fn new(filepath: String) -> WorkpieceLoader {

        let texture = Texture::from_file(filepath.as_str()).expect("Couldn't load image");
        let xy = texture.size();

        WorkpieceLoader {
            swatch_texture: texture,
            first_stage_texture: Texture::new(xy.x, xy.y).unwrap(),
            xy: xy,
            vec: vec![]
        }
    }

    pub fn load_first_stage(&mut self, pixels: Vec<u8>) {
        self.vec.clear();
        let mut t = Texture::new(self.xy.x, self.xy.y).expect("Couldn't load image");
        t.update_from_pixels(pixels.as_slice(), self.xy.x, self.xy.y, 0, 0);
        self.vec.push(t);

    }

    pub fn get_first(&mut self) -> &Texture {
        &self.first_stage_texture
    }
}

pub struct Workpiece<'a> {
    pub render_sprite: Sprite<'a>
}

impl<'a> Workpiece<'a> {

    pub fn new() -> Workpiece<'a> {

        Workpiece {
            render_sprite: Sprite::new()
        }
    }

    pub fn load_first_stage(&mut self, texture: &'a mut Texture) {


        self.render_sprite.set_texture(texture, false);
        self.render_sprite.set_position((0., 0.));

    }
}

impl<'s> Drawable for Workpiece<'s> {
    fn draw<'a: 'shader, 'texture, 'shader, 'shader_texture>(
        &'a self,
        render_target: &mut RenderTarget,
        _: RenderStates<'texture, 'shader, 'shader_texture>,
    ) {

    }
}