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.util.ArrayList;
import java.util.List;
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.DAO.bankAccountDetailsDAO;
import com.reallinkbilling.cipher.EncryptionDecryption;
import com.reallinkbilling.db.GetDBConnection;

public class ListBankAccountJsp extends ActionSupport implements SessionAware {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3467435617843807795L;

	private HttpSession session;

	private List<bankAccountDetailsDAO> bankAccountInfoList =
			new ArrayList<bankAccountDetailsDAO>();

	private SessionMap<String, Object> sessionMap;

	public String execute() throws Exception {

		String result = "login";

		session = ServletActionContext.getRequest().getSession(false);

		if (session == null || session.getAttribute("login") == null) {

			addActionMessage("Please Login First !!!");
			result = "login";

		} else {

			String shopId = (String) this.getSessionMap().get("shopId");
			String shopCode = (String) this.getSessionMap().get("shopCode");

			Connection connection = null;
			PreparedStatement preparedStatement = null;
			ResultSet resultSet = null;

			String sql = "";
			String db = (String) this.getSessionMap().get("db");

			try {

				connection = GetDBConnection.getDBConnection(db);

				sql = "SELECT "
						+ "account_id,"
						+ "account_holder_name,"
						+ "account_number,"
						+ "ifsc_code,"
						+ "bank_name,"
						+ "branch,"
						+ "opening_balance "
						+ "FROM "
						+ "bank_account "
						+ "WHERE "
						+ "shop_id = ? "
						+ "AND "
						+ "shop_code = ? "
						+ "ORDER BY account_id ASC";

				preparedStatement = connection.prepareStatement(sql);

				preparedStatement.setString(1, shopId);
				preparedStatement.setString(2, shopCode);

				resultSet = preparedStatement.executeQuery();

				resultSet.last();

				int rowCount = resultSet.getRow();

				resultSet.beforeFirst();

				if (rowCount > 0) {

					while (resultSet.next()) {

						bankAccountDetailsDAO dao =
								new bankAccountDetailsDAO();

						dao.setKey(
								EncryptionDecryption.encrypt(
										resultSet.getString(1)
								)
						);

						dao.setAccountId(resultSet.getString(1));

						dao.setAccountHolderName(
								resultSet.getString(2)
						);

						dao.setAccountNumber(
								resultSet.getString(3)
						);

						dao.setIfscCode(
								resultSet.getString(4)
						);

						dao.setBankName(
								resultSet.getString(5)
						);

						dao.setBranch(
								resultSet.getString(6)
						);

						dao.setOpeningBalance(
								resultSet.getString(7)
						);

						this.getBankAccountInfoList().add(dao);
					}

					result = "success";

				} else {

					result = "error";

					addActionError(
							"Bank account details not found!"
					);
				}

			} catch (SQLException e) {

				result = "error";

				addActionError(
						e.getErrorCode()
						+ " || "
						+ e.getMessage()
				);

			} catch (IOException e) {

				result = "error";

				addActionError(
						e.getMessage()
						+ " || "
						+ e.getCause()
				);

			} finally {

				if (resultSet != null) {

					try {
						resultSet.close();
					} catch (SQLException e) {
					}
				}

				if (preparedStatement != null) {

					try {
						preparedStatement.close();
					} catch (SQLException e) {
					}
				}

				if (connection != null) {

					try {
						connection.close();
					} catch (SQLException e) {
					}
				}
			}
		}

		return result;
	}

	public HttpSession getSession() {
		return session;
	}

	public void setSession(HttpSession session) {
		this.session = session;
	}

	public List<bankAccountDetailsDAO> getBankAccountInfoList() {
		return bankAccountInfoList;
	}

	public void setBankAccountInfoList(
			List<bankAccountDetailsDAO> bankAccountInfoList) {

		this.bankAccountInfoList = bankAccountInfoList;
	}

	@Override
	public void setSession(Map<String, Object> arg0) {

		sessionMap = (SessionMap<String, Object>) arg0;
	}

	public SessionMap<String, Object> getSessionMap() {
		return sessionMap;
	}

	public void setSessionMap(
			SessionMap<String, Object> sessionMap) {

		this.sessionMap = sessionMap;
	}
}