class EmployersController < ApplicationController

  # View Methods
  def index 
    @uninitialized = Employer.in_automation_initilization
    @with_active_id_card_setup = Employer.with_active_id_card_setup
    @deactivated = Employer.deactivated
  end

  def show
    @employer = Employer.find_by(slug: params[:id])
  end

  def new
    @employer = Employer.new
    render :new
  end

  # def import_old
  #   word_doc = params[:employer][:import_from_word]
  #   @plan_templates = IdCardBenefitsTemplate.where.not(title: "BLANK")
  #   if word_doc.present? && word_doc.is_a?(ActionDispatch::Http::UploadedFile)
  #     @employer = WordDocProcessor.new(word_doc.tempfile).call
  #   else
  #     @employer = Employer.new
  #     @employer.build_plan_with_default_benefits
  #   end
  #   render :new
  # end

  def create
    employer_params = Employer.permitted_params(params)
    puts "---Params---"
    puts employer_params
    # post_image_processing_params = process_logos(employer_setup_process_params)
    @employer = Employer.new(employer_params)
    if @employer.save
      # update_logos_with_employer_setup_information()
      redirect_to employer_path(@employer.slug), notice: 'Employer Saved'
    else
      render :new
    end
  end

  def edit
    @employer = Employer.find_by(slug: params[:id])
    render :edit
  end

  def update
    puts "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
    employer_params = Employer.permitted_params(params)
    @employer = Employer.find(params[:id])

    if @employer.update(employer_params)
      puts "sucess"
      redirect_to employer_path(@employer.slug), notice: 'Employer was successfully updated.'
    else
      puts "fail"
      render :edit, status: :unprocessable_entity
    end

  end

  def destroy
    # @resource = Resource.find(params[:id])
    # @resource.destroy
    # redirect_to resources_url, notice: 'Resource was successfully destroyed.'
  end


  # API Methods

  def refresh_employer_information
    @employer = Employer.find(params[:id])
    @employer.sync_with_vhcs
    redirect_to employer_path(@employer.slug)
  end

  def refresh_employer_members_information
    @employer = Employer.find(params[:id])
    @employer.sync_members_with_vhcs
    redirect_to employer_path(@employer.slug)
  end

  def import
    word_doc = params[:employer][:import_from_word]
    if word_doc.present? && word_doc.is_a?(ActionDispatch::Http::UploadedFile)
      @employer = BenefitsWordDocService::WordDocProcessor.new(word_doc.tempfile).call
      @employer.save
      redirect_to employer_path(@employer.slug), notice: 'Employer Imported'
    else
      @employer = Employer.new
      render :new
    end
  end

  private

  # def process_logos(employer_setup_process_params)
  #   @uploaded_logos = []
  #   employer_logo = employer_setup_process_params["employer_logo"]
  #   if employer_logo.present? && employer_logo.is_a?(ActionDispatch::Http::UploadedFile)
  #     filename = employer_logo.original_filename
  #     binary_data = employer_logo.read
  #     meme_type = Marcel::MimeType.for(employer_logo)

  #     CardLogoFile.create(
  #       filename: filename,
  #       image_data: binary_data,
  #       content_type: meme_type,
  #       logo_type: "employer"
  #     )

  #     @uploaded_logos.push(filename)
  #     employer_setup_process_params["employer_logo"] = filename
  #   end

  #   network_logos = employer_setup_process_params["alternate_network_logos_attributes"]
  #   if network_logos.present?
  #     network_logos.each do |alt|

  #       network_logo = alt.last["network_logo"]
  #       if network_logo.present? && network_logo.is_a?(ActionDispatch::Http::UploadedFile)
  #         filename = network_logo.original_filename
  #         binary_data = network_logo.read
  #         meme_type = Marcel::MimeType.for(network_logo)

  #         CardLogoFile.create(
  #           filename: filename,
  #           image_data: binary_data,
  #           content_type: meme_type,
  #           logo_type: "network"
  #         )

  #         @uploaded_logos.push(filename)
  #       end
  #       alt.last["network_logo"] = @uploaded_logos.last
  #     end
  #   end
  #   employer_setup_process_params
  # end

  # def update_logos_with_employer_setup_information()
  #   @uploaded_logos.each do |logo|
  #     logo_file = CardLogoFile.find_by(filename: logo)
  #     if logo_file.present? && @employer.present?
  #       logo_file.employer_setup_process = @employer
  #       logo_file.save
  #     end
  #   end

  # end

  # 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
