# This controller was added by william
class TagsController < ApplicationController

  before_filter :require_admin,:only => :show
  #Added by nie
  #before_filter :require_login,:only => :add_tag
  #end
  include ProjectsHelper
  include IssuesHelper
  include UsersHelper
  include ActsAsTaggableOn::TagsHelper

  $selected_tags = Array.new
  $related_tags = Array.new
  def index

    $selected_tags = []
    $related_tags = []
    $selected_tags << params[:q]

    @issues_tags_num = Issue.tag_counts.size
    @projects_tags_num = Project.tag_counts.size
    @users_tags_num = User.tag_counts.size

    @issues_results = get_issues_by_tag($selected_tags)
    @projects_results = get_projects_by_tag($selected_tags)
    @users_results = get_users_by_tag($selected_tags)

    @obj_id = params[:obj_id]
    case params[:object_flag]
    when '1' then
      @obj = User.find_by_id(@obj_id)
    when '2' then
      @obj = Project.find_by_id(@obj_id)
    when '3' then
      @obj = Issue.find_by_id(@obj_id)
    when '4' then
      @obj = Bid.find_by_id(@obj_id)
    else
    @obj = nil
    end

    unless @obj.nil?
      @tags = @obj.tag_list
      $selected_tags.each do |i|
        @tags.delete(i)
      end
    $related_tags = @tags
    else
    return
    end

  end

  def add_tag
    @tag = params[:tag]
    $selected_tags << @tag
    $related_tags.delete(@tag)

    @issues_results = get_issues_by_tag($selected_tags)
    @projects_results = get_projects_by_tag($selected_tags)
    @users_results = get_users_by_tag($selected_tags)
  end

  def delete_tag
    @tag = params[:tag]
    $related_tags << @tag
    $selected_tags.delete(@tag)

    @issues_results = get_issues_by_tag($selected_tags)
    @projects_results = get_projects_by_tag($selected_tags)
    @users_results = get_users_by_tag($selected_tags)
  end

  def count(name)
  end

  def show

  end

end
