class Desktopapp::Otheraccounts < Sinatra::Base
	require 'open-uri'
  	require 'rexml/document'
  	require 'net/http' 
  	require 'net/https'
 # 	require 'twitter'
  	include REXML


  	get '/desktop/save_account_info' do
	  	@dowhat = params['dowhat']
	    @email = params['email']
	    @password = params['password']
	    @account = params['account']
	    case @dowhat
	    when "new"
	      OtherAccount.create(user_id: current_user.id, email: @email, encrypted_password: @password, type: @account)
	    when "edit"
	      @otheraccount = OtherAccount.where(:type.all => [@account],:user_id.all => [current_user.id]) rescue nil
	      @otheraccount.first.update_attributes(:email => @email, :encrypted_password => @password)
	    when "delete"
	      @otheraccount = OtherAccount.where(:type.all => [@account], :user_id.all => [current_user.id]) rescue nil
	      @otheraccount.destroy_all
	    end
	    a = Array.new
	    a << {"success"=>"true"}
	    a.to_json
  	end

  	get '/desktop/forgmail' do
  		 @gmailaccount = OtherAccount.where(:type.all => ["gmail"],:user_id.all => [current_user.id]) rescue nil
  			
	    # if @gmailaccount.first != nil
	    #   @decrypted_password = @gmailaccount.first.encrypted_password.decrypt 
	    #   @email = @gmailaccount.first.email
	    #   url = URI.parse("https://mail.google.com/mail/feed/atom")
	    #   #url = URI.parse("https://www.google.com/m8/feeds/contacts/default/full")
	    #   req = Net::HTTP::Get.new(url.path)
	    #   req.basic_auth @email, @decrypted_password
	    #   req.content_type = 'text/xml'

	    #   http = Net::HTTP.new(url.host, url.port)
	    #   http.use_ssl = true
	    #   response = http.start { |http| http.request(req) }

	    #   root = Document.new(response.read_body).root
	    #   root
	    # else
	    #   msg = "<HEAD><ERROR>true</ERROR><ERRORMSG>Account setting problem.</ERRORMSG></HEAD>"
	    #   # respond_to do |m|
	    #   #   m.xml {render :xml=>msg}
	    #   # end
	    #   msg.to_xml
	    # end
  	end

  	get '/desktop/getaccounts' do
  		 @accounts = OtherAccount.where(:user_id.all => [current_user.id]).without(:encrypted_password)
   		 @accounts.to_json
  	end
end