# encoding: utf-8
class SharesController < ApplicationController
  before_filter :require_login, :except => [:index]
  before_filter :require_admin, :only => [:index]
  # GET /shares
  # GET /shares.json
  def index
    @shares = Share.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @shares }
    end
  end

  # GET /shares/1
  # GET /shares/1.json
  def show
    @share = Share.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @share }
    end
  end

  # GET /shares/new
  # GET /shares/new.json
  def new
    @share = Share.new
    @user = User.current
    @ps = @user.projects.all
    
    projectName  = params[:projectname]
    userName     = params[:username]
    url          = params[:url]
    share_type   = params[:share_type]
    share_type   ||= 0 #默认是测试结果分享
    description  = params[:description]

    #deal params
    if share_type == 0 && !params[:description].nil?
      arr = params[:description].split(",")
      # @share.description = @arr.join(",")
      #description = "stcloud源代码测试平台:用户" << userName << "对项目" << projectName << "进行了测试。测试结果:" << "\n"
      name = User.current.login.to_s.dup
      description = name << "对项目进行了测试。测试结果:" << "\n"
      description << "总缺陷数#{arr[0]},Fault数目#{arr[1]},Rule数目#{arr[2]},Question数目#{arr[3]},Safety数目#{arr[4]}。"
    end
    
    @share[:title]       = projectName
    @share[:url]         = url
    @share[:share_type]  = share_type
    @share[:description] = description


    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @share }
    end
  end

  # GET /shares/1/edit
  def edit
    @share = Share.find(params[:id])
  end

  # POST /shares
  # POST /shares.json
  def create
    @share = Share.new(params[:share])
    if (@share.project_id.nil?)
      flash[:notice] = l(:label_x_projects) 
    end
    @share.user_id = User.current.id
    respond_to do |format|
      if @share.save
        #format.html { redirect_to @share, notice: 'Share was successfully created.' }
        format.html { render "succ", notice: 'Share was successfully created.' }
        format.json { render json: @share, status: :created, location: @share }
      else
        format.html { render action: "new" }
        format.json { render json: @share.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /shares/1
  # PUT /shares/1.json
  def update
    @share = Share.find(params[:id])
    
    @project = params[:project_id]
    @share.user_id = User.current.id
    respond_to do |format|
      if @share.update_attributes(params[:share])
        format.html { redirect_to @share, notice: 'Share was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @share.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /shares/1
  # DELETE /shares/1.json
  def destroy
    @share = Share.find(params[:id])
    @share.destroy

    respond_to do |format|
      format.html { redirect_to shares_url }
      format.json { head :no_content }
    end
  end
end