# encoding: utf-8
class EventNewsModuleController < ApplicationController
  before_filter :set_I18n

  def get_event_news
    page = Page.where(:module => "event_news").first rescue nil

    # 頁次
    page_num = params[:page_num].blank? ? 0 : params[:page_num].to_i
    # 每頁顯示的則數
    per_page = params[:per_page].blank? ? 10 : params[:per_page].to_i
    per_page = per_page > 0 ? per_page : 10

    I18n.locale = :zh_tw
    if !params[:keyword].blank?
      keyword = Regexp.new(".*"+params[:keyword]+".*")
      event_news = EventNews.any_of({:title=>keyword},{:subtitle=>keyword},{:text=>keyword})
    else
      event_news = EventNews.all
    end

    if !params[:category].blank?
      module_id = ModuleApp.where(:key=>"event_news").first.id
      category = Regexp.new(".*"+params[:category]+".*")
      category_id = Category.where(:title => category, :module_app_id => module_id).first.id
      event_news = event_news.where(:category_id => category_id)
    else
      event_news = event_news
    end

    event_news = event_news.where(:is_preview.in=>[false,nil])
    event_news = event_news.where(:approved.ne => false , :rejected.ne => true)
    event_news = event_news.where(:postdate.lt=>Time.now)
    event_news = event_news.desc( :is_top, :postdate).page(page_num).per(per_page)

    event_news = event_news.collect do |b|
      image = request.protocol + request.host_with_port + b.image.url rescue nil

      links = b.event_news_links.collect do |bl|
        {
          "title" => bl.title_translations,
          "url" => bl.url
        }
      end rescue nil

      files = b.event_news_files.collect do |bf|
        file = request.protocol + request.host_with_port + bf.file.url rescue nil
        {
          "title" => bf.title_translations,
          "description" => bf.description_translations,
          "file" => file
        }
      end rescue nil

      ts = b.tags.collect do |t|
        {
          "name" => t.name_translations
        }
      end rescue nil

      text = {"en" => "", "zh_tw" => ""}
      text["en"] = (b.text_translations["en"].nil? ? "" :smart_convertor(b.text_translations["en"]))
      text["zh_tw"] = (b.text_translations["zh_tw"].nil? ? "" : smart_convertor(b.text_translations["zh_tw"]))

      author = User.find(b.create_user_id).member_profile.name rescue ""

      {
        "id" => b.id.to_s,
        "title" => b.title_translations,
        "subtitle" => b.subtitle_translations,
        "text" => text,
        "postdate" => b.postdate,
        "deadline" => b.deadline,
        "category" => b.category.title_translations,
        "tags" => ts,
        "image" => image,
        "links" => links,
        "files" => files,
        "author" => author,
        "url" => "/#{I18n.locale.to_s + page.url}/#{b.to_param}"
      }

    end

    # 計算總筆數 Start
    if !params[:keyword].blank?
      keyword = Regexp.new(".*"+params[:keyword]+".*")
      event_news = EventNews.any_of({:title=>keyword},{:subtitle=>keyword},{:text=>keyword})
    else
      event_news = EventNews.all
    end
    event_news = event_news.where(:is_preview.in=>[false,nil])
    event_news = event_news.where(:approved.ne => false , :rejected.ne => true)
    event_news = event_news.where(:postdate.lt=>Time.now)
    total_pages = event_news.count
    # End

    render :json => {
      "event_news" => event_news,
      "event_news_count" => event_news.count,
      "page_num" => page_num,
      "total_pages" => total_pages,
    }.to_json
  end

  def smart_convertor(text)
    html_string = text
    links = html_string.scan(/img.*?src="(.*?)"/i)
    links.each do |link|
      l = link.first
      new_link = nil
      if l.starts_with?("/")
        new_link = request.protocol + request.host_with_port + l
      elsif l.starts_with?("..")
        l1 = l.gsub("../","")
        new_link = request.protocol + request.host_with_port + "/" + l1
      end
      html_string = html_string.sub(l,new_link) if !new_link.nil?
    end
    return html_string
  end

  protected

  def set_I18n
    I18n.locale = params[:lang] if params[:lang].present?
  end

end