class InitTemplatesController < ApplicationController
  include CacheExistence
  before_filter :authenticate_user!
  before_action :set_init_template, only: [:show, :destroy]
  load_and_authorize_resource :class => InitTemplate
  skip_load_resource :class => InitTemplate, :only => [:new, :create]

  def index
    @init_templates = current_user.organisation.init_templates.where(is_default: false, user_id: current_user.id)
  end

  def new
    @init_template = InitTemplate.new
  end

  def show
  end

  def list_init_templates
    init_template_name = params[:init_template_name]
    init_template_id = params[:init_template_id]
    ui_mode = params[:ui]
    user_init_templates = current_user.organisation.init_templates.where(user_id: current_user.id)
    all_init_templates = if ui_mode
      user_init_templates.map { |n| { id: n.id, name: n.name } }
    else
      user_init_templates.map { |n| n.json_build }
    end
    if init_template_name
      init_template = current_user.organisation.init_templates.where(name: init_template_name).first
    else
      init_template = current_user.organisation.init_templates.where(id: init_template_id).first
    end
    if init_template_name.present? || init_template_id.present?
      if init_template.present?
        render json: { data: init_template.json_build, status: 200 }
      else
        render json: { msg: "Requested Init Template not found check your init templates", status: 404 }
      end
    else
      render json: { data: { init_templates: all_init_templates }, status: 200 }
    end
  end

  def create
    @init_template = current_user.organisation.init_templates.new(init_template_params)
    @init_template.details = get_ap_uplink_details(init_template_params[:ap_mac_id])
   
    if @init_template.save
      flash[:success] = "Init Template was successfully created."
      json_data = { data: @init_template.json_build, status: 200 }
    else
      flash[:error] = "Oops! There was some problem in creating Init Template."
      json_data = { message: "Init Template is not created. Please try again.", status: 422 }
      Rails.logger.warn "#{@init_template.errors.full_messages}"
    end
   
    if request.format.json?
      render json: json_data, status: json_data[:status]
    else
      redirect_to init_templates_path
    end
  end

  def edit
  end

  def update
  end

  def destroy   
    if @init_template.destroy
      flash[:success] = "Successfully deleted InitTemplate."
      json_data = { message: "Init Template deleted successfully.", status: 200 }
    else
      flash[:error] = "Oops! There was some problem deleting Init Template."
      json_data = { message: "Init Template could not be deleted. Please try again.", status: 422 }
      Rails.logger.warn "#{@init_template.errors.full_messages || 'Init Template not found'}"
    end
   
    if request.format.json?
      render json: json_data, status: json_data[:status]
    else
      redirect_to init_templates_path
    end
  end


  private

  def current_ability
    if cache_exist?
      @current_ability ||= Ability::InitTemplateAbility.new(current_user)
    else
      @current_ability ||= Ability.new(current_user)
    end
  end

  def set_init_template
    @init_template = @current_user.organisation.init_templates.where(:id => params[:id]).first
    if @init_template.blank?
      if request.format.json?
        render json: {message: "Requested InitTemplate not found!", status: 404}, status: 404
      else
        flash[:notice] = "Requested InitTemplate not found!"
        redirect_to init_templates_path
      end
    end
  end

  def init_template_params
    params.require(:init_template).permit(:name,:template_type,:ap_mac_id).merge(user_id: current_user.id, organisation_id: current_user.organisation.id)
  end

  def get_ap_uplink_details(ap_mac_id)
    RouterInventory.where(mac_id: ap_mac_id).first.uplinks.map{|uplink| uplink.attributes.except("id","router_inventory_id","created_at","updated_at","last_mobile_status","last_seen_ip","nt","mm","cid","lac","lat","lng","imsi")}
  end
end
