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

class PlacemarksController < ApplicationController

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

    actions :index, :show, :edit, :update, :new, :create do
      allow :admin
      allow logged_in
    end
    
    actions :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])
      @placemarks = Placemark.find_all_by_layer_id(@layers)  
    else 
      @placemarks = Placemark.find(:all)
    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    
  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
        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"
          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.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!
      redirect_to placemark_path(@placemark, :anchor => "")
    else
      flash[:notice]="Sorry. Der Kommentar konnte nicht gespeichert werden!"
      redirect_to placemark_path(@placemark, :anchor => "comment_form")
    end
      
 
  end
  
  
    
  
end
