require 'mongo'

require 'json'
require 'rubygems'
require 'nokogiri'
require 'rails'

require 'imperituroard/add_functions/logger/any_functions'

class MongoVpn

  attr_accessor :mongo_vpn_ip, :mongo_vpn_port, :client_vpn, :mongo_vpn_database, :internal_vpn_func, :logger_any

  def initialize
    @mongo_vpn_database = Imperituroard::AUTOVPN_MONGO_VPN_DATABASE
    @mongo_vpn_ip = Imperituroard::AUTOVPN_MONGO_IP
    @mongo_vpn_port = Imperituroard::AUTOVPN_MONGO_VPN_PORT
    client_vpn_host = [mongo_vpn_ip + ':' + @mongo_vpn_port]
    @client_vpn = Mongo::Client.new(client_vpn_host, :database => @mongo_vpn_database)
    @logger_any = LogAddFunctions_2.new
  end

  #get data by msisdn. Validate data from collection msisdn_list
  def get_mongo_msisdn(msisdn)
    out_resp = {}
    begin
      msisdn_data = []
      collection = client_vpn[:sdp_msisdn_list]
      collection.find({'msisdn': msisdn.to_s}).each {|row|
        msisdn_data = row
      }
      if msisdn_data != [] && msisdn_data['msisdn'] != nil && msisdn_data['msisdn'] != ''
        if msisdn_data['group_id'] != [] && msisdn_data['group_id'] != nil && msisdn_data['group_id'] != ''
          if msisdn_data['profile_id'] != [] && msisdn_data['profile_id'] != nil && msisdn_data['profile_id'] != ''
            out_resp = {:code => 200, :result => 'get_mongo_msisdn: Request completed successfully', :body => msisdn_data}
          else
            out_resp = {:code => 406, :result => 'get_mongo_msisdn: invalid data. profile_id not found'}
          end
        else
          out_resp = {:code => 405, :result => 'get_mongo_msisdn: invalid data. organization_id not found'}
        end
      else
        out_resp = {:code => 404, :result => 'get_mongo_msisdn: login not found in database'}
      end
    rescue
      out_resp = {:code => 507, :result => 'get_mongo_msisdn: Unknown SDK error'}
    end
    logger_any.printer_texter(out_resp, 'debug')
    out_resp
  end

  def get_mongo_organization_id(id)
    out_resp = {}
    begin
      mongo_data = []
      collection = client_vpn[:attr_organizations]
      collection.find({'organization_id': id.to_s}).each {|row|
        mongo_data = row
      }
      if mongo_data != [] && mongo_data['organization_id'] != nil && mongo_data['organization_id'] != ''
        if mongo_data['customer_id'] != [] && mongo_data['customer_id'] != nil && mongo_data['customer_id'] != ''
            out_resp = {:code => 200, :result => 'get_mongo_organization_id: Request completed successfully', :body => mongo_data}
        else
          out_resp = {:code => 405, :result => 'get_mongo_organization_id: invalid data. customer_id not found'}
        end
      else
        out_resp = {:code => 404, :result => 'get_mongo_organization_id: organization_id not found in database'}
      end
    rescue
      out_resp = {:code => 507, :result => 'get_mongo_organization_id: Unknown SDK error'}
    end
    logger_any.printer_texter(out_resp, 'debug')
    out_resp
  end


  def get_mongo_profile_id(id)
    out_resp = {}
    begin
      mongo_data = []
      collection = client_vpn[:attr_profiles]
      collection.find({'profile_id': id.to_s}).each {|row|
        mongo_data = row
      }
      if mongo_data != [] && mongo_data['profile_id'] != nil && mongo_data['profile_id'] != ''
        if mongo_data['attribute_list'] != [] && mongo_data['attribute_list'] != nil && mongo_data['attribute_list'] != ''
          out_resp = {:code => 200, :result => 'get_mongo_profile_id: Request completed successfully', :body => mongo_data}
        else
          out_resp = {:code => 405, :result => 'get_mongo_profile_id: invalid data. customer_id not found'}
        end
      else
        out_resp = {:code => 404, :result => 'get_mongo_profile_id: organization_id not found in database'}
      end
    rescue
      out_resp = {:code => 507, :result => 'get_mongo_profile_id: Unknown SDK error'}
    end
    logger_any.printer_texter(out_resp, 'debug')
    out_resp
  end




end