extern crate image;
extern crate rusty_machine;

use rusty_machine::linalg::{Matrix, BaseMatrix};
use rusty_machine::learning::k_means::{KMeansClassifier};
use rusty_machine::learning::UnSupModel;

//extern crate palette;

use image::{GenericImage, ImageBuffer};

fn get_color(px:image::Rgba<u8>) {
    println!("{:?}",px);

}

fn modify(val:u8, percent:u32)-> u8{
    let ret=((val as u32*percent)+50)/100;
    ret.min(255) as u8
}

fn convert(centroids:&Matrix<f64>){
    for i in 0..centroids.rows() {

        println!("{:?}",centroids.get_row(i).unwrap())
    }

}

fn main() {
    let img = image::open("smalltest.jpg").unwrap();
    let dimensions = img.dimensions();
    let mut buff:image::RgbaImage = ImageBuffer::new(dimensions.0,dimensions.1);


    let mut pixelData = Vec::new();
    let pixeliter = img.pixels() ;
    for pixel in pixeliter {
        //get_color(pixel.2);
        let mut newpix = pixel.clone();
        pixelData.push(newpix.2[0] as f64);
        pixelData.push(newpix.2[1] as f64);
        pixelData.push(newpix.2[2] as f64);
    }
    println!("{:?}",(dimensions.0*dimensions.1) as usize);
    let a = Matrix::new((dimensions.0*dimensions.1) as usize,3,pixelData);
    let mut model = KMeansClassifier::new(5);


    model.train(&a).unwrap();

    println!("{:?}",model);


    let centroids = model.centroids().as_ref().unwrap();
    println!("Model Centroids:\n{:.0}", centroids);
    convert(centroids);
//    let slice = MatrixSlice::from_matrix(&centroids, [0,0], 1, 3);
    let slice = centroids.get_row(0).unwrap();

    println!("{:?}", slice);



//    let a = model.predict(&test_inputs).unwrap();


//    println!("{:?}", e);
//    println!("{:?}", m.data());
//
//    let pixeliter = img.pixels() ;
//    for pixel in pixeliter{
//        //get_color(pixel.2);
//        let mut newpix = pixel.clone();
//        let mut red = newpix.2[0];
//        let mut green = newpix.2[1];
//        let mut blue = newpix.2[2];
//
//        newpix.2[0] = modify(green, 200);
//        newpix.2[1]=blue;
//        newpix.2[2]=red ;
//
//        buff.put_pixel(newpix.0,newpix.1,newpix.2);
//    }
//    println!("test");
//
//
//    buff.save("test.png").unwrap();
}