package com.sabre.apd.mi.smartgwt.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.i18n.client.Messages;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Image;
import com.isomorphic.datasource.DSRequest;
import com.isomorphic.datasource.DSResponse;
import com.sabre.apd.mi.smartgwt.client.layout.Banner;
import com.sabre.apd.mi.smartgwt.client.layout.NavigationToolBar;
import com.sabre.apd.mi.smartgwt.client.layout.ReportPane;
import com.sabre.apd.mi.smartgwt.client.layout.SearchCriteriaPane;
import com.sabre.apd.mi.smartgwt.server.cache.CacheManager;
import com.smartgwt.client.core.KeyIdentifier;
import com.smartgwt.client.data.Criteria;
import com.smartgwt.client.data.DSCallback;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.ImageStyle;
import com.smartgwt.client.types.VerticalAlignment;
import com.smartgwt.client.util.KeyCallback;
import com.smartgwt.client.util.Page;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.*;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.FormItemIfFunction;
import com.smartgwt.client.widgets.form.fields.*;
import com.smartgwt.client.widgets.form.fields.events.*;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.tab.Tab;
import com.smartgwt.client.widgets.tab.TabSet;
import org.apache.poi.xwpf.usermodel.VerticalAlign;
import org.omg.CORBA.PUBLIC_MEMBER;
import javax.print.DocFlavor;
import javax.validation.constraints.Null;
import java.awt.*;
import java.util.*;
import java.util.List;


/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class MarketIntelligence implements EntryPoint {

    private final Messages messages = GWT.create(Messages.class);

    /**
     * This is the entry point method.
     */
    public void onModuleLoad() {


        KeyIdentifier debugKey = new KeyIdentifier();
        debugKey.setCtrlKey(true);
        debugKey.setKeyName("D");
        Page.registerKey(debugKey, new KeyCallback() {
            public void execute(String keyName) {
                SC.showConsole();
            }
        });

               final DynamicForm form = new DynamicForm();
               form.setWidth(500);
               form.setNumCols(6);
        
                final SelectItem DateRangeItem = new SelectItem();
                DateRangeItem.setName("DateRange");
                DateRangeItem.setTitle("DateRange");
                DateRangeItem.setValueMap("Monthly", "Quartely", "Yearly");
                DateRangeItem.setDefaultToFirstOption(true);
                final SelectItem fromYear = new SelectItem();
                fromYear.setName("fromYear");
                fromYear.setPickListWidth(210);
                fromYear.setTitle("fromYear");
                final  SelectItem fromMonth = new SelectItem() ;
                fromMonth.setName("fromMonth");
                fromMonth.setTitle("fromMonth");

            String selectedDateRangeType   =(String) DateRangeItem.getValue() ;
              if(selectedDateRangeType == null)
                selectedDateRangeType =  "Monthly";

            FillDateRange(fromYear,fromMonth,selectedDateRangeType,false)   ;

            DateRangeItem.addChangeHandler(new ChangeHandler() {
            public void onChange(ChangeEvent event) {
                String selectedDateRangeType = (String) event.getValue();

                     FillDateRange(fromYear,fromMonth,selectedDateRangeType,false)   ;
            }
            });

             fromYear.addChangeHandler(new ChangeHandler() {
            //@Override
            public void  onChange(ChangeEvent event) {
                 
                String selectedDateRangeType   =(String) DateRangeItem.getValue() ;
                FillDateRange(fromYear,fromMonth,selectedDateRangeType,true)   ;
                  }
                  });
                  form.setItems(DateRangeItem,fromYear,fromMonth);
                  form.draw();
    }
    public void FillDateRange(final SelectItem fromYear,final SelectItem fromMonth,final String selectedDateRangeType,final boolean blnPullFromSI)
    {
                DataSource dS = DataSource.get("dateRange");
                Criteria criteria = new Criteria();
                criteria.addCriteria("PRODUCT_ID","SA");
                criteria.addCriteria("CLIENT_ID", 99);
                com.smartgwt.client.data.DSRequest dSR = new com.smartgwt.client.data.DSRequest();
                dSR.setOperationId("yearQuarterMonthFetch");
                dS.fetchData(criteria, new DSCallback() {
                @Override
                public void execute(com.smartgwt.client.data.DSResponse dsResponse, Object o, com.smartgwt.client.data.DSRequest dsRequest) {
                     Record[] recordDR = dsResponse.getData();
                     String selectedYear = null;
                     if(blnPullFromSI)
                      selectedYear= fromYear.getValue().toString();
                     else
                     selectedYear =   fillYears(recordDR,"",fromYear);
                     getMonths(recordDR,selectedDateRangeType,fromMonth,selectedYear);
                 }
             }, dSR);
    }

    public String fillYears(Record[] recordDR,String selectedDateRangeType ,SelectItem selectItem)
    {
       LinkedHashMap<String, String> yearList = new LinkedHashMap<String, String>();
       for (Record i : recordDR)
       {
           yearList.put(i.getAttributeAsString("YEAR_NUM"),i.getAttributeAsString("YEAR_NUM"));
       }
       selectItem.setValueMap(yearList);
       if(recordDR.length>0)
       selectItem.setValue( recordDR[0].getAttributeAsString("YEAR_NUM"));
       return  recordDR[0].getAttributeAsString("YEAR_NUM");
    }

     public void getMonths(Record[] recordDR,String selectedDateRangeType ,SelectItem selectItem,String selectedYear)
    {
        selectItem.clearValue();
        LinkedHashMap<String, String> monthList = new LinkedHashMap<String, String>();
        int cnt = 0; boolean flag= true;
       String StrColumn = null;
       if(selectedDateRangeType.equalsIgnoreCase("Monthly"))
           StrColumn ="MONTH_SHORT_NAME";
       if(selectedDateRangeType.equalsIgnoreCase("Quartely"))
           StrColumn ="QU_QUARTER_SHORT_NAME";//"QU_QUARTER_SHORT_NAME";
       if(selectedDateRangeType.equalsIgnoreCase("Yearly"))
           StrColumn="QU_QUARTER_SHORT_NAME";
        String strTemp = null;
        for (Record i : recordDR)
        {
            if(flag)
            cnt++;
            String b =  i.getAttributeAsString("YEAR_NUM");
            if(b.equalsIgnoreCase(selectedYear))
            {

                  monthList.put(i.getAttribute(StrColumn),i.getAttributeAsString(StrColumn));
                  flag= false;
            }
        }
       selectItem.setValueMap( monthList);
       if(recordDR.length>0)
        selectItem.setValue( recordDR[cnt-1].getAttributeAsString(StrColumn));

    }

    public  void  GetDateRange( final SelectItem fromYear )
    {
             
           DataSource dS = DataSource.get("dateRange");
           Criteria criteria = new Criteria();
           criteria.addCriteria("PRODUCT_ID","SA");
           criteria.addCriteria("CLIENT_ID", 99);
           com.smartgwt.client.data.DSRequest dSR = new com.smartgwt.client.data.DSRequest();
           dSR.setOperationId("yearQuarterMonthFetch");
           dS.fetchData(criteria, new DSCallback() {
            @Override
            public void execute(com.smartgwt.client.data.DSResponse dsResponse, Object o, com.smartgwt.client.data.DSRequest dsRequest) {
                Record[] recordDR = dsResponse.getData();
                fillYears(recordDR,"",fromYear);
            }
        }, dSR);

    }



}
