package com.reallinkbilling.jspRedirect;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.SessionAware;

import com.reallinkbilling.DAO.ProductBarcodeDetailsDAO;
import com.reallinkbilling.db.GetDBConnection;
import com.reallinkbilling.resource.GetListResource;
import com.opensymphony.xwork2.ActionSupport;

public class GetBarcodeDetails extends ActionSupport implements SessionAware{

	/**
	 * 
	 */
	private static final long serialVersionUID = 6208411094853732491L;
	private SessionMap<String,Object> sessionMap;  
	
	private String productName;
	private String quantity;
	private String labelSize;
	private Boolean addCodedPrice;
	private Boolean addSupplierId;
	private Boolean addPurchaseDate;
	private Boolean addMrp;
	private String productActualName;
	private String codedPrice;
	private String supplierId;
	private String puchaseDate;
	private String headName;
	private String mrpPrice;
	private String productCode;
	private String activexobject;
	private String printerName;
	
	private List<String> labelSizeList = new ArrayList<String>(); 
	
	
	public String execute() {
		 
		String result = "login";
	
		
	        if(this.getSessionMap()==null || this.getSessionMap().get("login")==null){ 
	        	addActionMessage("Please Logout and Login again !");
	        	result = "login";
	        }  
	        else{
	    		DecimalFormat df = new DecimalFormat("##");
	    		DecimalFormat df2 = new DecimalFormat("##.##");
				
	        	Connection connection = null;
				PreparedStatement preparedStatement= null;
				ResultSet resultSet = null;
			
				String shopId = (String) this.getSessionMap().get("shopId");
				String shopCode = (String) this.getSessionMap().get("shopCode");
					
				String sql = "";
				String db = (String)  this.getSessionMap().get("db");
				
				String productDet [] = this.getProductName().split("-");
	        	
				String productCode = "";
	    		
				try
				{
	    			connection = GetDBConnection.getDBConnection(db);
	    			
	    			sql = "SELECT product_short_code "
	    					+ "FROM "
	    					+ "shop_product_master "
	    					+ "WHERE "
	    					+ "product_id = ? "
	    					+ "AND "
	    					+ "shop_id = ? "
	    					+ "AND "
	    					+ "shop_code = ?";
	    			
	    			preparedStatement = connection.prepareStatement(sql);
	    			preparedStatement.setString(1, productDet[0]);
	    			preparedStatement.setString(2, shopId);
	    			preparedStatement.setString(3, shopCode);
	    			
	    			resultSet = preparedStatement.executeQuery();
	    			
	    			if(resultSet.next())
	    			{
	    				
	    				productCode = resultSet.getString(1);
	    				
	    			}
	    			
	    			HashMap<String,ProductBarcodeDetailsDAO> productBarcodeDetailsMap = (HashMap<String, ProductBarcodeDetailsDAO>) this.getSessionMap().get("barcodeProductDetails");
		        	
	    			ProductBarcodeDetailsDAO  barcodeDetailsDAO = productBarcodeDetailsMap.get(productCode);
	    	
	    			
	    			if(resultSet != null)
	    			{
	    				resultSet.close();
	    				resultSet = null;
	    			}
	    			
	    			if(preparedStatement != null)
	    			{
	    				preparedStatement.close();
	    				preparedStatement = null;
	    			}
	    			String supplierId="";
	    			String purDate = "";
	    			
	    			sql = "SELECT "
	    					+ "pinfo.supplier_id,"
	    					+ "DATE_FORMAT(pinfo.purchase_date,'%d%m%Y') "
	    					+ "FROM "
	    					+ "shop_purchase_info_master AS pinfo INNER JOIN shop_purchase_product_info_master AS ppinfo "
	    					+ "ON "
	    					+ "pinfo.purchase_number = ppinfo.purchase_number "
	    					+ "WHERE "
	    					+ "pinfo.shop_id = ? "
	    					+ "AND "
	    					+ "pinfo.shop_code = ? "
	    					+ "AND "
	    					+ "ppinfo.shop_id = ? "
	    					+ "AND "
	    					+ "ppinfo.shop_code = ? "
	    					+ "AND "
	    					+ "ppinfo.product_id = ? "
	    					+ "ORDER BY pinfo.purchase_date DESC LIMIT 1";
	    			
	    			
	    			preparedStatement = connection.prepareStatement(sql);
	    			
	    			preparedStatement.setString(1, shopId);
	    			preparedStatement.setString(2, shopCode);
	    			preparedStatement.setString(3, shopId);
	    			preparedStatement.setString(4, shopCode);
	    			
	    			
	    			preparedStatement.setString(5, productDet[0]);
	    			
	    			resultSet = preparedStatement.executeQuery();
	    			
	    			while(resultSet.next())
					{
	    				supplierId = resultSet.getString(1);
	    				purDate = resultSet.getString(2);
					}
	    					
	    			
	    			 String coddedPrice = "";
	    			 
	    				 coddedPrice = "01"+ df.format(barcodeDetailsDAO.getCustomerSalePrice())
		        		 	+"3"+ df.format(barcodeDetailsDAO.getPurchasePrice())+"27";
		        	 
		        	 this.setProductCode(productCode);
		        	 this.setSupplierId(supplierId);
		        	 this.setPuchaseDate(purDate);
		        	 this.setCodedPrice(coddedPrice);
		        	 this.setMrpPrice(df2.format(barcodeDetailsDAO.getBarcodeMRP()));
		        	 this.setProductActualName(barcodeDetailsDAO.getProductNameEn());
		        	 this.setHeadName((String) this.getSessionMap().get("shopnameenglish"));
		               
		        	 result = SUCCESS;
				}
				catch (SQLException e) {
					e.printStackTrace();
					result = "error";
					
					addActionError(e.getErrorCode()+" || "+e.getMessage() +" || Details not found!");
				}
				catch (Exception e) {
					e.printStackTrace();
						result = "error";
					
					addActionError(e.getMessage() +" || Details not found !");
				}
				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) {}
					}
					
				}
	        	
	        }
	        return result;
	}
	
