package com.reallinkbilling.update;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;
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.resource.CheckAlreadyAvailable;

public class UpdateSupplier extends ActionSupport implements SessionAware{

	/**
	 * 
	 */
	private static final long serialVersionUID = 6424268643743578099L;
	
	private Map<String,String> customerSearchOptionList = new HashMap<String,String>();
	private String customerSearchOption;
	private String defaultValue;
	private HttpSession session;
	
	private String supplierId;
	private String supplierName;
	private String personName;
	private String mobileNumber;
	private String email;
	private String address;
	private String gstNumber;
	private String pinCodeNumber;
	
	private SessionMap<String,Object> sessionMap;  
	
	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;
				String shopId = (String) this.getSessionMap().get("shopId");
	    		String shopCode = (String) this.getSessionMap().get("shopCode");
	    		
				
	        	customerSearchOptionList.put("BYNAME", "BY NAME");
				customerSearchOptionList.put("BYMOBILE", "BY MOBILE");
				customerSearchOptionList.put("BYADDRESS","BY ADDRESS");
				
				setDefaultValue("BYNAME");
				setCustomerSearchOption("BYNAME");
				
	        
				
				String sql = "";
				String db = (String) this.getSessionMap().get("db");
	        
				try
				{
					
					connection = GetDBConnection.getDBConnection(db);
					
					sql = "UPDATE "
							+ "shop_supplier_master "
							+ "SET "
							+ "suplier_name = ?,"
							+ "contact_person_name = ?,"
							+ "contact_number = ?,"							
							+ "supplier_address = ?,"
							+ "gst_number = ?,"
							+ "pin_code_number =? "
							+ "WHERE "
							+ "shop_id = ? "
							+ "AND "
							+ "shop_code = ? "
							+ "AND "
							+ "supplier_id = ? ";
					
					preparedStatement = connection.prepareStatement(sql);
					
					preparedStatement.setString(1, this.getSupplierName().trim().toUpperCase());
					preparedStatement.setString(2, this.getPersonName().trim().toUpperCase());
					preparedStatement.setString(3, this.getMobileNumber().trim());					
					preparedStatement.setString(4, this.getAddress().trim().toUpperCase());
					preparedStatement.setString(5, this.getGstNumber().trim().toUpperCase());
					preparedStatement.setString(6, this.getPinCodeNumber().trim().toUpperCase());
					preparedStatement.setString(7, shopId);
					preparedStatement.setString(8, shopCode);
					preparedStatement.setString(9, this.getSupplierId());
					
					int rowCountUpdated = preparedStatement.executeUpdate();
					
					if(rowCountUpdated > 0)
					{
						result = "success";
						addActionMessage("Supplier details updated successfully!");
					}
					else
					{
						result = "error";
						addActionError("Error ! Supplier details not updated!");
					}
					
					
				}catch (SQLException e) {
					result = "error";
					//e.printStackTrace();
					addActionError(e.getErrorCode()+" || "+e.getMessage() +" || Supplier details not updated !");
				} catch (IOException e) {
					//e.printStackTrace();
					result = "error";						
					addActionError(e.getMessage()+" || "+e.getCause()+" || Supplier details not updated !");
				}finally {
				
				if(preparedStatement != null)
				{
					try {preparedStatement.close();} catch (SQLException e) {}
				}
				if(connection != null)
				{
					
					try {connection.close();}catch (SQLException e) {}
				}
		
	}



}
return result;
}
@Override
public void validate() 
{
		// TODO Auto-generated method stub
	
	
	if(getSupplierName().equals("") || getPersonName().equals("") || getMobileNumber().equals("") || getAddress().equals(""))
	{
		addActionError("Please provide details for fields marked with (*) astrick!");
	}

	if(!hasActionErrors())
	{
		if(!getMobileNumber().equals(""))
		{
			if(getMobileNumber().length() >10)
			{
				addActionError("Please Provide correct mobile number ! (10 digit)");
			}
		}
	}
	if(!hasActionErrors())
	{
		if(this.getGstNumber() == null || this.getGstNumber().equals(""))
		{
			addActionError("Please provide GST number or specify as 'NA'!");
		}
	}
	
	if(!hasActionErrors())
	{
		if(this.getPinCodeNumber() == null || this.getPinCodeNumber().equals(""))
		{
			addActionError("Please provide pincode number of specify as 'NA'!");
		}
	}
	
	if(!hasActionErrors())
	{
		String isCustomerAdded = "false";
		
		String [] result = new String[2];
		String condition = "AND suplier_name != '"+getSupplierName().trim()+"'";
		
		result = new CheckAlreadyAvailable().checkAvaibility("shop_supplier_master", "suplier_name", condition, this.getSupplierName().trim(), this.getSessionMap());
		
		isCustomerAdded = result[0];
		
		if(isCustomerAdded.equals("true"))
		{
			addActionError("Supplier is already added with same name!");
		}				
		
	}
	
	if(!hasActionErrors())
	{
		String isCustomerAdded = "false";
		
		String [] result = new String[2];
		
		String condition = "AND contact_number != '"+getMobileNumber()+"'";
		
		result = new CheckAlreadyAvailable().checkAvaibility("shop_supplier_master", "contact_number", condition, this.getMobileNumber(), this.getSessionMap());
		
		isCustomerAdded = result[0];
		
		if(isCustomerAdded.equals("true"))
		{
			addActionError("Supplier is already added with same mobile number!");
		}				
		
	}
	
	
	if(!hasActionErrors())
	{
		String isCustomerAdded = "false";
		
		String [] result = new String[2];
		String condition = "AND gst_number != '"+getGstNumber().trim()+"'";
		
		result = new CheckAlreadyAvailable().checkAvaibility("shop_supplier_master", "gst_number", condition, this.getGstNumber().trim(), this.getSessionMap());
		
		isCustomerAdded = result[0];
		
		if(isCustomerAdded.equals("true"))
		{
			addActionError("Supplier is already added with provided GST Number!");
		}				
		
	}
	}

	public Map<String, String> getCustomerSearchOptionList() {
		return customerSearchOptionList;
	}

	public void setCustomerSearchOptionList(Map<String, String> customerSearchOptionList) {
		this.customerSearchOptionList = customerSearchOptionList;
	}

	public String getCustomerSearchOption() {
		return customerSearchOption;
	}

	public void setCustomerSearchOption(String customerSearchOption) {
		this.customerSearchOption = customerSearchOption;
	}

	public String getDefaultValue() {
		return defaultValue;
	}

	public void setDefaultValue(String defaultValue) {
		this.defaultValue = defaultValue;
	}

	public HttpSession getSession() {
		return session;
	}

	public void setSession(HttpSession session) {
		this.session = session;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public String getSupplierId() {
		return supplierId;
	}

	public void setSupplierId(String supplierId) {
		this.supplierId = supplierId;
	}

	public String getSupplierName() {
		return supplierName;
	}

	public void setSupplierName(String supplierName) {
		this.supplierName = supplierName;
	}

	public String getPersonName() {
		return personName;
	}

	public void setPersonName(String personName) {
		this.personName = personName;
	}

	public String getMobileNumber() {
		return mobileNumber;
	}

	public void setMobileNumber(String mobileNumber) {
		this.mobileNumber = mobileNumber;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}


	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}



	public String getGstNumber() {
		return gstNumber;
	}
	public void setGstNumber(String gstNumber) {
		this.gstNumber = gstNumber;
	}
	public String getPinCodeNumber() {
		return pinCodeNumber;
	}
	public void setPinCodeNumber(String pinCodeNumber) {
		this.pinCodeNumber = pinCodeNumber;
	}
	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;  
	}
		
	
	
}
