#encoding: utf-8

# -*- encoding : utf-8 -*-

class PlacemarksController < ApplicationController

  access_control do
    actions :destroy, :publish, :unpublish  do
      allow :admin
    end

    actions :edit, :update, :new, :create do
      allow :admin
      allow logged_in
    end

    actions :index, :show, :create_comment do
      allow anonymous, logged_in
    end
  end

  # GET /placemarks
  # GET /placemarks.json
  def index

    if params[:map]
      @map = Map.find_by_id(params[:map])
      @layers = Layer.find_all_by_map_id(params[:map])
      if current_user
        @placemarks = Placemark.find(:all,  :order => "updated_at DESC", :conditions => ["layer_id IN (?)", @layers])
      else
        @placemarks = Placemark.find(:all,  :order => "updated_at DESC", :conditions => ["layer_id IN (?) and public = ?", @layers, true])
      end
    else
      if current_user
        @placemarks = Placemark.find(:all, :order => "updated_at DESC", )

      else
        @placemarks = Placemark.find(:all, :order => "updated_at DESC", :conditions => ["public = ?", true])
      end
    end
    @maps = Map.find(:all)

    respond_to do |format|
      format.json { render json: @placemarks }
      format.html # index.html.erb

    end
  end

  # GET /placemarks/1
  # GET /placemarks/1.json
  def show
    @placemark = Placemark.find(params[:id])
    if  @placemark.layer && @placemark.layer.map
      @map = Map.find_by_id(@placemark.layer.map.id)
    end
    @layer = @placemark.layer

    @comment = Comment.new
    @comments = @placemark.comments.recent.all
    @comments.sort! { |b,a| a.created_at <=> b.created_at }

    query = "Nutzungsbedingungen"
    if Page.exists?(['title LIKE ?', "%#{query}%"])
      @tos = Page.find(:first, :conditions => ['title LIKE ?', "%#{query}%"])
    end

    if @placemark.public || current_user
      respond_to do |format|
        format.html # show.html.erb
        format.json { render json: @placemark }
        format.js { render :layout=>false }
      end
    else
       render 'shared/_not_public'
    end
  end

  # GET /placemarks/new
  # GET /placemarks/new.json
  def new
    @placemark = Placemark.new
    @map = Map.find_by_id(params[:map])
    @layers = Layer.find_all_by_map_id(params[:map])
    @districts = DISTRICTS.sort

    query = "Style-Guide"
    if Page.exists?(['title LIKE ?', "%#{query}%"])
      @styleguide = Page.find(:first, :conditions => ['title LIKE ?', "%#{query}%"])
    end

    if @layers.size > 0
      respond_to do |format|
        format.html # new.html.erb
        format.json { render json: @placemark }
      end
    else
      flash[:error] = "Keine Karte oder Ebenen definiert! Bitte zuerst eine Karte auswaehlen (oder neu anlegen)!"
      redirect_to :controller => "maps", :action => "index"
    end
  end

  # GET /placemarks/1/edit
  def edit
    @placemark = Placemark.find(params[:id])
    if  @placemark.layer && @placemark.layer.map
      @map = Map.find_by_id(@placemark.layer.map.id)
    end
    @layers = Layer.all
    @districts = DISTRICTS.sort
    query = "Style-Guide"
    if Page.exists?(['title LIKE ?', "%#{query}%"])
      @styleguide = Page.find(:first, :conditions => ['title LIKE ?', "%#{query}%"])
    end

    if current_user
      if !@placemark.public || (current_user.has_role? :admin )
        render
      else
        flash[:notice] = "Pardon, aber veröffentlichte Orte können nicht mehr bearbeitet werden.\n Inhaltliche Ergänzungen können als Kommentar am Ende dieses Eintrags angefügt werden!\nBist du AutorIn des Ortes? Dann bitte unter info@webmap-global.de melden."
        redirect_to placemark_path(@placemark)
      end
    end
  end

  # POST /placemarks
  # POST /placemarks.json
  def create
    @placemark = Placemark.new(params[:placemark])
    @district = params[:district_string] if params[:district_string] != ""
    @placemark.district = district if params[:placemark][:district_string] == ""
    @placemark.user_id = current_user.id


    respond_to do |format|
      if @placemark.save

        @placemark.send_admin_new_placemark_information!

        format.html { redirect_to @placemark, notice: 'Der Ort wurde erfolgreich angelegt.' }
        format.json { render json: @placemark, status: :created, location: @placemark }
      else
        # @map = Map.find(@layer.map_id)
        # @layers = Layer.find_all_by_map_id(@layer.map_id)
        @layers = Layer.all
        @districts = DISTRICTS.sort
        format.html { render action: "new" }
        format.json { render json: @placemark.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /placemarks/1
  # PUT /placemarks/1.json
  def update
    @placemark = Placemark.find(params[:id])
    @layer = Layer.find(@placemark.layer_id)
    @layers = Layer.all
    @districts = DISTRICTS.sort
    respond_to do |format|
      if @placemark.update_attributes(params[:placemark])
        if params[:commit] == "Speichern und Bearbeitung beenden"
          # @placemark.send_admin_new_placemark_information!
          format.html { redirect_to @placemark, notice: 'Der Ort wurde aktualisiert' }
          format.json { head :ok }
        else
          @map = Map.find(@layer.map_id)
          flash[:notice] = "Der Ort wurde gespeichert!"
          @tab = params[:remember_tab]
          format.html { redirect_to :action => 'edit', :anchor => @tab }
        end
      else
        format.html { render action: "edit" }
        format.json { render json: @placemark.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /placemarks/1
  # DELETE /placemarks/1.json
  def destroy
    @placemark = Placemark.find(params[:id])
    @placemark.destroy

    respond_to do |format|
      format.html { redirect_to placemarks_url }
      format.json { head :ok }
    end
  end

  def publish
    @placemark = Placemark.find(params[:id])
    @placemark.update_attribute(:public, true)
    flash[:notice]="Ort veröffentlicht"
    referer = request.referer
    referer ||= placemark_path(@placemark)
    redirect_to referer
  end

  def unpublish
    @placemark = Placemark.find(params[:id])
    @placemark.update_attribute(:public, false)
    flash[:notice]="Ort versteckt"
    referer = request.referer
    referer ||= placemark_path(@placemark)
    redirect_to referer
  end

  def create_comment
    @comment = Comment.new(params[:comment])
    @placemark = Placemark.find(params[:placemark_id])

    @placemark.add_comment @comment

    if ( params[:the_question].to_i != 5 )
      flash[:notice]="Sorry. Der Kommentar konnte nicht gespeichert werden! #{params[:the_question]} ist nicht die Summe von 2 + 3"
    elsif  @comment.save
      if current_user && (current_user.has_role? :admin)
        flash[:notice]="Danke! Dein Kommentar wurde gesendet."
      else
        flash[:notice]="Danke! Dein Kommentar wurde gesendet und muss nun noch von der Administration freigegeben werden."
      end
      @comment.send_comment_information!
    else
      flash[:notice]="Sorry. Der Kommentar konnte nicht gespeichert werden!"
    end
    redirect_to placemark_path(@placemark, :anchor => "")

  end




end
