#!/usr/bin/env ruby

require "Gemnigma"

class StartApplication
  def init_application
    @option = ARGV[0]

    if ARGV.length == 0
      help
    end

    if ARGV.length == 1
      case @option
        when "-v"
          version
        when "--version"
          version
        when "-h"
          help
        when "--help"
          help
        when "-ls"
          list
        when "list"
          list
        else
          help
      end
    end

    if ARGV.length > 1
      case @option  
        when "-e"
          encrypt
        when "encrypt"
          encrypt
        when "-d"
          decrypt
        when "decrypt"
          decrypt
        when "-c"
          crack
        when "crack"
          crack
        when "-ls"
          list
        when "list"
          list
        else
          help
      end
    end

  end

  private 

  def version
    version = Gemnigma::VERSION
    p version
  end

  def help
    puts "Usage:\nGemnigma [option] [*arguments]"
    puts "\nOptions:\n\t-h, [--help]" + "#Displays this help message and quits".rjust(50)
    puts "\t-v, [--version]" + "#Display Gemnigma version number and quit".rjust(51)
    puts "\t-ls,[list]" + "#List files in the current working directory".rjust(57)
    puts "\t-e, [encrypt]" + "#Encrypt a file in the current directory".rjust(50)
    puts "\t-d, [decrypt]" + "#Decrypt an encrypted file in the current directory".rjust(61)
    puts "\t-c, [crack]" + "#Cracks an encrypted file in the current directory".rjust(62)

    puts "\nEncrypting files:"
    puts "To Encrypt files in the current directory, call Gemnigma -e or Gemnigma --encrypt and\npass in "\
         "the file to be encrypted and a new file to store the encrypted message.\n\n"\
         "Example:\n Gemnigma -e message.txt encrypted.txt"

    puts "\nDecrypting files:"
    puts "To decrypt files in the current directory, call Gemnigma -d or Gemnigma --decrypt and\n"\
          "pass in the file to store the decrypted contents of an encrypted file, you are also\n"\
          "required to pass in the date that the file was generated and the key used to generate\n"\
          "the encrypted file.\n\n"\
          "Example:\n Gemnigma -d encrypted.txt decrypted.txt 41521 #{Time.now.strftime("%d%m%y").to_i}"

    puts "\nCracking files:"
    puts "To crack files, call Gemnigma -c or Gemnigma --crack. You must specify the date encrypted"\
         "file was generated and file to be written to\n\n"\
         "Example:\nGemnigma -c encrypted.txt cracked.txt #{Time.now.strftime("%d%m%y").to_i}"
  end

  def encrypt
    enc = Gemnigma::Encrypt.new
    enc.get_cmd_args
  end

  def decrypt
    dec = Gemnigma::Decrypt.new
    dec.get_cmd_args
  end

  def crack
    crack = Gemnigma::Crack.new
    crack.get_cmd_args
  end

  def list_all
    result = %x["ls"]
    result_arr = result.split("\n")

    result_arr.map! do |item|
      if File.file? item
        "[File] " + item
      else
        "[Folder] " + item
      end
    end
    puts "All items in current working directory: \n\n"
    puts result_arr
    puts "\nTotal amount items in this directory: #{result_arr.length}"
  end

  def list_file
    result = %x["ls"]
    result_arr = result.split("\n")

    result_arr.map! do |item|
      if File.file? item
        "[File] " + item
      else
        ""
      end
    end
    puts "All files in current working directory: \n\n"
    result_arr.delete("")
    puts result_arr
    puts "\nTotal amount of files in this directory: #{result_arr.length}"
  end

  def list_folder
    result = %x["ls"]
    result_arr = result.split("\n")
    result_arr.map! do |item|
      if File.directory? item
        "[Folder] " + item
      else
        ""
      end
    end
    puts "All folders in current working directory: \n\n"
    result_arr.delete("")
    puts result_arr
    puts "\nTotal amount of folders in this directory: #{result_arr.length}"
  end

  def list_help
    puts "Usage:\n Gemnigma list [option]"
    puts "\nOptions:\n\thelp," + "#Displays this help message and quits".rjust(49)
    puts "\tall," + "#Display all files and folder in the current working directory".rjust(75)
    puts "\tfile," + "#Display only files in the current working directory".rjust(64)
    puts "\tfolder," + "#Display all folders in the current working directory".rjust(63)
  end

  def list
    list_all if ARGV.length == 1 || (ARGV.length == 2 && ARGV[1] == "all")

    list_file if ARGV.length == 2 && ARGV[1] == "file"

    list_folder if ARGV.length == 2 && ARGV[1] == "folder"

    list_help if ARGV.length == 2 && ARGV[1] == "help"

    if ARGV.length == 2 && !(ARGV[1] == "all" || ARGV[1] == "file" || ARGV[1] == "folder" || ARGV[1] == "help")
      puts "Not a valid option"
    end

    if ARGV.length > 2
      puts "Too many options passed in. Not valid"
    end

  end
  
end

StartApplication.new.init_application