module PsAdmin
  # All Page controller
  class AllPageController < ApplicationController
    before_action :authenticate_admin!
    layout 'admin'

    def index
      @all_pages = AllPage.where('type_of != 3')
      pp_pages
    end

    def show; end

    def new
      @all_page = AllPage.new
      @articles = Article.all
    end

    def create
      @all_page = AllPage.new(all_page_params)
      @all_page.updated_by = current_admin.id
      @all_page.updated_at = Time.now
      @all_page.published = false
      @articles = Article.all
      if @all_page.save
        if params[:publish] == '1'
          params[:id] = @all_page.id
          publish
        end
        redirect_to action: 'index'
      else
        render :new
      end
    end

    def edit
      @all_page = AllPage.find(params[:id])
      @articles = Article.all
    end

    def update
      @all_page = AllPage.find(params[:id])
      @all_page.updated_by = current_admin.id
      @all_page.updated_at = Time.now
      @all_page.published = false
      if @all_page.update_attributes(all_page_params)
        if params[:publish] == '1'
          params[:id] = @all_page.id
          publish
        end
      else
        @articles = Article.all
        @all_page.errors.full_messages.each do |msg|
          puts "<li>#{msg}</li>"
        end
        render :edit
      end
    end

    def destroy
      @all_page.destroy if (@all_page = AllPage.find(params[:id]))
      redirect_to action: 'index'
    end

    def pp_priority_up
      pp_this = PublishedPage.find(params[:id])
      pp_this_prio = pp_this.priority
      pp_last = PublishedPage.where('(type_of = 1 OR type_of = 2) AND priority < ?', pp_this_prio).order('priority DESC').first
      pp_this.priority = pp_last.priority
      pp_this.save
      pp_last.priority = pp_this_prio
      pp_last.save
      pp_pages
    end

    def pp_priority_down
      pp_this = PublishedPage.find(params[:id])
      pp_this_prio = pp_this.priority
      pp_last = PublishedPage.where('(type_of = 1 OR type_of = 2) AND priority > ?', pp_this_prio).order('priority ASC').first
      pp_this.priority = pp_last.priority
      pp_this.save
      pp_last.priority = pp_this_prio
      pp_last.save
      pp_pages
    end

    def publish
      @all_page = AllPage.find(params[:id])
      return if @all_page.blank?
      unless (@published_page = @all_page.published_page
             )
        @published_page = PublishedPage.new
        @published_page.priority = PublishedPage.all.size + 1
        @published_page.created_by = current_admin.id
        @published_page.created_at = Time.now
      end
      @published_page.name = @all_page.name
      @published_page.title = @all_page.title
      @published_page.meta_description = @all_page.meta_description
      @published_page.all_page_id = @all_page.id
      @published_page.nofollow = @all_page.nofollow
      @published_page.type_of = @all_page.type_of
      @published_page.full_text = @all_page.full_text
      @published_page.article_id = @all_page.article_id
      @published_page.updated_by = current_admin.id
      @published_page.updated_at = Time.now
      if @published_page.save
        @all_page.published = true
        @all_page.save
      end
    end

    def unpublish
      @all_page = AllPage.find(params[:id])
      return if @all_page.blank?
      @all_page.published_page.destroy if @all_page.published_page
      @all_page.published = false
      @all_page.save
    end

    private

    def pp_pages
      @published_pages = PublishedPage.where('type_of = 1 OR type_of = 2')
                                      .order('priority ASC')
      @pp_first = PublishedPage.where('type_of = 1 OR type_of = 2')
                               .order('priority ASC').first
      @pp_last = PublishedPage.where('type_of = 1 OR type_of = 2')
                              .order('priority DESC').first
    end

    def all_page_params
      params.require(:all_page).permit(:name, :article_id, :title, :type_of,
                                       :full_text)
    end
  end
end
