class StepsController < ApplicationController
  respond_to :html, :json
  load_and_authorize_resource :workflow, through: :current_organization
  load_and_authorize_resource :step, through: :workflow
  
  before_filter :new_breadcrumbs, only: [:new, :create]
  before_filter :show_breadcrumbs, only: [:edit, :show, :update]

  # GET
  def index
    @breadcrumbs = [["#{t(:steps)}"]]
    @steps = @steps.page(params[:page]).decorate
  end

  # GET
  def new
  end

  # GET
  def show 
  end

  # GET
  def edit
    init_definition   
  end

  # POST
  def create
    @step = Step.new(step_params)
    @workflow = current_organization.workflows.find(params[:workflow_id])
    @step = @workflow.steps.build step_params
    @step.organization = current_organization
    @step.user = current_user
    
    definition_creator = Services::DefinitionCreator.new(@step, step_params)
    respond_to do |format|
      if definition_creator.save
        format.html { redirect_to edit_workflow_url(@workflow), notice: "#{t(:step)} #{t(:was_successfully_created)}" }
      else
        flash.now[:danger] = "#{t(:failed_to_create)} #{t(:step)}"
        format.html { render action: 'new' }
      end
    end
  end

  # PATCH/PUT
  def update
    definition_creator = Services::DefinitionCreator.new(@step, step_params)
    respond_to do |format|
      if definition_creator.update
        format.html { redirect_to edit_workflow_url(@step.workflow_id), notice: "#{t(:step)} #{t(:was_successfully_updated)}" }
      else
        flash.now[:danger] = "#{t(:failed_to_update)} #{t(:step)}"
        format.html { render action: 'show' }
      end
    end
  end

  # DELETE
  def destroy
    @step.destroy
    respond_to do |format|
      format.html { redirect_to edit_workflow_url(@step.workflow_id), notice: "#{t(:step)} #{t(:was_successfully_deleted)}" }
    end
  end


  def state_change
    authorize! :manage, @step
    if @step.state_change(params[:event], params[:state_change_at])
      msg_h = { notice: t(:success) }
    else
      msg_h = { alert: t(:fail) }
    end
    redirect_to workflow_steps_url, msg_h
  end
  
  private

  # Never trust parameters from the scary internet, only allow the white list through.
  def step_params
    params.require(:step).permit(Step.accessible_attributes.to_a)
  end

  def new_breadcrumbs
    @breadcrumbs = [["#{t(:workflows)}", workflows_path],[@step.workflow.name, edit_workflow_path(@workflow)], ["#{t(:new)} #{t(:step)}"]]
  end

  def show_breadcrumbs
    @breadcrumbs = [["#{t(:workflows)}", workflows_path],[@step.workflow.name, edit_workflow_path(@workflow)], [@step.step_number]]
  end

  def init_definition
    case @step.definition_type
    when 'Decision'
      @step.decision_name = @step.definition.name
      @step.decision_description = @step.definition.description
    when 'Discussion'
      @step.discussion_name = @step.definition.name
      @step.discussion_description = @step.definition.description
    when 'Task'
        @step.task_name = @step.definition.name
        @step.task_description = @step.definition.description
    else
      Rail.logger.info "-->No definition in step_controller"
	end 
  end
end
