package ibase.client;
import java.util.ArrayList;
import java.util.Date;
import com.google.gwt.core.client.Callback;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONValue;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.xml.client.Document;
import com.google.gwt.xml.client.Node;
import com.google.gwt.xml.client.NodeList;
import com.google.gwt.xml.client.XMLParser;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */

public class GWTDashBoardReport implements EntryPoint
{
	ArrayList<String> parameterList = new ArrayList<String>();
	ArrayList<FilterInfo> filterList = new ArrayList<FilterInfo>();
	HorizontalPanel hpanel= new HorizontalPanel();
	String metadataname = "";
	// added by swati 30 dec 2014 for subtitle 
	public static String subTitle="";
	public static boolean  flag= false;
	// ended by swati 30 dec 2014 for subtitle 
	@SuppressWarnings("unchecked")
	public void onModuleLoad()
		{
		metadataname = Window.Location.getParameter("metadataname") == null ? "" : Window.Location.getParameter("metadataname");
		// added by swati 30 dec 2014 for subtitle 
			String temp = Window.Location.getHref();
			subTitle = temp.substring(temp.lastIndexOf('=')+1, temp.length());		
			if(subTitle.trim()!=(metadataname.trim()))
			{
				flag = true;
			}
		// ended by swati 30 dec 2014 for subtitle 
			if (metadataname.trim().length() != 0)
			{
	
				parameterList.add(metadataname);
				E12DashBoardPage.getJsonForFilterData(new Callback<JSONObject, String>()
				{
					@Override
					public void onSuccess(JSONObject jsnObj)
					{
						JSONValue jsonValue = jsnObj.get("metaData");
						if (jsonValue.toString().length() != 2)
						{
							getParameterList(jsnObj);
						}else
						{
							Window.alert("MetaData Should Be Proper");
						}
					}
	
					@Override
					public void onFailure(String result)
					{
						Window.alert("Exception :: onModuleLoad() [" + result + "]");
					}
				}, metadataname);
			} else
			{
				Window.alert("Please Enter Metadata name");
			}
		}
		
		public void getParameterList(JSONObject metaDataJsnObj)
		{
            Date date = new Date();
			MetaDataDtl dataDtl = new MetaDataDtl();
			filterList          =dataDtl.getFilters(metaDataJsnObj);
			int year=0;
			String month="";
			String monthYear="";
			year= 1900+date.getYear();
			month=String.valueOf(date.getMonth()+1);
            if(month.trim().length()==1)
            {
            	month="0"+month;
            }
            monthYear=year+month;
            if(filterList.size()>0)
            {
				for (FilterInfo filterInfo : filterList)
				{
					if(filterInfo.getColNameId().equals("PRD_SALESDASH"))
					{
						parameterList.add(Window.Location.getParameter(filterInfo.getColNameId()) == null ? monthYear : Window.Location.getParameter(filterInfo.getColNameId()));
					}
					else
					{
						parameterList.add(Window.Location.getParameter(filterInfo.getColNameId()) == null ? "" : Window.Location.getParameter(filterInfo.getColNameId()));
					}
				}
            }
            else
            {
            	parameterList.add(Window.Location.getParameter("metadataname"));
            }
            parameterList.add(this.getDefaultView(metaDataJsnObj));
            E12DashBoardPage e12DashBoardPage = new E12DashBoardPage(parameterList,metaDataJsnObj);
		    RootPanel.get("filterDiv").add(e12DashBoardPage);
		}
		public String getDefaultView(JSONObject metaDataJsnObj)
		{
				String xmlMetadata = null;
				Document document = null;
				NodeList rootNodeList = null;
				Node rootNode = null;
				String defaultView = "";
				String id = "";
				try
				{
					xmlMetadata = metaDataJsnObj.get("metaData").toString();
					xmlMetadata = metaDataJsnObj.get("metaData").toString().replace("\\", "").trim();
					xmlMetadata = xmlMetadata.replace('"', ' ').trim();
					document = XMLParser.parse(xmlMetadata);
					XMLParser.removeWhitespace(document);
					if (document != null)
					{
						rootNodeList = document.getElementsByTagName("Root").item(0).getChildNodes();
						for (int rootCtr = 0; rootCtr < rootNodeList.getLength(); rootCtr++)
						{
							rootNode = rootNodeList.item(rootCtr);
							if ("view".equals(rootNode.getNodeName()))
							{
								id = rootNode.getAttributes().getNamedItem("id").getNodeValue();
								if (id.equalsIgnoreCase("1"))
								{
									defaultView = rootNode.getAttributes().getNamedItem("name").getNodeValue();
									break;
								}
							}
						}
					}
				} catch (Exception e)
				{
					Window.alert("Exception in Class GWTDashBoardReport:()getDefaultView" + e.getMessage());
				}
				return defaultView;
			}
}
