require_relative '../../lib/aapb'
require_relative '../../lib/markdowner'
require_relative 'state'
require 'yaml'
require 'cgi'

class Organization < Cmless
  ROOT = (Rails.root + 'app/views/organizations/md').to_s

  attr_reader :short_name_html
  attr_reader :state_html
  attr_reader :city_html
  attr_reader :url_html
  attr_reader :about_html
  attr_reader :productions_html
  attr_reader :logo_html

  def self.clean(html)
    CGI.unescape(html.gsub(/<[^>]+>/, ''))
  end

  def self.clean_organization_names(organization_names)
    organization_names.map { |name| name.include?("\n") ? name.delete!("\n").split.join(" ") : name }
  end

  def id
    @id ||= path
  end

  def pbcore_name
    @pbcore_name ||= title
  end

  def summary
    @summary ||= about_html.sub(/(^.{10,}?\.\s+)([A-Z].*)?/m, '\1')
  end

  def logo_src
    clean = Organization.clean(logo_html)
    clean.empty? ? '' : "#{AAPB::S3_BASE}/org-logos/#{Organization.clean(logo_html)}"
  end

  def facet
    @facet ||= "#{short_name} (#{state_abbreviation})"
  end

  def state_abbreviation
    @state_abbreviation = State.find_by_name(state).abbreviation
  end

  def state
    @state ||= Organization.clean(state_html)
  end

  def city
    @city ||= Organization.clean(city_html)
  end

  def facet_url
    digitized_records? ? "/catalog?f[contributing_organizations][]=" + CGI.escape(facet) : "/catalog?f[contributing_organizations][]=" + CGI.escape(facet) + "&f[access_types[]=all"
  end

  def short_name
    # this is really hacky, but the redcarpet gem for in cmless
    # for interpreting md was not recognizing the escaped ampersand
    # and a literal ampersand would display as a '&amp;'
    # fix should probably be in cmless with an update of redcarpet
    # but we're punting that for now.
    @short_name ||= Organization.clean(short_name_html).gsub("&amp;", "&")
  end

  def urls
    @urls ||= Nokogiri::HTML(url_html).xpath('//a').map { |a| a['href'] }
  end

  @orgs_by_pbcore_name = Hash[Organization.map { |org| [org.pbcore_name, org] }]
  @orgs_by_id          = Hash[Organization.map { |org| [org.id, org] }]
  @orgs_by_state       = Hash[Organization.group_by(&:state)]

  def self.find_by_pbcore_name(pbcore_name)
    @orgs_by_pbcore_name[pbcore_name]
  end

  def self.find_by_id(id)
    @orgs_by_id[id]
  end

  def self.find_by_state(state)
    @orgs_by_state[state]
  end

  def self.organizations(organization_names)
    organization_names.map { |org| Organization.find_by_pbcore_name(org) }.compact
  end

  def self.build_organization_names_display(organizations)
    organizations.map(&:short_name)
  end

  def to_a
    [short_name, city, state]
  end

  private

  def digitized_records?
    Solr.instance.connect.get('select', params: { q: "contributing_organizations:\"#{facet}\"", fq: "access_types:\"digitized\"" })['response']['numFound'].to_i > 0 ? true : false
  end
end
