class Writing
  include Mongoid::Document
  include Mongoid::Timestamps
  include OrbitModel::Status
  include Slug
  include OrbitCategory::Categorizable
  belongs_to :member_profile

  field :writing_title, :as=>:slug_title, localize: true
  field :other, localize: true
  field :month
  field :year
  field :url
  field :create_user_id, :type => BSON::ObjectId
  field :update_user_id, :type => BSON::ObjectId

  # paginates_per 10
  
  has_many :writing_files, :autosave => true, :dependent => :destroy
  accepts_nested_attributes_for :writing_files, :allow_destroy => true

  before_validation :add_http

  scope :sort_for_frontend, ->{ where(:is_hidden=>false).order_by(:year => "desc") }

  def get_plugin_data(fields_to_show)
    plugin_datas = []
    fields_to_show.each do |field|
      plugin_data = self.get_plugin_field_data(field) rescue nil
      next if plugin_data.blank? or plugin_data['value'].blank?
      plugin_datas << plugin_data
    end
    plugin_datas
  end

  def self.get_plugin_datas_to_member(datas)

    fields_to_show = [
      "year",
      "month",
      "writing_title",
      "other",
      "url"
    ]

    fields_to_remove = []

    pd_title = []

    fields_to_show.each do |t|
      if (self.fields[t].type.to_s == "String" rescue false)
        fields_to_remove << t if (datas.where(t.to_sym.ne => nil).or(t.to_sym.ne => "").count == 0 rescue false)
      else
        fields_to_remove << t if (datas.where(t.to_sym.ne => "").count == 0 rescue false)
      end
      pd_title <<  {
          "plugin_data_title" => I18n.t("personal_writing.#{t}")
        } if !fields_to_remove.include?(t)
    end

    fields_to_show = fields_to_show - fields_to_remove

    plugin_datas = datas.sort_for_frontend.collect do |p|

      pd_data = []
      fields_to_show.collect do |t|
        if t == "writing_title"
          pd_data << { "data_title" => "<a href='#{OrbitHelper.url_to_plugin_show(p.to_param,'personal_writing')}' title='#{p.send(t)}' target='_blank'>#{p.send(t)}" }
        else
          pd_data << { "data_title" => p.send(t) }
        end
      end

      {
        "pd_datas" => pd_data
      }

    end

    return [pd_title,plugin_datas]

  end

  def get_plugin_field_data(field)
    case field
    when "language"
      value = I18n.t(self.language) rescue ""
    when "file"
      files = []
      self.writing_files.each do |writing_file|  
        url = writing_file.file.url
        title = (writing_file.title.blank? ? File.basename(writing_file.file.path) : writing_file.title)
        files << "<li><a href='#{url}'' target='_blank'>#{title}</li>"
      end
      value = files.join("")
    else
      value = self.send(field) rescue ""
    end

    value = (value =~ /\A#{URI::regexp(['http', 'https'])}\z/) ? "<a href='#{value}' target='blank'>#{value}</a>" : value
    
    {
      "key"=>field,
      "title_class"=>"writing-#{field.gsub('_','-')}-field",
      "value_class"=>"writing-#{field.gsub('_','-')}-value",
      "title"=>I18n.t('personal_writing.'+field),
      "value"=>value
    }
  end

  protected

  def add_http
    unless self.url.blank? || self.url[/^http:\/\//] || self.url[/^https:\/\//]
      self.url = 'http://' + self.url
    end
  end

end