package com.reallinkbilling.bulksmsgateway;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

import com.reallinkbilling.bulksmsgateway.BulkSMSGatewayMessageReplyPojo;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;




public class SendBulkSMSGatewayMessage {
	
	

	      public static String sendLanguageSms(String userName, String password, String sToPhoneNo,String sMessage, String senderId, String templateId)
	       {
	    	 /* System.out.println("userName : "+userName);
	    	  System.out.println("Password : "+password);
	    	  System.out.println("sToPhoneNo : "+sToPhoneNo);
	    	  System.out.println("sMessage : "+sMessage);
	    	  System.out.println("senderId :"+senderId);
	    	 */
	    	  String sResult="";
	          try 
	          {
	           // Construct data
	        	  
        	  //login.bulksmsgateway.in/unicodesmsapi.php?
        	  //username=........&password=.......&mobilenumber=........
        	  //&message=.......&senderid=.......&type=3
	           String data = "username=" + URLEncoder.encode(userName,"UTF-8");
	           data += "&password="+URLEncoder.encode(password,"UTF-8");
	           data += "&mobilenumber="+ URLEncoder.encode(sToPhoneNo, "UTF-8");
	           data += "&message=" + URLEncoder.encode(sMessage, "UTF-8");
	           data += "&senderid="+ URLEncoder.encode(senderId, "UTF-8");          
	           data += "&type="+URLEncoder.encode("3", "UTF-8");
	           data += "&template_id="+URLEncoder.encode(templateId, "UTF-8");
	         //  System.out.println(sMessage);
	           // Send data
	           URL url = new URL("https://login.bulksmsgateway.in/unicodesmsapi.php?"+data);
	       //    System.out.println(url);
	           URLConnection conn = url.openConnection();
	           conn.setDoOutput(true);
	           OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
	           wr.write(data);
	           wr.flush();
	           // Get the response
	           BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
	           String line;
	         
	           while ((line = rd.readLine()) != null) 
	           {
	                 // Process line...
	                 sResult=sResult+line+" ";
	           }
	           wr.close();
	           rd.close();
	           
	           } 
	          catch (Exception e) 
              {
                 //System.out.println("Error SMS "+e);
                 return "Error "+e;
              }
	     
	        
	         // System.out.println(sResult);    
	         
	          return sResult;  
	        }
	      
	      
	      
	      
	      public static String sendSms(String userName, String password, String sToPhoneNo,String sMessage, String senderId, String templateId)
	       {
	    	 /* System.out.println("userName : "+userName);
	    	  System.out.println("Password : "+password);
	    	  System.out.println("sToPhoneNo : "+sToPhoneNo);
	    	  System.out.println("sMessage : "+sMessage);
	    	  System.out.println("senderId :"+senderId);
	    	 */
	    	  String sResult="";
	          try 
	          {
	           // Construct data
	        	  
       	  //login.bulksmsgateway.in/unicodesmsapi.php?
       	  //username=........&password=.......&mobilenumber=........
       	  //&message=.......&senderid=.......&type=3
	           String data = "user=" + URLEncoder.encode(userName,"UTF-8");
	           data += "&password="+URLEncoder.encode(password,"UTF-8");
	           data += "&mobile="+ URLEncoder.encode(sToPhoneNo, "UTF-8");
	           data += "&message=" + URLEncoder.encode(sMessage, "UTF-8");
	           data += "&sender="+ URLEncoder.encode(senderId, "UTF-8");          
	           data += "&type="+URLEncoder.encode("3", "UTF-8");
	           data += "&template_id="+URLEncoder.encode(templateId, "UTF-8");
	         
	           // Send data
	           URL url = new URL("https://login.bulksmsgateway.in/sendmessage.php?"+data);
	          // System.out.println(url);
	           URLConnection conn = url.openConnection();
	           conn.setDoOutput(true);
	           OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
	           wr.write(data);
	           wr.flush();
	           // Get the response
	           BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
	           String line;
	         
	           while ((line = rd.readLine()) != null) 
	           {
	                 // Process line...
	                 sResult=sResult+line+" ";
	           }
	           wr.close();
	           rd.close();
	           
	           } 
	          catch (Exception e) 
             {
                //System.out.println("Error SMS "+e);
                return "Error "+e;
             }
	     
	        		
	       //  System.out.println(sResult);
	          return sResult;  
	        }
	      
	      public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
			
	    	  
	    
	    	  String msg = "SHETKARI RAJA"
							+"\nDate:27-04-2021"
							+"\nName:SHAM SUTAR"
							+"\nCode:4"
							+"\nShift:MORNING"
							+"\nType:COW"
							+"\nLtr:5.8"
							+"\nFAT:3.5"
							+"\nCLR:29.5"
							+"\nSNF:8.5";
	    
	    	  
	    
	    	  System.out.println(msg);
	    	  
	    
	    	 String s =  SendBulkSMSGatewayMessage.sendSms("rahullondhedemo","Sushama@123","8208064913", msg,"SRDUDH","1507161932996629783");
	    	 																										   
	    	 			
	    	 ObjectMapper mapper = new ObjectMapper();
	    	  mapper.configure(com.fasterxml.jackson.core.JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
	    	 
	    	 BulkSMSGatewayMessageReplyPojo pojo = mapper.readValue(s,BulkSMSGatewayMessageReplyPojo.class);
	    	 
	    	 System.out.println(s);
	    	 System.out.println("Message Balance: "+pojo.getRemainingcredits());
	    	 System.out.println("Status : "+pojo.getStatus());
	    	 System.out.println("msgcount : "+pojo.getMsgcount());
	    	  
		}
	}

