# require "set"
require "toolhound-ruby/authentication"
require "toolhound-ruby/configurable"
require "toolhound-ruby/client/projects"
# require "toolhound-ruby/incident"


module Toolhound

  class Client
    include Toolhound::Authentication
    include Toolhound::Configurable

    include Toolhound::Client::Projects
    # include Toolhound::Client::Bookmarks
    # include Toolhound::Client::Categories
    # include Toolhound::Client::Incidents
    # include Toolhound::Client::Notifications
    # include Toolhound::Client::Projects
    # include Toolhound::Client::RateLimit
    # include Toolhound::Client::Users
    # include Toolhound::Client::Companies
    # include Toolhound::Client::Attachments

    # include Toolhound::Client::Users
    # include Toolhound::Client::ProjectLibrary
    # include Toolhound::Client::Projects
    # include Toolhound::Client::Templates
    # include Toolhound::Client::Checklists
    # include Toolhound::Client::Tasks
    # include Toolhound::Client::Issues
    # include Toolhound::Client::Utils

    # def projects
    #
    # end
    # attr_accessor :access_token, :client_id, :uid, :expiry, :me
    attr_accessor :connection, :dataserver, :username, :password, :port

    def initialize(options = {})

      # Use options passed in, but fall back to module defaults
      Toolhound::Configurable.keys.each do |key|
        instance_variable_set(:"@#{key}", options[key] || Toolhound.instance_variable_get(:"@#{key}"))
      end

      sign_in if authenticatable?
    end

    # Compares client options to a Hash of requested options
    #
    # @param opts [Hash] Options to compare with current client options
    # @return [Boolean]
    def same_options?(opts)
      opts.hash == options.hash
    end


    def inspect # :nodoc:

      inspected = super

      # mask password
      inspected = inspected.gsub! @password, "*******" if @password
      # Only show last 4 of token, secret
      # if @access_token
      #   inspected = inspected.gsub! @access_token, "#{'*'*36}#{@access_token[36..-1]}"
      # end
      # if @client_secret
      #   inspected = inspected.gsub! @client_secret, "#{'*'*36}#{@client_secret[36..-1]}"
      # end

      inspected
    end


    # Set username for authentication
    #
    # @param value [String] Toolhound-field username
    def username=(value)
      reset_connection
      @email = value
    end

    # Set password for authentication
    #
    # @param value [String] Toolhound-field password
    def password=(value)
      reset_connection
      @password = value
    end

    def reset_connection
      @connection = nil
    end


    # Wrapper around Kernel#warn to print warnings unless
    # TOOLHOUND_SILENT is set to true.
    #
    # @return [nil]
    def nearmiss_warn(*message)
      unless ENV['TOOLHOUND_SILENT']
        warn message
      end
    end



  end


end