require 'dry/core/inflector'
require 'roxml/hash_definition'
require 'roxml/utils'
require 'time'

module ROXML
  class ContradictoryNamespaces < StandardError
  end

  class Definition # :nodoc:
    attr_reader :name, :sought_type, :wrapper, :blocks, :accessor, :to_xml, :attr_name, :namespace, :inflector
    [:name_explicit, :array, :cdata, :required, :frozen].each do |attr|
      define_method :"#{attr}?" do
        instance_variable_get(:"@#{attr}") || false
      end
    end

    def initialize(sym, opts = {}, &block)
      @default = opts.delete(:else)
      @to_xml = opts.delete(:to_xml)
      @name_explicit = opts.has_key?(:from) && opts[:from].is_a?(String)
      @cdata = opts.delete(:cdata)
      @required = opts.delete(:required)
      @frozen = opts.delete(:frozen)
      @wrapper = opts.delete(:in)
      @namespace = opts.delete(:namespace)
      @inflector = opts.fetch(:inflector) { Dry::Core::Inflector.inflector }

      @accessor = sym.to_s
      opts[:as] ||=
        if @accessor.end_with?('?')
          :bool
        elsif @accessor.end_with?('_on')
          Date
        elsif @accessor.end_with?('_at')
          DateTime
        end

      @array = opts[:as].is_a?(Array)
      @blocks = collect_blocks(block, opts[:as])

      @sought_type = extract_type(opts[:as])
      if @sought_type.respond_to?(:roxml_tag_name)
        opts[:from] ||= @sought_type.roxml_tag_name
      end

      if opts[:from] == :content
        opts[:from] = '.'
      elsif opts[:from] == :name
        opts[:from] = '*'
      elsif opts[:from] == :attr
        @sought_type = :attr
        opts[:from] = nil
      elsif opts[:from] == :namespace
        opts[:from] = '*'
        @sought_type = :namespace
      elsif opts[:from].to_s.start_with?('@')
        @sought_type = :attr
        opts[:from] = opts[:from].sub('@', '')
      end

      @name = @attr_name = accessor.to_s.chomp('?')
      @name = inflector.singularize(@name) if hash_definition? || array?
      @name = (opts[:from] || @name).to_s
      if hash_definition? && (hash_definition.key.name? || hash_definition.value.name?)
        @name = '*'
      end
      raise ContradictoryNamespaces if @name.include?(':') && (@namespace || @namespace == false)

      raise ArgumentError, "Can't specify both :else default and :required" if required? && @default
    end

    def instance_variable_name
      :"@#{attr_name}"
    end

    def setter
      :"#{attr_name}="
    end

    def hash_definition
      if hash_definition?
        @sought_type.wrapper ||= name
        @sought_type
      end
    end

    def hash_definition?
      @sought_type.is_a?(HashDefinition)
    end

    def name?
      @name == '*'
    end

    def content?
      @name == '.'
    end

    def default
      if @default.nil?
        @default = [] if array?
        @default = {} if hash_definition?
      end

      begin
        @default.dup
      rescue TypeError
        @default
      end
    end

    def to_ref(inst)
      case sought_type
      when :attr          then XMLAttributeRef
      when :text          then XMLTextRef
      when :namespace     then XMLNameSpaceRef
      when HashDefinition then XMLHashRef
      when Symbol         then raise ArgumentError, "Invalid type argument #{sought_type}"
      else                     XMLObjectRef
      end.new(self, inst)
    end

  private
    def self.all(items, &block)
      array = items.is_a?(Array)
      results = (array ? items : [items]).map do |item|
        yield item
      end

      array ? results : results.first
    end

    def self.fetch_bool(value, default)
      value = value.to_s.downcase
      if %w{true yes 1 t}.include? value
        true
      elsif %w{false no 0 f}.include? value
        false
      else
        default
      end
    end

    CORE_BLOCK_SHORTHANDS = {
      # Core Shorthands
      Integer => lambda do |val|
        all(val) do |v|
          v.to_i unless Utils.string_blank?(v.to_s)
        end
      end,
      Float => lambda do |val|
        all(val) do |v|
          Float(v) unless Utils.string_blank?(v.to_s)
        end
      end,
      Time => lambda do |val|
        all(val) {|v| Time.parse(v) unless Utils.string_blank?(v.to_s)}
      end,

      :bool => nil,
      :bool_standalone => lambda do |val|
        all(val) do |v|
          fetch_bool(v, nil)
        end
      end,
      :bool_combined => lambda do |val|
        all(val) do |v|
          fetch_bool(v, v)
        end
      end
    }

    def self.block_shorthands
      # dynamically load these shorthands at class definition time, but
      # only if they're already availbable
      CORE_BLOCK_SHORTHANDS.tap do |blocks|
        blocks[BigDecimal] = lambda do |val|
          all(val) do |v|
            BigDecimal(v) unless Utils.string_blank?(v.to_s)
          end
        end if defined?(BigDecimal)

        blocks[DateTime] = lambda do |val|
          if defined?(DateTime)
            all(val) {|v| DateTime.parse(v) unless Utils.string_blank?(v.to_s)}
          end
        end if defined?(DateTime)

        blocks[Date] = lambda do |val|
          if defined?(Date)
            all(val) {|v| Date.parse(v) unless Utils.string_blank?(v.to_s)}
          end
        end if defined?(Date)
      end
    end

    def collect_blocks(block, as)
      if as.is_a?(Array)
        if as.size > 1
          raise ArgumentError, "multiple :as types (#{as.map(&:inspect).join(', ')}) is not supported.  Use a block if you want more complicated behavior."
        end

        as = as.first
      end

      if as == :bool
        # if a second block is present, and we can't coerce the xml value
        # to bool, we need to be able to pass it to the user-provided block
        as = (block ? :bool_combined : :bool_standalone)
      end
      as = self.class.block_shorthands.fetch(as) do
        unless (as == :text) || as.respond_to?(:from_xml) || (as.respond_to?(:first) && as.first.respond_to?(:from_xml)) || (as.is_a?(Hash) && !(as.keys & [:key, :value]).empty?)
          raise ArgumentError, "Invalid :as argument #{as}" unless as.nil?
        end
        nil
      end
      [as, block].compact
    end

    def extract_type(as)
      if as.is_a?(Hash)
        HashDefinition.new(as)
      elsif as.respond_to?(:from_xml)
        as
      elsif as.is_a?(Array) && as.first.respond_to?(:from_xml)
        @array = true
        as.first
      else
        :text
      end
    end
  end
end
