module OrbitApp
    module Module
      module Registration
      Version = "0.2"

        module ClassMethods
            @@registrations = []

          def new( name ,&block)
            @@registrations << DataSheet.new(name,&block)
          end

          def find_by_key(key)
            @@registrations.each{|t|
              return t if t.key == key
            }
            return nil
          end

          def all
            return @@registrations
          end
        end
      
        extend ClassMethods
        def self.included( other )
          other.extend( ClassMethods )
        end

        class DataSheet
          attr_reader :name,:key,:base_path,:module_label,:data_count, :has_category, :has_tag, :approvable_models, :authorizable_models, :is_approvable, :is_authorizable

          def initialize(name, &block)
            @name = name
            @key = name.underscore.singularize
            @side_bar = nil
            @front_end_app_pages = nil
            @module_label = 'rulingcom.errors.init.module_app_noname'
            @data_count = 1..15 # as default
            @has_category = nil
            @has_tag = nil
            @approvable_models = []
            @authorizable_models = []
            @is_approvable = nil
            @is_authorizable = nil
            block.arity < 1 ? instance_eval(&block) : block.call(self) if block_given?
            setup_module_app
          end

          def get_module_app
            ModuleApp.first(conditions: {:key=>@key,:title=>name})
          end

          def setup_module_app
            module_app = get_module_app
            module_app = ModuleApp.new(:key=>@key,:title=>name) if module_app.nil?
            module_app.refetch_setting!(self)
            begin
              module_app.save(:validate=>false)
            rescue
              retry
            end

          end

          def get_label_i18n
            @module_label 
          end

          def get_default_widget_fields
            return @widget_set.get_fields
          end

          def get_link_methods
            return @widget_set.get_link_methods
          end

          def get_default_widget
            if @widget_set.nil? # Init not defining widget
              return {}
            elsif @widget_set.get_default_widget.nil? # Init had defining widget,but say nothing about default_widget
              return {}
              else
                @widget_set.get_default_widget.to_module_app_format
            end
          end

          def get_enable_frontend
            (@front_end_app_pages.nil? ? false : true)
          end

          def get_app_pages
            @front_end_app_pages.nil? ? [] : @front_end_app_pages.to_module_app_format
          end

          def get_data_count
            @data_count 
          end

          def get_icon_class
            @side_bar.get_icon_class
          end

          def get_widget_by_path(path)
            if @widget_set
              @widget_set.find_by_path(path) 
            else
              nil
            end
          end

          def default_widget_setting
            @widget_set.default_widget_setting
          end

          def widget_setting
            @widget_set
          end

          def get_widgets
            @widget_set.nil? ? {} : @widget_set.to_module_app_format
          end

          def front_end(&block) #setter for front_end from init
            @front_end_app_pages = FrontendUtility::AppPageSet.new(@name,@key,&block)
          end

          def side_bar(&block) #setter for side_bar from init
            @side_bar = SideBarRegisition::SideBar.new(@name,@key,method(:get_module_app), is_approvable_with_link?, is_authorizable_with_link?, &block)
          end

          def personal_plugin(params) #setter for personal_plugin from init
            # TODO 這裡要看是一些檔案是不是都有
            Plugin::Registration.new_from_module_app(@name,@key,@base_path,params)
          end    

          def widgets(&block) #setter for widget from init
            # @widgets = WidgetRegisition::WidgetSet.new(&block)
            @widget_set = WidgetUtility::WidgetSet.new(@name,@key,&block)

            # @widgets = widget_set.widgets
            # @default_widget = widget_set.default_widget
          end

          %w{data_count module_label category base_url version organization author intro update_info}.each  do |field|
              define_method(field){|var| instance_variable_set( "@" + field, var)}
          end

          def approvable(link=true, &block)
            @is_approvable = {:with_link => link}
            if block
              block.call
            else
              approvable_on
            end
          end

          def authorizable(link=true, &block)
            @is_authorizable = {:with_link => link}
            if block
              block.call
            else
              authorizable_on
            end
          end

          def approvable_on(klass = 'Category')
            @approvable_models << klass
          end

          def authorizable_on(klass = 'Category')
            @authorizable_models << klass
          end

          def categorizable
            @has_category = true
          end

          def taggable
            @has_tag = true
          end

          def get_approvable_models
            @approvable_models
          end

          def get_authorizable_models
            @authorizable_models
          end

          def get_has_category
            @has_category == true
          end

          def get_has_tags
            @has_tag == true
          end

          def get_is_approvable
            !@is_approvable.blank?
          end

          def get_is_authorizable
            !@is_authorizable.blank?
          end

          def is_approvable_with_link?
            @is_approvable && @is_approvable[:with_link]
          end

          def is_authorizable_with_link?
            @is_authorizable && @is_authorizable[:with_link]
          end

        end # of class DataSheet

    end
  end
end