class Panel::Survey::FrontEnd::SurveysController < OrbitWidgetController
  helper ApplicationHelper
  
  def initialize
    super
    @app_title = 'survey'
  end

  def index
  end

  def show
    @survey = ::Survey.find params[:id]
    if @survey.redirect_mode
      @redirect_url = @survey.redirect_url
      render :redirect_standalone
    else
      redirect_standalone panel_survey_front_end_survey_path(params[:id], :standalone => 'true') do
        @questions = @survey.survey_questions.all
        if @survey.jump_mode
          @jumpable_questions = @questions.all.select{ |q| q.jumpable? && q.can_set_jump? }

          @jump_tos_map = @jumpable_questions.map(&:get_jump_tos).inject(:merge)
        end
      end
    end
  end

  def update
    @survey = ::Survey.find params[:id]
    answer = params[:answer]

    @answer_model = @survey.survey_answers.new
    @survey.survey_questions.each do |question|
      qid = question.id.to_s
      if question.is_required && answer[qid].blank? && !@survey.jump_mode #&& (! @survey.jump_mode || ( @survey.jump_mode && question.jumpable? ) )
        @answer_model.errors.add question.title, t('survey_question.required_error')
      else
        case question.type
        when ::SurveyQuestion::Radio, ::SurveyQuestion::Select
          if question.custom_option && answer[qid] == 'custom_option'
            @answer_model[qid] = answer[qid + '_custom_option']
          else
            if answer[qid]
              @answer_model[qid] = question.survey_question_options.find(answer[qid]).name
            end
          end
        when ::SurveyQuestion::Check
          @answer_model[qid] = []
          if answer[qid]
            answer[qid].each do |oid, value|
              if value.to_i != 0
                if question.custom_option && oid == 'custom_option'
                  @answer_model[qid].push answer[qid + '_custom_option']
                else
                  @answer_model[qid].push question.survey_question_options.find(oid).name
                end
              end
            end
          end
        when ::SurveyQuestion::Radiogroup
          @answer_model[qid] = {}
          options = Hash[question.survey_question_options.collect{|o| [ o.id.to_s, o.name ] }]
          radiogroups = Hash[question.survey_question_radiogroups.collect{|rg| [ rg.id.to_s, rg.name] }]
          if answer[qid]
            answer[qid].each do |oid, value|
              unless value.blank?
                @answer_model[qid][oid] = radiogroups[value]
              else
                if question.is_required
                  @answer_model.errors.add question.title, t('survey_question.required_error')
                end
              end
            end
          end
        when ::SurveyQuestion::Oneline, ::SurveyQuestion::Multiline
          @answer_model[qid] = answer[qid]
        end
      end
    end
    if @answer_model.errors.empty?
      @answer_model.save!
      render :answer_success
    else
      @survey_answer_error = @answer_model.errors.full_messages.join(',')
      render :answer_error
    end
  end

  def result
    redirect_standalone result_panel_survey_front_end_survey_path(params[:id], :standalone => 'true') do
      @survey = ::Survey.find params[:id]
      @survey.result_type = ::Survey::ResultChart if params[:force_chart]
      case @survey.result_type
      when ::Survey::ResultChart
        @chart_data, @survey_questions, @survey_answers = @survey.generate_chart_data
      end
    end
  end

  def redirect_standalone url
    unless params[:standalone] == 'true'
      @redirect_url = url
      render :redirect_standalone
    else
      module_app = ModuleApp.first(:conditions => {:key => 'survey'})
      @item = Item.where(module_app_id: module_app.id, app_frontend_url: 'default_widget').first
      yield
      render :layout => 'standalone'
    end
  end

end