class Knowledge < NodeEntityAbstract

  has_many :knowledge_figures, :dependent => :destroy

#  validates_length_of :title, :within => 0..400
  validates_length_of :textbody, :within => 0..100000
  validates_length_of :description, :within => 0..4000
  validates_format_of :category, :with => /^[a-zA-Z0-9_]+$/, :message => "you can only use alphabets and numbers and underscore to Category."
  
  #<< class methods >>

  class << self
    def read_parameters_of_knowledge_document(path, user)
      knowledge = Knowledge.find(:first, :conditions => ["path=?", path], :user => user)
      comments = Knowledge.find(:all, :conditions => ["comment_on = ?" ,knowledge.node_id])
      knowledge_figures = KnowledgeFigure.find(:all, :conditions => ["knowledge_id=?", knowledge.id]) # not find -> return []

      # = 絵のパスと大きさ
      #   * 絵のパスをnodesテーブルのpathより取得
      #   * 絵の大きさをimagesテーブルのvizshotより取得
      image_paths = Array.new
      image_widths = Array.new
      image_heights = Array.new
      caption_widths = Array.new
      caption_heights = Array.new
      knowledge_figures.each do |kf|
        unless (image = Image.find(kf.image_id, :user => user))
          raise "Figure is not found.\n"
        else
          image_paths << image.path
          vizshot_yaml = image.vizshot
          begin
            vizshot = YAML.load(vizshot_yaml)
            if (image_size = vizshot.get_size)
              image_widths << image_size[0]
              image_heights << image_size[1]
              caption_widths << (image_size[0] * 0.8).to_i
              caption_heights << (image_size[1] * 0.8).to_i
            else
              # vizshotから復元できなかったときはnormalサイズを適用する
              image_widths << 554
              image_heights << 554
              caption_widths << 440
              caption_heights << 440
            end
          rescue
            # vizshotが無いときはnormalサイズを適用する
            image_widths << 250
            image_heights << 254
            caption_widths << 440
            caption_heights << 440
          end
        end
      end
      return knowledge, knowledge_figures, comments, image_paths, image_widths, image_heights, caption_widths, caption_heights
    end
    
    # == 新たなコメントに対してコメント番号を割り振る
    def new_comment_number_from_parent_document(parent_knowledge_node_id, user)
      comments = Knowledge.find(:all, :conditions => ["comment_on = ? AND comment_number IS NOT NULL", parent_knowledge_node_id], :user => user)
      if comments.length > 0
        comments.collect! {|comment|
          comment = comment.comment_number
        }
        comment_number = comments.max + 1
      else
        comment_number = 1
      end
    end
    
    # OpenIDの接頭につく http:// や https:// 、また末尾につく / を取り除く
    def remove_scheme(user)
      if user.internal_user
        user.login
      else
        username_in_path = user.login
        case username_in_path
        when /^http:\/\/(.+)/, /^https:\/\/(.+)/
          username_in_path = $1
        end
        
        case username_in_path
        when /(.+)\/$/
          username_in_path = $1
        end
        return username_in_path
      end
    end
    
  end
end



