use std::sync::Arc;

use crate::{
    camera::Camera,
    constant_medium::ConstantMedium,
    cuboid::Cuboid,
    flip_normals::FlipNormals,
    hitable::Hit,
    hitable_list::HitableList,
    kdtree::KDTree,
    material::{DiffuseLight, Lambertian, Material},
    rect::{XYRect, XZRect, YZRect},
    renderer::{Opt, Scene},
    rotate::RotateY,
    texture::ConstantTexture,
    translate::Translate,
    vec3::Vec3,
};

pub fn new(opt: &Opt) -> Scene {
    let lookfrom = Vec3::new(278., 278., -800.);
    let lookat = Vec3::new(278., 278., 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.),
        40.,
        opt.width as f32 / opt.height as f32,
        aperture,
        dist_to_focus,
        time_min,
        time_max,
    );

    let red = Lambertian::new(ConstantTexture::new([0.65, 0.05, 0.05]));
    let white: Arc<dyn Material> =
        Arc::new(Lambertian::new(ConstantTexture::new([0.73, 0.73, 0.73])));
    let green = Lambertian::new(ConstantTexture::new([0.12, 0.45, 0.15]));
    let light = DiffuseLight::new(ConstantTexture::new([7., 7., 7.]));

    let objects: Vec<Box<dyn Hit>> = vec![
        // White smoke box on the right
        Box::new(ConstantMedium::new(
            Translate::new(
                RotateY::new(
                    Cuboid::new(
                        Vec3::new(0., 0., 0.),
                        Vec3::new(165., 165., 165.),
                        Arc::clone(&white),
                    ),
                    -18.,
                ),
                Vec3::new(130., 0., 65.),
            ),
            0.01,
            ConstantTexture::new([1., 1., 1.]),
        )),
        // Black smoke box on the left
        Box::new(ConstantMedium::new(
            Translate::new(
                RotateY::new(
                    Cuboid::new(
                        Vec3::new(0., 0., 0.),
                        Vec3::new(165., 330., 165.),
                        Arc::clone(&white),
                    ),
                    15.,
                ),
                Vec3::new(265., 0., 295.),
            ),
            0.01,
            ConstantTexture::new([0., 0., 0.]),
        )),
        // Green wall left
        Box::new(FlipNormals::new(YZRect::new(
            0., 555., 0., 555., 555., green,
        ))),
        // Red floor right
        Box::new(YZRect::new(0., 555., 0., 555., 0., red)),
        // Light in ceiling
        Box::new(XZRect::new(113., 443., 127., 432., 554., light)),
        // Grey ceiling
        Box::new(FlipNormals::new(XZRect::new(
            0.,
            555.,
            0.,
            555.,
            555.,
            Arc::clone(&white),
        ))),
        // Grey floor
        Box::new(XZRect::new(0., 555., 0., 555., 0., Arc::clone(&white))),
        // Grey back wall
        Box::new(FlipNormals::new(XYRect::new(
            0.,
            555.,
            0.,
            555.,
            555.,
            Arc::clone(&white),
        ))),
    ];
    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()
    }
}
