package com.reallinkbilling.add;


import java.util.ArrayList;
import java.util.Map;

import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.SessionAware;

import com.reallinkbilling.DAO.RejectionProductListProductDAO;
import com.opensymphony.xwork2.ActionSupport;

public class AddProductToRejectionDetailsList extends ActionSupport implements SessionAware{

	/**
	 * 
	 */
	private static final long serialVersionUID = 6208411094853732491L;
	private SessionMap<String,Object> sessionMap;  
	private String rejectionAddDate;
	private String batchId;
	private String productName;
	private Float quantity;
	
	private ArrayList<RejectionProductListProductDAO> rejectionProductList ;
	
	public String execute() {
		 
		String result = "login";
	
		
	        if(this.getSessionMap()==null || this.getSessionMap().get("login")==null){ 
	        	addActionMessage("Please Logout and Login again !");
	        	result = "login";
	        }  
	        else{
	        		
	         
	        	if(this.getRejectionProductList() == null)
	        	{
	        		this.setRejectionProductList(new ArrayList<RejectionProductListProductDAO>());
	        	}
	        	
	        	
	        	String [] productDetails = this.getProductName().split("-");
	        	
	        	RejectionProductListProductDAO dao = new RejectionProductListProductDAO();
	        	
	        	dao.setRejectionDate(this.getRejectionAddDate());
	        	dao.setBatchId(this.getBatchId());
	        	dao.setProductAllDetails(this.getProductName());
	        	dao.setProductId(productDetails[0]);
	        	dao.setProductName(productDetails[1]);
	        	dao.setProductCode(productDetails[2]);
	        	dao.setQuantity(this.getQuantity());
	        	
	        	this.setProductName("");
	        	this.setQuantity(0.0f);
	        	
	        	this.getRejectionProductList().add(dao);
	        	
	        	this.getSessionMap().put("rejectionList", this.getRejectionProductList());
	        	
	        	result = SUCCESS;
	        	
	        }
	        return result;
	}
	
	@Override
	public void validate() {
		
		this.setRejectionProductList((ArrayList<RejectionProductListProductDAO>) this.getSessionMap().get("rejectionList"));
		
		
		if(this.getRejectionAddDate() == null || this.getRejectionAddDate().equals(""))
		{
			addActionError("Please select rejection date!");
		}
		
		if(!hasActionErrors())
		{
			if(this.getBatchId() == null || this.getBatchId().equals(""))
			{
				addActionError("Please provide batch Id!");
			}
		}
		
		if(!hasActionErrors())
		{
			try
			{
				if(this.getProductName() == null || this.getProductName().equals(""))
				{
					addActionError("Please provide product name!");
					
				}
				else if(this.getProductName().split("-").length < 3)
				{
					addActionError("Please provide correct product name!");
				}
			}catch(Exception e)
			{
				addActionError("Please provide correct product name!");
			}
		}
		
		
		if(!hasActionErrors())
		{
			if(this.getQuantity() == null )
			{
				addActionError("Please provide quantity!");
			}
			else if(this.getQuantity() <= 0)
			{
				addActionError("Please provide correct quantity!");
			}
		}
		
		
		if(!hasActionErrors())
		{
			if(this.getRejectionProductList() != null)
			{
				if(this.getRejectionProductList().size() >= 1)
				{
					for(RejectionProductListProductDAO dao:this.getRejectionProductList())
					{
						if(dao.getProductAllDetails().equals(this.getProductName()))
						{
							addActionError("This product is already added to list!");
						}
					}
				}
			}
		}
	}
	
	
	public String getRejectionAddDate() {
		return rejectionAddDate;
	}


	public void setRejectionAddDate(String rejectionAddDate) {
		this.rejectionAddDate = rejectionAddDate;
	}


	public String getBatchId() {
		return batchId;
	}


	public void setBatchId(String batchId) {
		this.batchId = batchId;
	}


	public String getProductName() {
		return productName;
	}


	public void setProductName(String productName) {
		this.productName = productName;
	}


	public Float getQuantity() {
		return quantity;
	}


	public void setQuantity(Float quantity) {
		this.quantity = quantity;
	}


	public static long getSerialversionuid() {
		return serialVersionUID;
	}


	public ArrayList<RejectionProductListProductDAO> getRejectionProductList() {
		return rejectionProductList;
	}


	public void setRejectionProductList(ArrayList<RejectionProductListProductDAO> rejectionProductList) {
		this.rejectionProductList = rejectionProductList;
	}


	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;  
	}
	

}



