module PsAdmin
  # Categories
  class CategoryController < ApplicationController
    before_action :authenticate_admin!
    layout 'admin'

    def index
      @category = if params[:parent_id].blank? || params[:parent_id].to_i.zero?
                    Category.where(id: 1).first
                  else
                    Category.where(id: params[:parent_id]).first
                  end
      return if @category.blank?
      @categories = Category.where(parent_id: @category.id).order('name ASC')
    end

    def new
      @category = Category.new(parent_id: params[:parent_id])
    end

    def create
      @category = Category.new(category_params)
      if @category.save
        if params[:image]
          image = params[:image]
          img = Image.new(image: image, updated_by: current_admin.id,
                          position: 1, cover: true, imageable: @category)
          img.save
        end
        respond_to do |format|
          format.html { redirect_to action: 'index' }
          format.js do
            @categories =
              Category.where(parent_id: @category.parent_id).order('name ASC')
          end
        end
      else
        render 'new'
      end
    end

    def edit
      @category = Category.where(id: params[:id]).first
    end

    def update
      @category = Category.where(id: params[:id]).first
      if @category.update_attributes(category_params)
        if params[:image]
          image = params[:image]
          if @category.image.blank?
            img = Image.new(image: image, updated_by: current_admin.id,
                            position: 1, cover: true, imageable: @category)
          else
            img =
              Image.where('imageable_type = ? AND imageable_id = ?', 'Category',
                          @category.id).first
            img.update_attributes(image: image,
                                  updated_by: current_admin.id)
            # @category.image
          end
          img.save
        end
        respond_to do |format|
          format.html { redirect_to action: 'index' }
          format.js do
            @categories = Category.where(parent_id: @category.parent_id)
                                  .order('name ASC')
          end
        end
      else
        render 'edit'
      end
    end

    def destroy
      @category = Category.find(params[:id])
      @category.destroy unless @category.blank?
      redirect_to action: 'index', parent_id: @category.parent_id
    end

    def set_active
      if !params[:id].blank? && !params[:active].blank?
        @category = Category.where(id: params[:id]).first
        unless @category.blank?
          @category.active = params[:active].to_i
          @category.save
          @categories = Category.where(parent_id: @category.parent_id)
                                .order('name ASC')
        end
      end
    end

    def import_categories
      pcls = PsCategoryLang.where('id_lang = 3')
      pcls.each do |pcl|
        category = Category.where(id: pcl.id_category).first
        next unless category.blank?
        pc = PsCategory.where('id_category = ?', pcl.id_category).first
        next if pc.blank?
        cat = Category.new(parent_id: pc.id_parent, level_depth: pc.level_depth,
                           active: pc.active, name: pcl.name,
                           created_by: current_admin.id,
                           updated_by: current_admin.id)
        cat.id = pc.id_category
        cat.save
      end
      render text: 'Done'
    end

    # method purpose
    # deactivates categories with none active products in it [params[:da] == 1]
    # activates categories with active_products in it [params[:da] == 2]
    # activates and deactivates [params[:da] == 0]
    def deactivate_activate
      unless params[:parent_id].blank?
        category = Category.where(id: params[:parent_id]).first
        category.walk_tree do |cat, _level|
          act = nil
          if cat.products.empty?
            # if category has no products check her active_childrens
            if cat.active_childrens.empty?
              # if its empty
              # deactivate category
              act = false if check_condition(params[:da], 1)
            else
              # activate category
              act = true if check_condition(params[:da], 2)
            end
          else
            if cat.active_products.empty?
              # if active products size == 0
              # deactivate category
              act = false if check_condition(params[:da], 1)
            else
              # activate category
              act = true if check_condition(params[:da], 2)
            end
          end
          unless act.nil?
            cat.active = act
            cat.save
          end
        end
      end
    end

    private

    def category_params
      params.require(:category).permit(:name, :parent_id)
    end

    def check_condition(p_da, cond)
      p_da.blank? || p_da.to_i.zero? || p_da.to_i == cond
    end
  end
end
