use image;
use rand;

use crate::camera::Camera;
use crate::hitable::Hit;
use crate::hitable_list::HitableList;
use crate::kdtree::KDTree;
use crate::material::DiffuseLight;
use crate::material::Lambertian;
use crate::noise::perlin::Perlin;
use crate::noise::NoiseType;
use crate::rect::XYRect;
use crate::rect::XZRect;
use crate::rect::YZRect;
use crate::renderer::Opt;
use crate::renderer::Scene;
use crate::sphere::Sphere;
use crate::texture::ConstantTexture;
use crate::texture::ImageTexture;
use crate::texture::NoiseTexture;
use crate::vec3::Vec3;

pub fn new(opt: &Opt) -> Scene {
    let lookfrom = Vec3::new(20., 20., 20.);
    let lookat = Vec3::new(0., 1., 0.);
    let dist_to_focus = 10.0;
    let aperture = 0.0;
    let time_min = 0.;
    let time_max = 1.;
    let camera = Camera::new(
        lookfrom,
        lookat,
        Vec3::new(0., 1., 0.),
        20.,
        opt.width as f32 / opt.height as f32,
        aperture,
        dist_to_focus,
        time_min,
        time_max,
    );
    let rng = &mut rand::thread_rng();
    let _ground_color = if opt.use_accel {
        Box::new(ConstantTexture::new(Vec3::new(1.0, 0.4, 0.4)))
    } else {
        Box::new(ConstantTexture::new(Vec3::new(0.4, 1.0, 0.4)))
    };

    let world_image_bytes = include_bytes!("../../images/world.jpg");
    let it = ImageTexture::new(image::load_from_memory(world_image_bytes).unwrap().to_rgb());
    let noise_source = Perlin::new(rng);
    let noise_type = NoiseType::Scale(10.);
    let objects: Vec<Box<dyn Hit>> = vec![
        // Big sphere
        Box::new(Sphere::new(Vec3::new(0., 2., 0.), 2.0, Lambertian::new(it))),
        // Earth sized sphere
        Box::new(Sphere::new(
            Vec3::new(0., -1000., 0.),
            1000.,
            // Box::new(Lambertian::new(ground_color)),
            Lambertian::new(NoiseTexture::new(noise_source, noise_type)),
        )),
        Box::new(XZRect::new(
            -100.,
            100.,
            -100.,
            1000.,
            60.,
            DiffuseLight::new(ConstantTexture::new(Vec3::new(1., 1., 1.))),
        )),
        Box::new(YZRect::new(
            1.,
            3.,
            -1.,
            1.,
            4.,
            DiffuseLight::new(ConstantTexture::new(Vec3::new(4., 0., 4.))),
        )),
        Box::new(YZRect::new(
            1.,
            3.,
            -1.,
            1.,
            -4.,
            DiffuseLight::new(ConstantTexture::new(Vec3::new(0., 4., 0.))),
        )),
        Box::new(XZRect::new(
            -1.,
            1.,
            -1.,
            1.,
            6.,
            DiffuseLight::new(ConstantTexture::new(Vec3::new(4., 4., 0.))),
        )),
        Box::new(XYRect::new(
            -1.,
            1.,
            1.,
            3.,
            -4.,
            DiffuseLight::new(ConstantTexture::new(Vec3::new(0., 0., 4.))),
        )),
        Box::new(XYRect::new(
            -1.,
            1.,
            1.,
            3.,
            4.,
            DiffuseLight::new(ConstantTexture::new(Vec3::new(0., 4., 4.))),
        )),
        /*
        Box::new(Sphere::new(
            Vec3::new(0., 0., 0.),
            0.5,
            Box::new(Lambertian::new(ConstantTexture::new(Vec3::new(
                0.1, 0.2, 0.5,
            )))),
        )),
        // Shiny sphere
        Box::new(Sphere::new(
            Vec3::new(1., 0., 0.),
            0.5,
            Metal::new(Vec3::new(0.8, 0.8, 0.8), 0.2),
        )),
        Box::new(MovingSphere::new(
            Vec3::new(-1., 0., -0.25),
            Vec3::new(-1., 0., 0.25),
            0.5,
            0.,
            1.,
            Lambertian::new(ConstantTexture::new(Vec3::new(
                0.2, 0.8, 0.2,
            ))),
        )),
        */
    ];
    let world: Box<dyn Hit> = if opt.use_accel {
        Box::new(KDTree::new(objects, time_min, time_max))
    } else {
        Box::new(HitableList::new(objects))
    };
    Scene {
        camera,
        world,
        subsamples: opt.subsamples,
        num_threads: opt.num_threads,
        width: opt.width,
        height: opt.height,
        ..Default::default()
    }
}
