class EmployerSetupController < ApplicationController
  def new
    # session.delete(:employer_setup_data)
    # session.clear()
    # puts "Start"
    # puts session[:employer_setup_data]
    if session[:employer_setup_data].blank?
      @process = EmployerSetupProcess.create(status: "form")
      session[:employer_setup_data] = { "employer_setup_process_id" => @process.id }

    end
    puts "-S--------------------------------------"
    puts session[:employer_setup_data]
    # session[:employer_setup_data]['current_step'] = 'plans'

    @top_form = EmployerSetupForm.new(session[:employer_setup_data])
    if @top_form.current_step != 'summary'
      @form = form_for_step
    else
      @form = @top_form
    end

    # case @top_form.current_step
    # when 'general_information'
    #   @form = EmployerSetupGeneralInformationForm.new(session[:employer_setup_data]&.dig('general_information_data'))
    # when 'plans'
    #   @form = EmployerSetupPlansForm.new(session[:employer_setup_data]&.dig('plans_data'))
    # when 'network_exceptions'
    #   @form = EmployerSetupNetworkExceptionsForm.new(session[:employer_setup_data]&.dig('network_exceptions_data'))
    # when 'summary'
    #   @form = @top_form
    # end
    render @top_form.current_step_view
  end

  def create
    # puts session[:employer_setup_data]
    @top_form = EmployerSetupForm.new(session[:employer_setup_data])
    if @top_form.current_step != 'summary'
      if process_step(@top_form.current_step)
        session[:employer_setup_data]['current_step'] = @top_form.next_step
        puts "---------------------------------------"
        puts "pass"
        puts "next - #{@top_form.next_step}"
        puts session[:employer_setup_data]
        puts "---------------------------------------"
        redirect_to new_employer_setup_path
      else
        puts "fail"
        render @top_form.current_step_view
      end
    else
      if @top_form.save
        session.delete(:employer_setup_data)
        redirect_to root_path, notice: "Employer setup successfully!"
      else
        render @top_form.current_step_view
      end
    end

    # case @top_form.current_step
    # when 'general_information'
    #   @form = EmployerSetupGeneralInformationForm.new(general_information_params)
    #   if @form.valid?
    #     session[:employer_setup_data]['general_information_data'] = general_information_params
    #     session[:employer_setup_data]['current_step'] = @top_form.next_step
    #     redirect_to new_employer_setup_path
    #   else
    #     render @top_form.current_step_view
    #   end
    # when 'plans'
    #   @form = EmployerSetupPlansForm.new(plans_params)
    #   if @form.valid?
    #     session[:employer_setup_data]['plans_data'] = plans_params
    #     session[:employer_setup_data]['current_step'] = @top_form.next_step
    #     redirect_to new_employer_setup_path
    #   else
    #     render @top_form.current_step_view
    #   end
    # when 'network_exceptions'
    #   @form = EmployerSetupNetworkExceptionsForm.new(network_exceptions_params)
    #   if @form.valid?
    #     session[:employer_setup_data]['network_exceptions_data'] = network_exceptions_params
    #     session[:employer_setup_data]['current_step'] = @top_form.next_step
    #     redirect_to new_employer_setup_path
    #   else
    #     render @top_form.current_step_view
    #   end
    # when 'summary'
    #   @form = EmployerSetupForm.new(session[:employer_setup_data])
    #   if @form.save
    #     session.delete(:employer_setup_data)
    #     redirect_to root_path, notice: "Employer setup successfully!"
    #   else
    #     render @top_form.current_step_view
    #   end
    # end
  end

  def update
    @form = RegistrationForm.new(registration_params)

    if params[:back_button]
      @form.current_step = @form.previous_step
    elsif params[:skip_newsletter]
      @form.current_step = @form.next_step # Skip newsletter step
    end

    render :new
  end

  private

  # def general_information_params
  #   params.require(:employer_setup_general_information_form).permit(
  #     :name,
  #     :employer_logo,
  #     :group_number,
  #     :dental,
  #     :pl_plan_key,
  #     :effect_date,
  #     :number_of_plans,
  #     :network,
  #     :number_of_additional_network_logos
  #   )
  # end

  # def plans_params
  #   params.require(:employer_setup_plans_form).permit(
  #     plans: permited_plans_keys,
  #     benefit_descs: benefit_sequence_keys
  #   )
  # end

  # def network_exceptions_params
  #   params.require(:employer_setup_network_exceptions_form).permit(
  #     network_exceptions: [:network_logo, exceptions: [:type, :value]],
  #   )
  # end

  def form_for_step
    step_name = @top_form.current_step
    form_method = "EmployerSetup#{step_name.camelize}Form".constantize
    # puts "/////\\\\\\||||||"
    # puts session[:employer_setup_data]
    # puts session[:employer_setup_data]['employer_setup_process_id']
    puts form_method
    form_method.new(session[:employer_setup_data]['employer_setup_process_id'])
  end

  def process_step(step_name)
    @form_method = "EmployerSetup#{step_name.camelize}Form".constantize
    session_data_name = "#{step_name}_data"
    # puts "1--------------params----"
    # puts params
    # puts "8--------------session----"
    # puts session[:employer_setup_data]
    employer_setup_process_id = session[:employer_setup_data]['employer_setup_process_id']
    # puts session[:employer_setup_data]
    puts "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
    puts params
    @form = @form_method.new(employer_setup_process_id, params)
    if @form.pl_plan_key.blank?
      @form.pl_plan_key = session[:employer_setup_data]['pl_plan_key']
    end
    if @form.valid? && @form.save
      pl_plan_key = @top_form.pl_plan_key || @form.pl_plan_key
      # session[:employer_setup_data].merge!({current_step: step_name, pl_plan_key: pl_plan_key})
      # form_fields = @form.attributes.merge!(global_params(step_name))
      # session[:employer_setup_data][session_data_name] = form_fields
      # session[:employer_setup_data].merge!(global_params(step_name))
      # puts session[:employer_setup_data][session_data_name]
      true
    else
      false
    end
  end

  def step_params(step_name)
    form_name_sym = "employer_setup_#{step_name}_form".to_sym
    params.require(form_name_sym).permit(@form_method.permitted_params)
  end

  def global_params(step_name)
    form_name_sym = "employer_setup_#{step_name}_form".to_sym
    params.require(form_name_sym).permit(EmployerSetupForm.permitted_params)
  end

  # def process_step(step_name)
  #   form_name = "employer_setup_#{step_name}_form".camelize.constantize
  #   form_params_name = "#{step_name}_params".to_sym
  #   allowed_params = [:general_information_params, :plans_params, :network_exceptions_params]
  #   if allowed_params.include?(form_params_name)
  #     form_params = send(form_params_name)
  #     @form = form_name.new(form_params)
  #     if @form.valid?
  #       session[:employer_setup_data]["#{step_name}_data"] = form_params
  #       true
  #     else
  #       false
  #     end
  #   end
  #   false
  # end

  # def employer_setup_params
  #   params.require(:employer_setup_form).permit(
  #     :current_step,
  #     :name,
  #     :employer_logo,
  #     :group_number,
  #     :pl_plan_key,
  #     :effect_date,
  #     :number_of_plans,
  #     :network,
  #     :number_of_additional_network_logos,
  #     network_exceptions: [:network_logo, exceptions: [:type, :value]],
  #     plans: permited_plans_keys,
  #     benefit_descs: benefit_sequence_keys
  #   )
  # end

  # def benefit_sequence_keys
  #   (1..14).map { |i| i.to_s.to_sym }
  # end

  # def permited_plans_keys
  #   benefit_sequence_keys.push(:plan_id)
  # end

  # def plans_params
  #   plans_keys = params[:plans]&.keys || []

  #   plans_keys.each_with_object({}) do |key, hash|
  #     if key == 'benefit_descs' || key.match?(/^plan_\d$/)
  #       hash[key.to_sym] = permited_plan_param_list
  #     end
  #   end
  # end

  # def permited_plan_param_list
  #   (1..14).map { |i| i.to_s.to_sym }.push(:plan_id)
  # end
end
