class Title < ActiveRecord::Base

  TITLE_VISIBLE   = ['admin.title.do_not_display','admin.title.test','admin.title.customer', 'admin.title.partner']
  TITLE_TYPE      = ['admin.title.magazine','admin.title.book_publisher','admin.title.corporate','admin.title.PQN','admin.title.university','admin.title.communication_agency','admin.title.press_com_agency','admin.title.website','admin.title.independant_icono']

  has_many :users, dependent: :nullify
  has_many :light_boxes, dependent: :destroy
  belongs_to :title_provider_group_name

  belongs_to :server
  belongs_to :country

  validates :server, associated: true
  validates :country, associated: true
  validates :server_id, presence: true
  validates :country_id, presence: true

  validates :name, presence: true, uniqueness: { case_sensitive: false, scope: :server_id }
  #validates :email, format: { with: /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/ }

  def list_dpi
    DPI.join(",")
  end

  def self.create_title(name, tpgn)
    self.create(name: name, title_provider_group_name_id: tpgn, server_id: Server.find_by(is_self: true).id, country_id: Country.find_by(name: 'France').id)
  end

  private

  def self.order_by_name_select_country(pays,session_pixtech='')
    if pays == 0
      if session_pixtech.blank?
        Title.joins(:server).where.not(servers: {which_type: 'PIXTECH'}).order("titles.name ASC").select(:id,'titles.name')
      else
        Title.joins(:server).where(servers: {which_type: 'PIXTECH'}).order("titles.name ASC").select(:id,'titles.name')
      end
    else
      if session_pixtech.blank?
        Title.joins(:server).where(country_id: pays).where.not(servers: {which_type: 'PIXTECH'}).order("titles.name ASC").select(:id,'titles.name')
      else
        Title.joins(:server).where(country_id: pays, servers: {which_type: 'PIXTECH'}).order("titles.name ASC").select(:id,'titles.name')
      end
    end
  end

  def self.order_by_name_visible_select_country(pays,session_pixtech='')
    if pays == 0
      if session_pixtech.blank?
        Title.joins(:server).where(visible: [1,2,3]).where.not(servers: {which_type: 'PIXTECH'}).order("titles.name ASC").select(:id,'titles.name')
      else
        Title.joins(:server).where(servers: {name: Pixways.get_pixtech_param_value(session_pixtech,'server_name') }).order("titles.name ASC").select(:id,'titles.name')
      end
    else
      if session_pixtech.blank?
        Title.joins(:server).where(visible: [1,2,3], country_id: pays).where.not(servers: {which_type: 'PIXTECH'}).order("titles.name ASC").select(:id,'titles.name')
      else
        Title.joins(:server).where(country_id: pays, servers: {name: Pixways.get_pixtech_param_value(session_pixtech,'server_name') }).order("titles.name ASC").select(:id,'titles.name')
      end
    end
  end
  def self.order_by_name(visible='visible',server=0,session_pixtech=nil)
    case visible
    when 'visible'
      visible_values = [1,2,3]
    when 'all'
      visible_values = [0,1,2,3]
    else
      visible_values = [1,2,3]
    end
    if server != 0
      if session_pixtech.nil?
        Title.joins(:server).where(visible: visible_values, server_id: server).where.not(servers: {which_type: 'PIXTECH'}).order("titles.name ASC").select(:id,'titles.name')
      else
        Title.joins(:server).where(visible: visible_values, server_id: server, servers: {which_type: 'PIXTECH'}).order("titles.name ASC").select(:id,'titles.name')
      end
    elsif session_pixtech == 'all'
      Title.joins(:server).where(visible: visible_values, servers: {which_type: 'PIXTECH'}).order("titles.name ASC").select(:id,'titles.name')
    elsif !session_pixtech.nil?
      Title.joins(:server).where(visible: visible_values, servers: {name: Pixways.get_pixtech_param_value(session_pixtech,'server_name')}).order("titles.name ASC").select(:id,'titles.name')
    else
      Title.joins(:server).where(visible: visible_values).where.not(servers: {which_type: 'PIXTECH'}).order("titles.name ASC").select(:id,'titles.name')
    end
  end

  def self.export_to_csv(titles, icono_title, icono_users, icono_test_title, icono_test_users, part_title, part_users, provs, current_user)
    export = ""
    export.encode!('UTF-8')
    tabr = ["Non visible", "Vignette", "Plein écran", "BD", "HD"]
    CSV(export, {col_sep: ";"}) do |csv|
      csv << [I18n.t('title'), I18n.t('group'), I18n.t('type'), I18n.t('first_name'), I18n.t('last_name'), I18n.t('email'), I18n.t('phone'), I18n.t('address'), I18n.t('admin.providers.zip_code'), I18n.t('admin.providers.city'), I18n.t('admin.providers.country'), I18n.t('status'), I18n.t('restriction'), I18n.t(:updated_at_recently)]
      titles.each do |p|
        contact_data = [p.visible_name, p.group, p.title_type, p.first_name, p.last_name, p.email, p.phone, p.address, p.zip_code, p.city, (p.country.name rescue 'France')]
        contact_data << I18n.t(TITLE_VISIBLE[p.visible])
        autho = {}
        tpgn = p.title_provider_group_name_id
        provs.each do |prov|
          tpg = TitleProviderGroup.where(title_provider_group_name_id: tpgn, provider_id: prov)
          autho[prov] = 0
          autho[prov] = Authorization.where(title_provider_group_id: tpg.first.id).count + 1 unless tpg.blank?
        end unless current_user.is_superadmin?
        restrictions = ""
        autho.each do |k,v|
          restrictions += "#{Provider.find(k).name} => #{tabr[v]}\n"
        end
        contact_data << restrictions.strip
        contact_data << I18n.l(p.updated_at, format: "%d/%m/%Y") if p.updated_at >= Time.now.months_ago(6)
        csv << contact_data
      end
      unless icono_title.nil?
        icono_users.each do |p|
          contact_data = [icono_title, "", "", p.first_name, p.last_name, p.email, p.phone, "","","","","",""]
          contact_data << I18n.l(p.updated_at, format: "%d/%m/%Y") if p.updated_at >= Time.now.months_ago(6)
          csv << contact_data
        end
      end
      unless icono_test_title.nil?
        icono_test_users.each do |p|
          contact_data = [icono_test_title, "", "", p.first_name, p.last_name, p.email, p.phone, "","","","","",""]
          contact_data << I18n.l(p.updated_at, format: "%d/%m/%Y") if p.updated_at >= Time.now.months_ago(6)
          csv << contact_data
        end
      end
      unless part_title.nil?
        part_users.each do |p|
          contact_data = [part_title, "", "", p.first_name, p.last_name, p.email, p.phone, "","","","","",""]
          contact_data << I18n.l(p.updated_at, format: "%d/%m/%Y") if p.updated_at >= Time.now.months_ago(6)
          csv << contact_data
        end
      end
    end
    export
  end

end
