package com.realllinkbilling.message;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.opensymphony.xwork2.ActionSupport;
import com.reallinkbilling.db.GetDBConnection;

public class SaveMessageDetails extends ActionSupport{
	/**
	 * 
	 */
	private static final long serialVersionUID = 7853442552809617863L;
	private static HttpSession session;

	
	@Override
	public String execute() throws Exception {

		String result = "";
		return result;
	}

	
	public static String saveMessageSendDetails(String resp, String cause, String mNumber)
	{
		
		String reply = "";
		
		 session=ServletActionContext.getRequest().getSession(false);
		 
		 	Connection connection = null;
			PreparedStatement preparedStatement= null;
			
			String sql = "";
			String db = (String) session.getAttribute("db");
			try
			{
				connection = new GetDBConnection().getDBConnection(db);

				ObjectMapper mapper = new ObjectMapper();     
		   
		    	MessageReplyPojo messageReplyPojo = mapper.readValue(resp,MessageReplyPojo.class);
		    	String messageBalance = messageReplyPojo.getBalance();
		    	 
		    	 sql = "update settings set value = '"+messageBalance+"' where name = 'messagebalance'";
		    	 
		    	 preparedStatement = connection.prepareStatement(sql);
		    	 
		    	 preparedStatement.executeUpdate();
		    	 
		    	 
		    	 if(preparedStatement != null)
		    	 {
		    		 preparedStatement.close();
		    	 }
		    		 Date messageDateTime = new Date();
				     	SimpleDateFormat messageDateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
				     	
				     	//System.out.println(messageDateFormat.format(messageDateTime));
				     	preparedStatement.close();
				         sql = "insert into messagedetails "
				         		+ "("
				         		+ "datetime,"
				         		+ "mobilenumber,"
				         		+ "senderid,"
				         		+ "status,"
				         		+ "batchid,"
				         		+ "messagecost,"
				         		+ "content,"
				         		+ "messagetype) "
				         		+ "values "
				         		+ "(?,?,?,?,?,?,?,?)";
				         
				         PreparedStatement  preparedStatement2 = connection.prepareStatement(sql);
				         preparedStatement2.setString(1, messageDateFormat.format(messageDateTime));
				         preparedStatement2.setString(2, mNumber);
				         preparedStatement2.setString(3, messageReplyPojo.getMessageObject().getSender());
				         preparedStatement2.setString(4, messageReplyPojo.getStatus());
				         preparedStatement2.setString(5, messageReplyPojo.getBatch_id());
				         preparedStatement2.setString(6, messageReplyPojo.getCost());
				         preparedStatement2.setString(7, messageReplyPojo.getMessageObject().getContent());
				         preparedStatement2.setString(8, cause.trim().toUpperCase());
				         
				         int i = preparedStatement2.executeUpdate();
				    
				         if(preparedStatement2 != null)
							{
							 preparedStatement2.close();	
							}
		    	 
		    	 reply = reply + "Total message cost :"+messageReplyPojo.getCost();
		    	 
		    	 reply = reply + ". Message balance is :"+messageBalance;
		    	 
		    	// System.out.println(reply);
				
			}
			catch (SQLException e)
			{//e.printStackTrace();
				 
			}
			catch (IOException e) 
			{//e.printStackTrace();
				
			}
				finally 
			
			{
					
					if(preparedStatement != null)
					{
						try {preparedStatement.close();} catch (SQLException e) {}
					}
					
					if(connection != null)
					{
						
						try {connection.close();}catch (SQLException e) {}
					}
					
			}
			
			return reply;
	}


	public HttpSession getSession() {
		return session;
	}


	public void setSession(HttpSession session) {
		SaveMessageDetails.session = session;
	}
	
	
}
