module Addic7ed
  class Subtitle

    attr_reader :version, :language, :status, :via, :downloads, :comment
    attr_accessor :url

    def initialize(options = {})
      @version   = Addic7edVersionNormalizer.call(options[:version])
      @language  = options[:language]
      @status    = options[:status]
      @url       = options[:url]
      @via       = options[:via]
      @hi        = options[:hi]
      @downloads = options[:downloads].to_i || 0
      @comment   = Addic7edCommentNormalizer.call(options[:comment])
    end

    def to_s
      "#{url}\t->\t#{version} (#{language}, #{status}) [#{downloads} downloads]#{" (via #{via})" if via}"
    end

    def works_for?(version = '', no_hi = false)
      hi_works = !@hi || !no_hi
      is_completed? and is_compatible_with? version and hi_works
    end

    def can_replace?(other_subtitle)
      return false unless is_completed?
      return true if other_subtitle.nil?
      language == other_subtitle.language &&
      is_compatible_with?(other_subtitle.version) &&
      is_more_popular_than?(other_subtitle)
    end

    def is_featured?
      via == "http://addic7ed.com"
    end

    def is_completed?
      status == 'Completed'
    end

  protected

    def is_compatible_with?(other_version)
      defined_as_compatible_with(other_version) || generally_compatible_with?(other_version) || commented_as_compatible_with?(other_version)
    end

    def defined_as_compatible_with(other_version)
      version.split(",").include? other_version
    end

    def generally_compatible_with?(other_version)
      COMPATIBILITY_720P[version] == other_version || COMPATIBILITY_720P[other_version] == version
    end

    def commented_as_compatible_with?(other_version)
      return false if /(won't|doesn't|not) +work/i.match comment
      return false if /resync +(from|of)/i.match comment
      res   = comment.include? other_version.downcase
      res ||= comment.include? COMPATIBILITY_720P[other_version].downcase if COMPATIBILITY_720P[other_version]
      res ||= comment.include? COMPATIBILITY_720P[version].downcase       if COMPATIBILITY_720P[version]
      !!res
    end

    def is_more_popular_than?(other_subtitle)
      return true  if other_subtitle.nil?
      return false if other_subtitle.is_featured?
      return downloads > other_subtitle.downloads
    end
  end
end
