package com.reallinkbilling.jspRedirect;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.reallinkbilling.db.GetDBConnection;
import com.reallinkbilling.utils.LabelCapSchema;
import com.reallinkbilling.utils.PackingMasterOptions;

public class AddNewProductionJsp extends ActionSupport implements SessionAware{

	/**
	 * 
	 */
	private static final long serialVersionUID = 5592543072791668645L;
	
	private HttpSession session;
	private SessionMap<String, Object> sessionMap;
	private String productionDateTime;
	
	private Map<String,String> finishedProductList = new LinkedHashMap<String,String>();
	private Map<String,String> labelList = new LinkedHashMap<String,String>();
	private Map<String,String> capList = new LinkedHashMap<String,String>();
	private String finishedProductId;
	private String labelId;
	private String capId;
	
	public String execute(){
		
		String result = "login";
		session=ServletActionContext.getRequest().getSession(false); 
		 
	        if(session==null || session.getAttribute("login")==null){ 
	        	addActionMessage("Please Login First !!!");
	        	result = "login";
	        }  
	        else
	        {
	        	
	        	Connection connection = null;
	    		PreparedStatement preparedStatement= null;
	    		ResultSet resultSet = null;
	    	
	    		String sql = "";
	    		String db = (String) this.getSessionMap().get("db");
	    		
	    		String shopId = (String) this.getSessionMap().get("shopId");
	    		String shopCode = (String) this.getSessionMap().get("shopCode");
	         	
	    		
	    		Date d = new Date();
	        	SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
	    		this.setProductionDateTime(dateFormat.format(d));
	    		
	    		try
	    		{
	    			connection = GetDBConnection.getDBConnection(db);
	    			LabelCapSchema.ensure(this.getSessionMap());
	    			PackingMasterOptions.loadFinishedProducts(connection, shopId, shopCode, this.getFinishedProductList());
	    			PackingMasterOptions.loadLabels(connection, shopId, shopCode, this.getLabelList());
	    			PackingMasterOptions.loadCaps(connection, shopId, shopCode, this.getCapList());
	    			
	    			this.setFinishedProductId("");
	    			this.setLabelId("");
	    			this.setCapId("");
	    			
	    			result = SUCCESS;
	    		}
	    		catch (SQLException e) {
	    			
	    			result = ERROR;	    			
	    			addActionError(e.getErrorCode()+" || "+e.getMessage() +" || Something Went Wrong!");
	    			
	    		} catch (IOException e) {
	    			
	    			result = ERROR;	    			
	    			addActionError(e.getMessage()+" || "+e.getCause()+" || Something Went Wrong!");
	    			
	    		}finally {
	    			if(resultSet != null)
    				{
    					try {resultSet.close(); resultSet = null;} catch (SQLException e) {}
    				}
	    			if(preparedStatement != null)
	    			{
	    				try {preparedStatement.close(); preparedStatement = null;} catch (SQLException e) {}
	    			}
	    			if(connection != null)
	    			{
	    				
	    				try {connection.close(); connection = null;}catch (SQLException e) {}
	    			}
	    			
	    		}
	        	
	        	 	
	        }

	        if(SUCCESS.equals(result) || ERROR.equals(result))
	        {
	        	if(this.isPhoneUserAgent())
	        	{
	        		result = "mobile";
	        	}
	        }
	        
	        return result;
	}

	private boolean isPhoneUserAgent()
	{
		String ua = ServletActionContext.getRequest().getHeader("User-Agent");
		if(ua == null)
		{
			return false;
		}
		String agent = ua.toLowerCase();
		return agent.contains("android")
				|| agent.contains("iphone")
				|| agent.contains("ipad")
				|| agent.contains("ipod");
	}

	public HttpSession getSession() {
		return session;
	}

	public void setSession(HttpSession session) {
		this.session = session;
	}

	public Map<String, String> getFinishedProductList() {
		return finishedProductList;
	}

	public void setFinishedProductList(Map<String, String> finishedProductList) {
		this.finishedProductList = finishedProductList;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}
	

	public String getProductionDateTime() {
		return productionDateTime;
	}

	public void setProductionDateTime(String productionDateTime) {
		this.productionDateTime = productionDateTime;
	}

	public String getFinishedProductId() {
		return finishedProductId;
	}

	public void setFinishedProductId(String finishedProductId) {
		this.finishedProductId = finishedProductId;
	}

	public Map<String, String> getLabelList() {
		return labelList;
	}

	public void setLabelList(Map<String, String> labelList) {
		this.labelList = labelList;
	}

	public Map<String, String> getCapList() {
		return capList;
	}

	public void setCapList(Map<String, String> capList) {
		this.capList = capList;
	}

	public String getLabelId() {
		return labelId;
	}

	public void setLabelId(String labelId) {
		this.labelId = labelId;
	}

	public String getCapId() {
		return capId;
	}

	public void setCapId(String capId) {
		this.capId = capId;
	}

	public SessionMap<String, Object> getSessionMap() {
		return sessionMap;
	}

	public void setSessionMap(SessionMap<String, Object> sessionMap) {
		this.sessionMap = sessionMap;
	}

 
	public void setSession(Map<String, Object> map) {  
	    sessionMap=(SessionMap<String, Object>)map;  
	}

	
}
