class Admin::PagesController < Admin::ItemsController

  def show
    @item = Page.find(params[:id])
    @no_orbit_bar = true
    @edit = true
    render :text => parse_page_content(@item), :layout => 'page_layout'
  end

  def create
    @item = Page.new(params[:page])
  	if @item.module_app && @item.module_app.key == 'page_content'
  	  @item.page_contexts.build(:create_user_id => current_user.id, :update_user_id => current_user.id )
  	end
    if @item.save
      render 'admin/items/reload_items'
    else
      @error = error_messages_for(@item)
      render 'admin/items/form_error'
    end
  end

  def update
    if params[:page][:category]
      if params[:page][:category][0] == ""
        params[:page][:category] = []
      end
    end
    if params[:page][:tag]
      if params[:page][:tag][0] == ""
        params[:page][:tag] = []
      end
    end

    @item = Page.find(params[:id])
    if @item.module_app && @item.module_app.key == 'page_content' && @item.page_contexts.blank?
      @item.page_contexts.build(:create_user_id => current_user.id, :update_user_id => current_user.id )
    end
    if @item.update_attributes(params[:page])
      unless params[:page]['frontend_field'].nil?
        frontend_class = []
        frontend_field_name = []
        frontend_sat_to_link = []

        params[:page]['frontend_field'].each do |field|
          frontend_class << field['class']
          frontend_field_name << field['field_name']
          frontend_sat_to_link << field['sat_to_link']
        end

        @item['frontend_class'] = frontend_class
        @item['frontend_field_name'] = frontend_field_name
        @item['frontend_sat_to_link'] = frontend_sat_to_link
      end
      
      @item['tag'] = params[:page]['tag'] == nil ?  [] : params[:page]['tag']

      @item.save

      render 'admin/items/reload_items'
    else
      @error = error_messages_for(@item)
      render 'admin/items/form_error'
    end
  end

  def get_display_style
    @item = Page.find(params[:id]) rescue nil
    @module_app = ModuleApp.find(params[:module_id]) rescue nil
    if @module_app
      if params[:val].eql?("default_widget")
        @checked_style = @item.frontend_style.present? ? @item.frontend_style : nil if @item && @item.app_frontend_url.eql?("default_widget")
        @enabled_styles = @module_app.get_default_widget["enabled_styles"]
        @widget_fields = @module_app.widget_fields.collect do |widget_field| 
          label = I18n.t("#{@module_app.key}.default_widget.#{widget_field[0]}")
          [label, widget_field[0], class: widget_field[2]] 
        end
        @class_options = LIST[:widget_field_type].collect{|widget_field| [widget_field.humanize, widget_field]}
        @partial = 'default_front'
      else
        @frontend_styles = @module_app.app_pages[params[:val]]["style"] rescue nil
        @partial = 'custom_front' if @frontend_styles.present?
      end
    end
  end

  def get_page_setting_json
    begin
      page = Page.find(params[:id])
      design = {main: page.design_id.to_s, sub: page.theme_id.to_s}
      m = {}
      m["main"] = page.module_app_id.to_s
      m["sub"] = page.app_frontend_url
      m["category"] = page.category.present? ? page.category.map{|c| c.to_s} : []
      m["tags"] = page.tag.present? ? page.tag.map{|c| c.to_s} : []
      m["public"] = page.is_published ? 1 : 0
      m["link"] = @site_in_use_locales.inject([]) do |link, locale|
        link << [locale, (page.enabled_for && page.enabled_for.include?(locale)) ? 1 : 0, (page.menu_enabled_for && page.menu_enabled_for[locale].eql?('true')) ? 1 :0]
      end
      m["count"] = page.frontend_data_count 
      render json: JSON.pretty_generate({design: design, module: m})
    rescue
      render :json => {error: ''}, status: 422
    end
  end

end
