class ShortenedUrlsController < ApplicationController

  skip_before_action :login_required, :check_session, only: :get_shorten_url
  # layout :nil

  def remote_shorten_url
    json_message={}
    endpoint_url = Pixways.get_pixtech_param_value(session[:pixtech],'share_url_domain')
    if endpoint_url.blank?
      json_message[:error_msg] = 'URL for shortener call is missing'
    else
      token = User.find_by(login: Pixways.get_pixtech_param_value(session[:pixtech],'login_guest_vitrine') ).persistence_token rescue ''
      if token.blank?
        json_message[:error_msg] = 'PixTech login_guest_vitrine info are missing'
      else
        if params[:url_to_shorten].blank?
          json_message[:error_msg] = 'URL to shorten is missing'
        else
          begin
            result_call = RestClient.post("#{endpoint_url}/get_shorten_url", { token: token, url_to_shorten: params[:url_to_shorten] }.to_json, content_type: :json, accept: :json)
            if result_call.blank?
              json_message[:error_msg] = t'action_failed'
            else
              if Pixways.valid_json(result_call.body)
                result_json = JSON.parse(result_call.body)
                if result_json["error_msg"].blank?
                  json_message[:shortened_url] = result_json["shortened_url"]
                else
                  json_message[:error_msg] = result_json["error_msg"]
                end
              else
                json_message[:error_msg] = "Erreur Json : #{result_call.body}"
              end
            end
          rescue => e
            json_message[:error_msg] = e.message
          end
        end
      end
    end

    respond_to do |format|
      format.json { render json: json_message.to_json }
    end
  end

  def get_shorten_url
    json_message={}
    if params[:token].blank?
      json_message[:error_msg] = 'Authentication failed : Token missing'
    else
      user = User.where(persistence_token: params[:token], status: 'active').first
      if user.nil?
        json_message[:error_msg] = 'Authentication failed : No active user with the given token'
      else
        if params[:url_to_shorten].blank?
          json_message[:error_msg] = 'URL to shorten is missing'
        else
          begin
            # UserSession::check_user()
            new_shortened_url = Shortener::ShortenedUrl.generate(URI.encode(params[:url_to_shorten]), owner: user)
            json_message[:shortened_url] = URI::join(root_url,"/#{Shortener.subdomain}/",new_shortened_url.unique_key).to_s
          rescue => e
            json_message[:error_msg] = e.message
          end
        end
      end
    end
    respond_to do |format|
      format.json { render json: json_message.to_json }
    end
  end


  def new
    json_message={}
    if params[:url_to_shorten].blank?
      json_message[:error_msg] = 'URL parameter is missing'
    else
      begin
        # UserSession::check_user()
        new_shortened_url = Shortener::ShortenedUrl.generate(URI.encode(params[:url_to_shorten]))
        json_message[:shortened_url] = URI::join(root_url,"/#{Shortener.subdomain}/",new_shortened_url.unique_key).to_s
      rescue => e
        json_message[:error_msg] = e.message
      end
    end
    respond_to do |format|
      format.json { render json: json_message.to_json }
    end

  end

end