module IdCard
  class SetupController < ApplicationController
    before_action :set_employer_and_setup

    # View Methods
    # def edit
    #   @employer = Employer.find_by(slug: params[:employer_id])
    #   if @employer.id_card_enabled?
    #     @setup = @employer.id_card_setup
    #   else
    #     @setup = @employer.create_id_card_setup
    #   end
    #   render :edit
    # end

    # def update
    #   setup_params = IdCard::Setup.permitted_params(params)
    #   @setup = IdCard::Setup.find(params[:id])

    #   if @setup.update(setup_params)
    #     puts "sucess"
    #     redirect_to employer_path(@setup.employer.slug), notice: 'ID Card Setup was successfully updated.'
    #   else
    #     puts "fail"
    #     render :edit, status: :unprocessable_entity
    #   end

    # end

    def index
      provider_defaults = IdCard::ProviderSection.defaults
      @provider_options = provider_defaults.map { |p| ["Default #{p.title}", p.id] }.concat(provider_defaults.map { |p| ["New #{p.title}", "new|#{p.id}"] })
      if @setup.provider_section_id.present? && provider_defaults.map(&:id).exclude?(@setup.provider_section_id)
         @provider_options.insert(0, ["#{@employer.name} Custom", @setup.provider_section_id])
      end
      @rx_options = IdCard::RxSection.all
      @fairos_rx_id = IdCard::RxSection.find_by(title: "FairosRx").id
      render :index
    end

    def update
      if params[:id_card_setup]["provider_section_id"].include?("new|")
        new_provider_section_params = IdCard::ProviderSection.permitted_params(params)
        new_provider_section = IdCard::ProviderSection.create(new_provider_section_params)
        params[:id_card_setup]["provider_section_id"] = new_provider_section.id
      end
      setup_params = IdCard::Setup.permitted_params(params)
      if @setup.update(setup_params)
        puts "sucess"
        redirect_to employer_path(@employer.slug), notice: 'ID Card Setup was successfully updated.'
      else
        puts "fail"
        render :index, status: :unprocessable_entity
      end
    end

    def plans
      @plan_templates = IdCard::Plan.templates
      render :plans
    end

    def update_plans
      plans_params = IdCard::Plan.permitted_params(params)
      if @setup.update(plans_params)
        puts "sucess"
        redirect_to employer_path(@setup.employer.slug), notice: 'ID Card Plans successfully updated.'
      else
        puts "fail"
        render :plans, status: :unprocessable_entity
      end

    end

    def field_exceptions
      render :field_exceptions
    end

    def update_field_exceptions
      field_exceptions_params = IdCard::FieldException.permitted_params(params)
      if @setup.update(field_exceptions_params)
        puts "sucess"
        redirect_to employer_path(@setup.employer.slug), notice: 'ID Card Exceptions successfully updated.'
      else
        puts "fail"
        render :field_exceptions, status: :unprocessable_entity
      end

    end

    def update_active_status
      @setup.active = !@setup.active
      if @setup.save
        puts "sucess"
        redirect_to employer_path(@setup.employer.slug), notice: 'ID Card Active Status successfully updated.'
      else
        puts "fail"
        render :show, 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

    private

    def set_employer_and_setup
      @employer = Employer.find_by(slug: params[:employer_id])
      if @employer.id_card_setup.present?
        @setup = @employer.id_card_setup
      else
        @setup = @employer.create_id_card_setup
      end
    end

    # 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_field_exceptions_params
    #   params.require(:employer_setup_network_field_exceptions_form).permit(
    #     network_field_exceptions: [:network_logo, field_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_field_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_field_exceptions: [:network_logo, field_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
end