@Override
public void validate() {
	if(this.getProductName() == null || this.getProductName().equals(""))
	{
		addActionError("Please provide correct product name!");
	}
	
	if(!hasActionErrors())
	{
		try
		{
			String [] s = this.getProductName().split("-");
			
			if(s.length <3)
			{
				addActionError("Please provide correct product name!");
			}
		}catch (Exception e) {
			addActionError("Please provide correct product name!");
		}
	}
	
		this.setLabelSizeList(GetListResource.getBarcodeLableSizeList());
    	
	
}
	
	public String getProductName() {
		return productName;
	}
	public void setProductName(String productName) {
		this.productName = productName;
	}
	public String getQuantity() {
		return quantity;
	}
	public void setQuantity(String quantity) {
		this.quantity = quantity;
	}
	public String getLabelSize() {
		return labelSize;
	}
	public void setLabelSize(String labelSize) {
		this.labelSize = labelSize;
	}
	public Boolean getAddCodedPrice() {
		return addCodedPrice;
	}
	public void setAddCodedPrice(Boolean addCodedPrice) {
		this.addCodedPrice = addCodedPrice;
	}
	public Boolean getAddSupplierId() {
		return addSupplierId;
	}
	public void setAddSupplierId(Boolean addSupplierId) {
		this.addSupplierId = addSupplierId;
	}
	public Boolean getAddPurchaseDate() {
		return addPurchaseDate;
	}
	public void setAddPurchaseDate(Boolean addPurchaseDate) {
		this.addPurchaseDate = addPurchaseDate;
	}
	public static long getSerialversionuid() {
		return serialVersionUID;
	}
	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;  
	}

	public String getCodedPrice() {
		return codedPrice;
	}

	public void setCodedPrice(String codedPrice) {
		this.codedPrice = codedPrice;
	}

	public String getSupplierId() {
		return supplierId;
	}

	public String getHeadName() {
		return headName;
	}

	public String getMrpPrice() {
		return mrpPrice;
	}

	public void setMrpPrice(String mrpPrice) {
		this.mrpPrice = mrpPrice;
	}

	public void setHeadName(String headName) {
		this.headName = headName;
	}

	public String getProductCode() {
		return productCode;
	}

	public void setProductCode(String productCode) {
		this.productCode = productCode;
	}

	public void setSupplierId(String supplierId) {
		this.supplierId = supplierId;
	}

	public String getPuchaseDate() {
		return puchaseDate;
	}

	public Boolean getAddMrp() {
		return addMrp;
	}

	public void setAddMrp(Boolean addMrp) {
		this.addMrp = addMrp;
	}

	public void setPuchaseDate(String puchaseDate) {
		this.puchaseDate = puchaseDate;
	}

	public List<String> getLabelSizeList() {
		return labelSizeList;
	}

	public void setLabelSizeList(List<String> labelSizeList) {
		this.labelSizeList = labelSizeList;
	}

	public String getProductActualName() {
		return productActualName;
	}

	public void setProductActualName(String productActualName) {
		this.productActualName = productActualName;
	}

	public String getActivexobject() {
		return activexobject;
	}

	public void setActivexobject(String activexobject) {
		this.activexobject = activexobject;
	}

	public String getPrinterName() {
		return printerName;
	}

	public void setPrinterName(String printerName) {
		this.printerName = printerName;
	}
	

}