package com.reallinkbilling.ajax;

import java.util.HashMap;

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

import com.opensymphony.xwork2.ActionSupport;
import com.reallinkbilling.DAO.ProductBarcodeDetailsDAO;
import com.reallinkbilling.util.BarcodeProductDetailsSessionLoader;

public class GetBarcodeProductInfo extends ActionSupport implements SessionAware {

    private static final long serialVersionUID = 1L;

    private SessionMap<String,Object> sessionMap;

    private String productCode;

    /** productId-nameEng-product_alias for AddProductToBarcodeBill when productName is filled. */
    private String compositeProductName;

    // Response fields
    private String productName;
    private String productNameMr;
    private String salesUnit;
    private String taxId;
    private Float salesRate;              // barcode MRP
    private Float amount;                 // same as MRP
    private Float amountAfterDiscount;    // sales price after discount
    private Float quantity;               // default barcode quantity

    @SuppressWarnings("unchecked")
    public String execute(){
        try{
            HashMap<String,ProductBarcodeDetailsDAO> barcodeDetailsMap = (HashMap<String, ProductBarcodeDetailsDAO>) this.getSessionMap().get("barcodeProductDetails");
            if (barcodeDetailsMap == null || barcodeDetailsMap.isEmpty()) {
                try {
                    BarcodeProductDetailsSessionLoader.loadAndPutInSession(this.getSessionMap());
                } catch (Exception ignore) {
                }
                barcodeDetailsMap = (HashMap<String, ProductBarcodeDetailsDAO>) this.getSessionMap().get("barcodeProductDetails");
            }
            if(barcodeDetailsMap!=null && this.getProductCode()!=null){
                ProductBarcodeDetailsDAO d = barcodeDetailsMap.get(this.getProductCode().trim());
                if(d!=null){
                    String al = d.getProductAlias() != null ? d.getProductAlias().trim() : "";
                    if (al.isEmpty()) {
                        al = "NA";
                    }
                    this.setCompositeProductName(
                            d.getProductId() + "-" + d.getProductNameEn() + "-" + al);
                    this.setProductName(d.getProductNameEn());
                    this.setProductNameMr(d.getProductNameMr());
                    this.setSalesUnit(d.getSalesUnit());
                    this.setTaxId(String.valueOf(d.getTaxId()));
                    this.setSalesRate(d.getBarcodeMRP());
                    this.setAmount(d.getBarcodeMRP());
                    this.setAmountAfterDiscount(d.getSalesPrice());
                    this.setQuantity(d.getBarCodeQuantity());
                }
            }
        }catch(Exception ignore){}
        return SUCCESS;
    }

    public void setSession(java.util.Map<String, Object> map) {  sessionMap=(SessionMap<String, Object>)map; }
    public SessionMap<String, Object> getSessionMap() { return sessionMap; }

    public String getProductCode() { return productCode; }
    public void setProductCode(String productCode) { this.productCode = productCode; }

    public String getCompositeProductName() { return compositeProductName; }
    public void setCompositeProductName(String compositeProductName) { this.compositeProductName = compositeProductName; }

    public String getProductName() { return productName; }
    public void setProductName(String productName) { this.productName = productName; }
    public String getProductNameMr() { return productNameMr; }
    public void setProductNameMr(String productNameMr) { this.productNameMr = productNameMr; }
    public String getSalesUnit() { return salesUnit; }
    public void setSalesUnit(String salesUnit) { this.salesUnit = salesUnit; }
    public String getTaxId() { return taxId; }
    public void setTaxId(String taxId) { this.taxId = taxId; }
    public Float getSalesRate() { return salesRate; }
    public void setSalesRate(Float salesRate) { this.salesRate = salesRate; }
    public Float getAmount() { return amount; }
    public void setAmount(Float amount) { this.amount = amount; }
    public Float getAmountAfterDiscount() { return amountAfterDiscount; }
    public void setAmountAfterDiscount(Float amountAfterDiscount) { this.amountAfterDiscount = amountAfterDiscount; }
    public Float getQuantity() { return quantity; }
    public void setQuantity(Float quantity) { this.quantity = quantity; }
}


