use inflorescence_view::testing::{
    test_kind, TestKind, PREVIEW_DIR, SCREENSHOTS_DIR,
};

use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};

use std::fs;
use std::io::Write;
use std::path::{Path, PathBuf};

const CRATE_DIR: &str = "inflorescence_view";

fn main() {
    match test_kind() {
        TestKind::Compare => {
            // Copy stored screenshots into the preview dir to use for
            // comparison
            let dst_dir = PathBuf::from(CRATE_DIR).join(PREVIEW_DIR);
            let src_dir = PathBuf::from(CRATE_DIR).join(SCREENSHOTS_DIR);
            let _res = fs::remove_dir_all(&dst_dir);
            fs::create_dir_all(&dst_dir).unwrap();

            for entry in fs::read_dir(&src_dir).unwrap() {
                let entry = entry.unwrap();
                let ty = entry.file_type().unwrap();
                if ty.is_dir() {
                    panic!("Unexpectedly found dir {} inside screenshots dir {SCREENSHOTS_DIR}", entry.path().to_string_lossy());
                } else {
                    fs::copy(entry.path(), dst_dir.join(entry.file_name()))
                        .unwrap();
                }
            }
            print_screenshot_target(&dst_dir);
        }
        TestKind::Preview => {
            // Clear the preview dir to store new ones
            let dir = PathBuf::from(CRATE_DIR).join(PREVIEW_DIR);
            if let Ok(entries) = fs::read_dir(&dir) {
                for entry in entries {
                    let entry = entry.unwrap();
                    let ty = entry.file_type().unwrap();
                    if ty.is_dir() {
                        panic!("Unexpectedly found dir {} inside screenshots dir {PREVIEW_DIR}", entry.path().to_string_lossy());
                    } else {
                        fs::remove_file(entry.path()).unwrap();
                    }
                }
            } else {
                fs::create_dir_all(&dir).unwrap();
            }
            print_screenshot_target(&dir);
        }
        TestKind::Accept => {
            // Clear the screenshots dir to store new ones
            let dir = PathBuf::from(CRATE_DIR).join(SCREENSHOTS_DIR);
            if let Ok(entries) = fs::read_dir(&dir) {
                for entry in entries {
                    let entry = entry.unwrap();
                    let ty = entry.file_type().unwrap();
                    if ty.is_dir() {
                        panic!("Unexpectedly found dir {} inside screenshots dir {PREVIEW_DIR}", entry.path().to_string_lossy());
                    } else {
                        fs::remove_file(entry.path()).unwrap();
                    }
                }
            } else {
                fs::create_dir_all(&dir).unwrap();
            }
            print_screenshot_target(&dir);
        }
    }
}

fn print_screenshot_target(path: &Path) {
    let mut stdout = StandardStream::stdout(ColorChoice::Always);
    writeln!(&mut stdout).unwrap();
    stdout
        .set_color(ColorSpec::new().set_bg(Some(Color::Magenta)))
        .unwrap();
    write!(&mut stdout, "Screenshots will be saved in:").unwrap();
    stdout.reset().unwrap();
    writeln!(&mut stdout, " \"{}\"", path.to_string_lossy()).unwrap();
    writeln!(&mut stdout).unwrap();
}