#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
#![allow(rustdoc::missing_crate_level_docs)] // it's an example
use eframe::egui;
use std::env;
use std::fs::File;
use std::fs::OpenOptions;
use std::io::Write;
use std::io::prelude::*;

mod byte_writer;
mod gui;
mod node;
mod read_and_write;

use gui::Omelt;
use read_and_write::{is_valid_file, save_action, string_translate};

fn main() -> std::io::Result<()> {
    let args: Vec<String> = env::args().collect();

    let request = args.get(1).map(|s| s.to_string());

    match request {
        Some(r) => {
            if r == "--compress" || r == "-c" {
                let file_name = args.get(2).map(|s| s.to_string());
                let file_to_un = args.get(3).map(|s| s.to_string());
                match file_name {
                    Some(f) => {
                        let mut to_compress = File::open(&f)?;
                        let mut after_compression: File;
                        if let Some(str) = file_to_un {
                            after_compression = File::create(str)?;
                        } else {
                            after_compression = File::create(f + ".oeuf")?;
                        }
                        let mut txt = "".to_owned();
                        to_compress.read_to_string(&mut txt)?;
                        eprintln!("txt = {:?}", txt);
                        save_action(&txt, &mut after_compression)?;
                        println!("Done.")
                    }
                    None => {
                        println!("No file name!");
                        display_options();
                    }
                }
            } else if r == "--print" || r == "-p" {
                let file_name = args.get(2).map(|s| s.to_string());
                match file_name {
                    Some(f) => {
                        let mut to_print = File::open(f)?;
                        if is_valid_file(&mut to_print) {
                            let untranslate = string_translate(&mut to_print);
                            println!("{}", untranslate);
                        } else {
                            println!("Error oppening file, check type file !")
                        }
                    }
                    None => {
                        println!("No file name!");
                        display_options();
                    }
                }
            } else if r == "--uncompress" || r == "-u" {
                let file_name = args.get(2).map(|s| s.to_string());
                let file_to_un = args.get(3).map(|s| s.to_string());
                match file_name {
                    Some(f) => {
                        let mut to_uncomp = File::open(&f)?;
                        let mut uncomp: File;
                        if is_valid_file(&mut to_uncomp) {
                            if let Some(str) = file_to_un {
                                uncomp = File::create(str)?;
                            } else {
                                uncomp = File::create(f + ".txt")?;
                            }
                            let txt = string_translate(&mut to_uncomp);
                            uncomp.write_all(txt.as_bytes())?;
                        } else {
                            println!("Error oppening file, check type file !")
                        }
                    }
                    None => {
                        println!("No File name !");
                        display_options();
                    }
                }
            } else {
                let options = eframe::NativeOptions {
                    viewport: egui::ViewportBuilder::default().with_inner_size([640.0, 480.0]),
                    ..Default::default()
                };
                let _ = eframe::run_native(
                    "Omelt",
                    options,
                    Box::new(|_| {
                        Ok(Box::new(Omelt::new(
                            &r,
                            OpenOptions::new().read(true).write(true).open(&r)?,
                        )))
                    }),
                );
            }
        }
        None => {
            let options = eframe::NativeOptions {
                viewport: egui::ViewportBuilder::default().with_inner_size([640.0, 480.0]),
                ..Default::default()
            };

            let _ = eframe::run_native(
                "Omelt",
                options,
                Box::new(|_| {
                    Ok(Box::new(Omelt::new(
                        "new_file.oeuf",
                        File::create_new("new_file.oeuf")?,
                    )))
                }),
            );
        }
    }

    Ok(())
}

fn display_options() {
    println!("<input.oeuf>                             To open file with gui.");
    println!("--compress  , -c <intput> [<output>]     To compress a file.");
    println!("--uncompress, -u <intput.oeuf> [<output>]     To uncompress a file.");
    println!("--print     ,-p  <input>                 To print a file compressed.");
}
