# Redmine - project management software
# Copyright (C) 2006-2013  Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

class WelcomeController < ApplicationController
  include ApplicationHelper
  include WelcomeHelper
  helper :project_score
  caches_action :robots
  before_filter :find_first_page, :only => [:index]
  # before_filter :fake, :only => [:index, :course]
  before_filter :entry_select, :only => [:index]
 
  def index
    if @first_page.nil? ||  @first_page.sort_type.nil?
      @projects = find_miracle_project(10, 3,"score desc")
    else
      case @first_page.sort_type
        when 0
          @projects = find_miracle_project(10, 3,"created_on desc")
          #@projects =  @projects_all.order("created_on desc")
        when 1
          @projects = find_miracle_project(10, 3,"score desc")
          #@projects = @projects_all.order("grade desc")
        when 2
          @projects = find_miracle_project(10, 3,"watchers_count desc")
          #@projects = @projects_all.order("watchers_count desc")

        #gcm
        #when '3'
        #@projects=desc_sort_course_by_avtivity(@project_activity_count_array,@project_all_array)
        #  @projects=handle_project @projects_all,@project_activity_count
        #  @s_type = 3
        #  @projects =  @projects[@project_pages.offset, @project_pages.per_page]

        else
          @projects = @projects_all.order("score desc")
      end
    end

  end

  def robots
    @projects = Project.all_public.active
    render :layout => false, :content_type => 'text/plain'
  end
  
  def course
    @course_page = FirstPage.find_by_page_type('course')
    @school_id = params[:school_id] || User.current.user_extensions.school.try(:id)
    @logoLink ||= logolink()

    ##3-8月份为查找春季课程，9-2月份为查找秋季课程
    #month_now = Time.now.strftime("%m").to_i
    #year_now = Time.new.strftime("%Y").to_i
    #(month_now >= 3 && month_now < 9) ? course_term = l(:label_spring) : course_term = l(:label_autumn)
    ##year_now -= 1 if year_now < 3
    #@school_id.nil? ? @cur_school_course = [] : @cur_school_course = find_miracle_course(10,7,@school_id, year_now, course_term)
    ##未登录或者当前学校未开设课程
    #if @cur_school_course.empty?
    #  @has_course = false
    #  User.current.logged? ? course_count = 9 : course_count = 10
    #  @cur_school_course += find_all_new_hot_course(course_count, @school_id, year_now, course_term)
    #  while @cur_school_course.count < 9 do
    #    if course_term == l(:label_spring)
    #      course_term = l(:label_autumn)
    #      year_now -= 1
    #    else
    #      course_term = l(:label_spring)
    #    end
    #    @cur_school_course += find_all_new_hot_course((10-@cur_school_course.count), nil, year_now, course_term)
    #  end
    #else
    #  if @cur_school_course.count < 9
    #    @has_course = false
    #    @cur_school_course += find_all_new_hot_course(9-@cur_school_course.count, @school_id, year_now, course_term)
    #    if @cur_school_course.count < 9
    #      if course_term == l(:label_spring)
    #        course_term = l(:label_autumn)
    #        year_now -= 1
    #      else
    #        course_term = l(:label_spring)
    #      end
    #      @cur_school_course += find_all_new_hot_course(9-@cur_school_course.count, nil, year_now, course_term)
    #    end
    #  else
    #    @has_course = true
    #  end
    #end
  end

  def logolink()
    @course_page = FirstPage.find_by_page_type('course')
    logo = get_avatar?(@course_page)
    id = params[:school_id]
    logo_link = ""
    if id.nil? && (User.current.user_extensions.nil? || User.current.user_extensions.school.nil?)
      if logo
        logo_link = url_to_avatar(@course_page)
      else
        logo_link = '/images/transparent.png'
      end

    else
      if id == "0"
        if logo
          logo_link = url_to_avatar(@course_page)
        else
          logo_link = '/images/transparent.png'
        end
      else
        if id.nil?
          if School.find(User.current.user_extensions.school.id).logo_link.nil?
            logo_link = '/images/transparent.png'
          else
            logo_link = School.find(User.current.user_extensions.school.id).logo_link
          end
        else
          logo_link = School.find(id).logo_link

        end
      end
    end
    return logo_link
  end
  
  
  def contest
    @contest_page = FirstPage.find_by_page_type('contest')
    @contest_notifications = Contestnotification.order("created_at desc").limit(5)
  end

  def search
    search_condition = params[:q] 
    search_type = params[:search_type].to_sym unless search_condition.blank?
    search_by = params[:search_by]

    if search_type.nil? && params[:contests_search] && params[:name] != ""
      search_type  = :contests
      search_condition = params[:name]
    end

    respond_to do |format|
      format.html{
        case search_type
        when :projects
          redirect_to projects_search_url(:name => search_condition,
                                            :project_type => Project::ProjectType_project)
        when :courses
          redirect_to courses_search_url(:name => search_condition)
        when :contests
          redirect_to contests_url(:name => search_condition)
        when :users
          redirect_to users_search_url(:name => search_condition,:search_by => search_by)
        when :users_teacher
          redirect_to users_search_url(:name => search_condition, :search_by => search_by, :role => :teacher)
        when :users_student
          redirect_to users_search_url(:name => search_condition, :search_by => search_by, :role => :student)
        else
          #redirect_to home_path, :alert => l(:label_sumbit_empty)
        (redirect_to home_url, :notice => l(:label_sumbit_empty);return) #if params[:name].blank?
        end
      }
    end
  end

  private
  # 判断网站的入口，是课程 course 则跳过index去渲染 course 方法
  def entry_select
    url = request.original_url.gsub('/','')
    if url.include?(Setting.host_course.gsub('/',''))
      if @first_page.show_course == 1
        course
        render :course
      else
        render_404
      end

      return 0
    elsif url.include?(Setting.host_contest.gsub('/',''))
      if @first_page.show_contest == 1
        contest
        render :contest
      else
        render_404
      end

      return 0
    elsif url.include?(Setting.host_user.gsub('/',''))
      #redirect_to(:controller => "users", :action => "index")
    end
      
    
  end

  # def render(*args)
  #   _fake if @fake_filter
  #   super
  # end

  # private

  # def fake
  #   @fake_filter = true
  # end
  
  # # 骗子方法
  # def _fake
  #   instance_variables.map { |variable|
  #     if variable.to_s =~ /Count$/
  #       self.instance_variable_set(variable.to_sym,
  #          ("1" + (self.instance_variable_get(variable.to_sym).to_s)).to_i)
  #     end
  #   }
  # end

end
