class WorkLocksController < ApplicationController
  def index
    list
    render :action => 'list'
  end
  
  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :destroy, :create, :update ],
  :redirect_to => { :action => :list }
  
  def list
    @work_lock_pages, @work_locks = paginate :work_locks, :per_page => 10
  end
  
  def show
    @work_lock = WorkLock.find(params[:id])
  end
  
  def new
    @work_lock = WorkLock.new
  end
  
  def create
    @work_lock = WorkLock.new(params[:work_lock])
    if @work_lock.save
      flash[:notice] = 'WorkLock was successfully created.'
      redirect_to :action => 'list'
    else
      render :action => 'new'
    end
  end
  
  def edit
    @work_lock = WorkLock.find(params[:id])
  end
  
  def update
    @work_lock = WorkLock.find(params[:id])
    if @work_lock.update_attributes(params[:work_lock])
      flash[:notice] = 'WorkLock was successfully updated.'
      redirect_to :action => 'show', :id => @work_lock
    else
      render :action => 'edit'
    end
  end
  
  def destroy
    WorkLock.find(params[:id]).destroy
    redirect_to :action => 'list'
  end
  
  def lock
    @year = (params[:year] && params[:year].to_i) || Date.today.year
    @week = (params[:week] && params[:week].to_i) || Date.today.cweek
    first_date = Date.commercial(@year, @week, 1)
    last_date = first_date + 6
    @lock = WorkLock.find(:first, :conditions => ['user_id = ? AND start_on <= ? AND end_on >= ?', current_user.id, first_date, last_date])
    raise "Already locked" if @lock
    WorkLock.create! :user_id => current_user.id, :start_on => first_date, :end_on => last_date
    work_account_subscribers = Work.works_for_week(@year, @week).flatten.compact.map {|w| w.work_account}.uniq.map {|wa| wa.work_lock_subscribers}.flatten.uniq
    user_subscribers = current_user.work_lock_subscribers
    notify_users = (work_account_subscribers + user_subscribers).uniq
    notify_users.each do |user|
      week_url = url_for :controller => 'works', :action => :weekly_work_sheet_by_work_account, :year => @year, :week => @week, :user_id => current_user.id
      spreadsheet_url = url_for :controller => 'works', :action => :timeliste, :year => @year, :week => @week, :user_id => current_user.id
      WorkLockNotify.deliver_lock(user, current_user, @week, week_url, spreadsheet_url)
    end
    back_or_redirect_to :controller => 'works', :action => :weekly_work_sheet, :year => @year, :week => @week
  end
  
  def unlock
    get_lock_from_params
    raise "Not locked" unless @lock
    raise "Unable to destroy" unless @lock.destroy
    back_or_redirect_to :controller => 'works', :action => :weekly_work_sheet, :year => @year, :week => @week
  end
  
  private
  
  def get_lock_from_params
    @year = (params[:year] && params[:year].to_i) || Date.today.year
    @week = (params[:week] && params[:week].to_i) || Date.today.cweek
    @first_date = Date.commercial(@year, @week, 1)
    @last_date = @first_date + 6
    @lock = WorkLock.find(:first, :conditions => ['user_id = ? AND start_on <= ? AND end_on >= ?', current_user.id, @first_date, @last_date])
  end
  
end