# coding: utf-8
class Admin::CatalogueCategoriesController < Admin::AdminController

  #todo Замутить так, что бы ROOT был не изменяемым и в view не забыть что бы его выбрать нельзя было

  def index

  end

  def new
    @catalogue_category = CatalogueCategory.new(params[:catalogue_category])
  end

  def edit
    @catalogue_category = CatalogueCategory.find(params[:id])
    if @catalogue_category.root?
      redirect_to admin_catalogue_categories_path, notice: t('dts.messages.cant_edit_root_node_notice')
    end
  end

  def create
    @catalogue_category = CatalogueCategory.new(params[:catalogue_category])
    if @catalogue_category.id && @catalogue_category.id <= 1
      redirect_to admin_catalogue_categories_path, notice: t('dts.messages.cant_edit_root_node_notice')
    end

    if @catalogue_category.save
      redirect_to admin_catalogue_categories_path, notice: view_context.notice_message
    else
      render action: 'new'
    end
  end

  def update
    @catalogue_category = CatalogueCategory.find(params[:id])
    if @catalogue_category.root?
      redirect_to admin_catalogue_categories_path, notice: t('dts.messages.cant_edit_root_node_notice')
    end

    if @catalogue_category.update_attributes(params[:catalogue_category])
      redirect_to admin_catalogue_categories_path, notice: view_context.notice_message
    else
      render 'edit'
    end
  end

  def destroy
    @catalogue_category = CatalogueCategory.find(params[:id])
    if @catalogue_category.root?
      notice = t('dts.messages.cant_remove_root_node_notice')
    else
      @catalogue_category.destroy
      notice = view_context.notice_message
    end

    redirect_to admin_catalogue_categories_path, notice: notice
  end

  def tree_move_node
    moved_category = CatalogueCategory.find(params['moved_node'])
    target_category = CatalogueCategory.find(params['target_node'])

    # argument errors
    if (moved_category.nil? || target_category.nil? || !%w(inside before after).include?(params['position']))
      render :nothing => true, :status => 500
      return
    end

    #logic errors
    if (moved_category.root? || target_category.root? and params['position'] == 'before')
      render :nothing => true, :status => 500
      return
    end

    case params['position']
      when 'inside'
        target_category.add_child moved_category
      when 'before'
        target_category.prepend_sibling moved_category
      when 'after'
        target_category.append_sibling moved_category
    end

    render :nothing => true
  end

  def tree_get_data

    def process_children(result_array, root, children)
      result_root = Hash.new
      result_root['label'] = root.title
      result_root['id'] = root.id
      if children
        result_children = Array.new
        children.each_key do |child|
          process_children(result_children, child, children[child])
        end
        result_root['children'] = result_children
      end

      result_array << result_root
    end

    @tree = Array.new
    catalogue_category_tree = CatalogueCategory.select([:id, :parent_id, :title]).hash_tree
    process_children(@tree, catalogue_category_tree.keys[0], catalogue_category_tree.values[0])

    render :json => @tree
  end


end