# == Schema Information
#
# Table name: sites
#
#  id         :integer          not null, primary key
#  name       :string(255)
#  url        :string(255)
#  key        :string(255)
#  site_type  :integer
#  created_at :datetime         not null
#  updated_at :datetime         not null
#

class Site < ApplicationRecord
  # add: 添加类链接
  # personal: 个人名下类链接,
  # common: 普通链接
  # dict: 字典类型
  # other 网站自定义参数
  enum site_type: { add: 0, personal: 1, common: 2, dict: 3, other: 4 }

  scope :by_search,     -> (keyword){ where("name LIKE :keyword OR url LIKE :keyword OR `key` LIKE :keyword", keyword: "%#{strip_param(keyword)}%") unless strip_param(keyword).blank? }
  scope :by_site_type,  -> (site_type){ where(site_type: strip_param(site_type)) unless strip_param(site_type).blank? }

  def self.set_default_menu
    set_add_menu!
    set_personal_menu!
    set_common_menu!
  end

  def self.has_notice_menu?
    self.common.where(key: 'notice').present?
  end

  def self.has_blockchain?
    self.common.where(key: 'blockchain').present?
  end

  private
    def self.set_add_menu!
      adds= [
        {name: '新建项目', key: 'add_mirror_project', url: '/projects/mirror/new'},
        {name: '导入项目', key: 'add_common', url: '/projects/deposit/new'},
        {name: '新建组织', key: 'add_r', url: '/organize/new'}]

      adds.each { |ele|
        Site.find_or_create_by(key: ele[:key]) do |site|
          site.name = ele[:name]
          site.url = ele[:url]
          site.site_type = Site.site_types[:add]
        end
      }
    end

    def self.set_personal_menu!
      personals = [
        {name: '个人中心', key: 'my_page', url: '/users/current_user'},
        {name: '我的组织', key: 'my_organizes', url: '/users/current_user/organizes'}
      ]

      personals.each { |ele|
        Site.find_or_create_by(key: ele[:key]) do |site|
          site.name = ele[:name]
          site.url = ele[:url]
          site.site_type = Site.site_types[:personal]
        end
      }
    end

    def self.set_common_menu!
      commons = [
        {name: '通知', key: 'notice', url: '/users/current_user/user_messages'},
        {name: '找回密码', key: 'lost_password', url: '/account/lost_password'},
        {name: '注册', key: 'register', url: '/register'}
      ]

      commons.each { |ele|
        Site.find_or_create_by(key: ele[:key]) do |site|
          site.name = ele[:name]
          site.url = ele[:url]
          site.site_type = Site.site_types[:common]
        end
      }
    end

    def self.get_dict_data(type)
      professional_fields =[]
      Site.dict.where(key: type).select(:name, :url).each do |site|
        hash = { name: "#{site.name}", value: "#{site.url}" }
        professional_fields << hash
      end
      professional_fields
    end

    def self.dict_str(type, value)
      dict_data = Site.get_dict_data(type)
      dict_key = dict_data.pluck(:name)
      dict_value = dict_data.pluck(:value)
      index = dict_value.index("#{value}")
      index.present? ? dict_key[index] : ""
    end
end
