# frozen_string_literal: true

module YouCanBookMe
  # YouCanBookMe APIs client.
  class Client
    API_HOST = 'https://api.youcanbook.me'
    API_VERSION = 'v1'

    # @param [String] account_id a YouCanBook.me's account id(or email).
    # @param [String] password_or_token a YouCanBook.me's account password or token.
    # @raise [YouCanBookMe::Error] if the account_id is empty.
    # @raise [YouCanBookMe::Error] if the password_or_token is empty.
    def initialize(account_id = nil, password_or_token = nil)
      @config = YouCanBookMe.configuration
      @account_id = account_id || @config.account_id
      @password_or_token = password_or_token || @config.password_or_token

      check_not_empty @account_id, 'account_id'
      check_not_empty @password_or_token, 'password_or_token'
      @connection = HttpCommand.new @account_id, @password_or_token
    end

    #
    # Account
    #

    def account_path
      "/#{API_VERSION}/#{@account_id}"
    end

    #
    # Get basic information about current account.
    #
    # @param [Array<String>] fields the fields which are included in the response.
    # @return [Account]
    def account(fields = nil)
      params = set_fields_into_params(fields)
      res = @connection.get account_path, params
      Account.new res.body, self
    end

    #
    # Get suggested subdomains
    #
    # @return [Array<String>]
    def suggested_subdomains
      res = @connection.get "/#{API_VERSION}/suggestedsubdomains"
      res.body
    end

    #
    # Get the subdomain is available or not.
    #
    # @param [String] subdomain
    # @return [Boolean]
    # @raise [YouCanBookMe::Error] if the subdomain arg is empty.
    def subdomain_available?(subdomain)
      check_not_empty subdomain, 'subdomain'
      res = @connection.get "/#{API_VERSION}/subdomains/#{subdomain}"
      res.body[:free]
    end

    private

    def check_not_empty(value, name)
      raise YouCanBookMe::Error, "#{name} is required." if blank? value
    end

    def blank?(value)
      return true if value.nil?
      return true if value.to_s.empty?

      false
    end

    def set_fields_into_params(fields, params = nil)
      params ||= {}
      return params unless fields
      return params unless fields.is_a? Array
      return params if fields.empty?

      params.merge(fields: fields.join(','))
    end
  end
end