package com.reallinkbilling.delete;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.reallinkbilling.db.GetDBConnection;

public class DeleteGodown extends ActionSupport implements SessionAware {

	private static final long serialVersionUID = 1L;

	private SessionMap<String, Object> sessionMap;

	private String godownId;
	private String godownCode;

	@Override
	public String execute() {
		
		System.out.println("========== DELETE GODOWN ==========");
		System.out.println("SESSION MAP = " + this.getSessionMap());
		System.out.println("LOGIN = " + this.getSessionMap().get("login"));
		System.out.println("SHOP ID = " + this.getSessionMap().get("shopId"));
		System.out.println("SHOP CODE = " + this.getSessionMap().get("shopCode"));
		System.out.println("GODOWN ID = " + this.getGodownId());

		String result = LOGIN;

		/*
		 * Check session
		 */
		if (this.getSessionMap() == null
				|| this.getSessionMap().get("login") == null) {

			addActionMessage("Please Logout and Login again!");

			return LOGIN;
		}

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

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

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

		/*
		 * Validate shop session
		 */
		if (shopId == null || shopId.trim().equals("")) {

			addActionError("Shop ID not found in session!");

			return ERROR;
		}

		if (shopCode == null || shopCode.trim().equals("")) {

			addActionError("Shop Code not found in session!");

			return ERROR;
		}

		if (db == null || db.trim().equals("")) {

			addActionError("Database not found in session!");

			return ERROR;
		}

		/*
		 * Validate godown
		 */
		if (godownId == null || godownId.trim().equals("")) {

			addActionError("Please select a godown to delete!");

			return ERROR;
		}

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

		try {

			connection =
					GetDBConnection.getDBConnection(db);

			/*
			 * -------------------------------------------------
			 * CHECK GODOWN STOCK
			 * -------------------------------------------------
			 *
			 * If warehouse_stock_master is used in EasySBill,
			 * prevent deletion when stock is available.
			 */
			String checkStockSql =
					"SELECT COUNT(*) "
					+ "FROM warehouse_stock_master "
					+ "WHERE warehouse_id = ? "
					+ "AND in_stock_quantity > 0";

			preparedStatement =
					connection.prepareStatement(checkStockSql);

			preparedStatement.setString(
					1,
					this.getGodownId());

			resultSet =
					preparedStatement.executeQuery();

			boolean canDelete = true;

			if (resultSet.next()
					&& resultSet.getInt(1) > 0) {

				canDelete = false;

				addActionError(
						"Cannot delete godown. "
						+ "It has stock in the warehouse!");
			}

			/*
			 * Close stock checking resources
			 */
			if (resultSet != null) {

				resultSet.close();
				resultSet = null;
			}

			if (preparedStatement != null) {

				preparedStatement.close();
				preparedStatement = null;
			}

			/*
			 * -------------------------------------------------
			 * DELETE GODOWN
			 * -------------------------------------------------
			 */
			if (canDelete) {

				String deleteSql =
						"DELETE FROM shop_godown_master "
						+ "WHERE godown_id = ? "
						+ "AND shop_id = ? "
						+ "AND shop_code = ?";

				preparedStatement =
						connection.prepareStatement(deleteSql);

				preparedStatement.setString(
						1,
						this.getGodownId());

				preparedStatement.setString(
						2,
						shopId);

				preparedStatement.setString(
						3,
						shopCode);

				int k =
						preparedStatement.executeUpdate();

				if (k > 0) {

					addActionMessage(
							"Godown deleted successfully!");

					result = SUCCESS;

				} else {

					addActionError(
							"Godown not found or already deleted!");

					result = ERROR;
				}

			} else {

				result = ERROR;
			}

		} catch (SQLException e) {

			result = ERROR;

			addActionError(
					e.getErrorCode()
					+ " || "
					+ e.getMessage()
					+ " || Something Went Wrong!");

		} catch (Exception e) {

			result = ERROR;

			addActionError(
					e.getMessage()
					+ " || Something Went Wrong!");

		} 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;
	}

	@Override
	public void validate() {

		if (this.getGodownId() == null
				|| this.getGodownId().trim().equals("")) {

			addActionError(
					"Please select a godown to delete!");
		}
	}

	/*
	 * -------------------------------------------------
	 * GETTERS AND SETTERS
	 * -------------------------------------------------
	 */

	public String getGodownId() {
		return godownId;
	}

	public void setGodownId(String godownId) {
		this.godownId = godownId;
	}

	public String getGodownCode() {
		return godownCode;
	}

	public void setGodownCode(String godownCode) {
		this.godownCode = godownCode;
	}

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

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

		this.sessionMap = sessionMap;
	}

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

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