module Archive
  class Engine < ::Rails::Engine
    
    initializer "archive" do 
      require File.expand_path('../../../app/models/archive_category', __FILE__)
      require File.expand_path('../../../app/models/archive_sort_order', __FILE__)
      if defined?(ArchiveSortOrder)
        if ArchiveSortOrder.count == 0
          ArchiveSortOrder.create('sort_order' => false)
        end
      end
      require "#{ENV['PWD']}/app/models/category"
      require "#{ENV['PWD']}/app/models/module_app"
      
      tmp = OrbitApp.registration "Archive", :type => "ModuleApp" do
        module_label "archive.archive"
        base_url File.expand_path File.dirname(__FILE__)
        feeds_url "/xhr/archive_files/feeds"
        begin
          feeds_model "ArchiveFile"
          feeds_uid_field "feed_uid"
          feeds_update_callback "update_feed_data"
          feeds_update_statuses_callback "update_feed_statuses"
          feeds_time_field "created_at"
          feeds_finish_callback "feeds_finish_callback"
        rescue => e
          puts "orbit kernel need update!"
        end
        widget_methods ["widget","group_by_category"]
        widget_settings [{"data_count"=>30,"enable_custom_widget_data"=>true}]
        models_to_cache [:archive_file]
        begin
          avoid_page_cache ArchiveCache
          avoid_page_cache ArchiveFileFeed
          avoid_page_cache ArchiveFileFeedCache
        rescue => e
          puts ["avoid_page_cache", e.to_s]
        end
        if ENV['worker_num']=='0' && File.basename($0) != 'rake' && !Rails.const_defined?('Console')
          require File.expand_path('../../../app/models/archive_cache', __FILE__)
          if defined?(ArchiveCache)
            ArchiveCache.all.delete
          end
        end
        taggable "ArchiveFile"
        categorizable
        authorizable
        frontend_enabled
        side_bar do
          head_label_i18n 'archive.archive', icon_class: "icons-archive"
          available_for "users"
          active_for_controllers (['admin/archive_files'])
          head_link_path "admin_archive_files_path"

          context_link 'all',
                :link_path=>"admin_archive_files_path" ,
                :priority=>1,
                :active_for_action=>{'admin/archive_files'=>"index"},
                :available_for => 'users'
          context_link 'new_',
                :link_path=>"new_admin_archive_file_path" ,
                :priority=>2,
                :active_for_action=>{'admin/archive_files'=>"new"},
                :available_for => 'sub_managers'

          context_link 'categories',
                :link_path=>"admin_module_app_categories_path" ,
                :link_arg=>"{:module_app_id=>ModuleApp.find_by(:key=>'archive').id}",
                :priority=>3,
                :active_for_action=>{'admin/archive_files'=>'categories'},
                :active_for_category => 'Archive',
                :available_for => 'managers'

          context_link 'tags',
                :link_path=>"admin_module_app_tags_path" ,
                :link_arg=>"{:module_app_id=>ModuleApp.find_by(:key=>'archive').id}",
                :priority=>4,
                :active_for_action=>{'admin/archive_files'=>'tags'},
                :active_for_tag => 'Archive',
                :available_for => 'managers'
          context_link 'archive.categories_order',
                :link_path=>"admin_archive_files_categories_order_path" ,
                :priority=>5,
                :active_for_action=>{'admin/archive_files'=>'categories_order'},
                :available_for => 'managers'
          context_link 'archive.feed_list',
                :link_path=>"admin_archive_file_feed_path" ,
                :priority=>6,
                :active_for_action=>{'admin/archive_files'=>'feed'},
                :available_for => 'managers'
        end
      end
      categories_temp = ModuleApp.where(:key => "archive").first.categories
      
      categories_temp.each do |category|
        archive_cat = ArchiveCategory.where(:category_id => category.id)
        if archive_cat.count == 0
          ArchiveCategory.create(category_id: category.id.to_s)
        else
          archive_cat[1..-1].each do |cat|
            cat.delete
          end
        end
      end
      tmp
    end
  end
end
