# frozen_string_literal: true

module Cryptum
  # This module is used to Accept User Input at Session Initiation
  module Option
    # Common module to validate input submitted at session initiation
    module InputValidation
      # Validate Options for cryptum Driver
      public_class_method def self.check(opts = {})
        option_choice = opts[:option_choice]

        # Conditions to display cryptum usage
        if option_choice.symbol.nil? && option_choice.list_products.nil?
          usage = true
          reason = :symbol
        end

        option_choice.session_root = "#{Dir.home}/cryptum" if option_choice.session_root.nil?

        unless Dir.exist?(option_choice.session_root)
          usage = true
          reason = :session_root
        end

        option_choice.market_trend_reset = 86_400 if option_choice.market_trend_reset.to_i.zero?
        unless option_choice.market_trend_reset.to_i >= 60 &&
               option_choice.market_trend_reset <= 604_800
          usage = true
          reason = :market_trend_reset
        end

        case option_choice.market_trend_reset
        when 604_800
          option_choice.market_trend_reset_label = '1W'
        when 86_400
          option_choice.market_trend_reset_label = '1D'
        when 14_400
          option_choice.market_trend_reset_label = '4h'
        when 10_800
          option_choice.market_trend_reset_label = '3h'
        when 7_200
          option_choice.market_trend_reset_label = '2h'
        when 3_600
          option_choice.market_trend_reset_label = '1h'
        when 2_700
          option_choice.market_trend_reset_label = '45m'
        when 1_800
          option_choice.market_trend_reset_label = '30m'
        when 900
          option_choice.market_trend_reset_label = '15m'
        when 300
          option_choice.market_trend_reset_label = '5m'
        when 180
          option_choice.market_trend_reset_label = '3m'
        when 60
          option_choice.market_trend_reset_label = '1m'
        else
          usage = true
          reason = :market_trend_reset
        end

        if usage
          case reason
          when :symbol
            puts "ERROR: --symbol Flag is Required.\n\n"
          when :session_root
            puts "ERROR: #{option_choice.session_root} does not exist.\n\n"
          when :market_trend_reset
            puts "ERROR: #{option_choice.market_trend_reset} - Possible values are: 604_800 || 86_400 || 14_400 || 10_800 || 7_200 || 3_600 || 2_700 || 1_800 || 900 || 300 || 180 || 60\n\n"
          end

          puts `#{option_choice.driver_name} --help`
          exit 1
        end
      rescue StandardError => e
        raise e
      end

      # Display Usage for this Module
      public_class_method def self.help
        constants.sort
      end
    end
  end
end