package in.techware.carrefour.activity.it;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;

import in.techware.carrefour.R;
import in.techware.carrefour.activity.AuthenticateActivity;
import in.techware.carrefour.activity.BaseAppCompatNoDrawerActivity;
import in.techware.carrefour.activity.FileTransferActivity;
import in.techware.carrefour.adapters.MenuListRecyclerAdapter;
import in.techware.carrefour.dbUtils.repositories.FacingRepository;
import in.techware.carrefour.dbUtils.repositories.InternalSalesCorrectionRepository;
import in.techware.carrefour.dbUtils.repositories.InternalSalesRepository;
import in.techware.carrefour.dbUtils.repositories.ItemListRepository;
import in.techware.carrefour.dbUtils.repositories.LabelSurveyRepository;
import in.techware.carrefour.dbUtils.repositories.MinStockRepository;
import in.techware.carrefour.dbUtils.repositories.OutOfStockRepository;
import in.techware.carrefour.dbUtils.repositories.PromotionRepository;
import in.techware.carrefour.dbUtils.repositories.StockTakeRepository;
import in.techware.carrefour.dbUtils.repositories.TransformQuantityRepository;
import in.techware.carrefour.dbUtils.repositories.WasteRepository;
import in.techware.carrefour.dialogs.ConfirmationDialog;
import in.techware.carrefour.model.FacingBean;
import in.techware.carrefour.model.InternalSalesBean;
import in.techware.carrefour.model.InternalSalesCorrectionBean;
import in.techware.carrefour.model.ItemListBean;
import in.techware.carrefour.model.LabelSurveyBean;
import in.techware.carrefour.model.MenuBean;
import in.techware.carrefour.model.MenuListBean;
import in.techware.carrefour.model.MinStockBean;
import in.techware.carrefour.model.OutOfStockBean;
import in.techware.carrefour.model.PromotionBean;
import in.techware.carrefour.model.StockTakeBean;
import in.techware.carrefour.model.TransformQuantityBean;
import in.techware.carrefour.model.WasteBean;
import in.techware.carrefour.util.AppConstants;
import in.techware.carrefour.util.menuUtils.ITMenuUtil;

public class ITActivity extends BaseAppCompatNoDrawerActivity {

    private RecyclerView rvIT;
    private ConfirmationDialog confirmationDialog;
    private ConfirmationDialog.ConfirmationDialogActionListener confirmationDialogListener;
    private MenuListRecyclerAdapter adapter;
    private MenuListBean menuListBean;
    private MenuBean selectedSubMenuBean;
    private MenuBean selectedMenuBean;
    private WasteRepository wasteRepository;
    private WasteRepository.WasteRepositoryListener wasteRepositoryListener;
    private LabelSurveyRepository labelSurveyRepository;
    private LabelSurveyRepository.LabelSurveyRepositoryListener labelSurveyRepositoryListener;
    private StockTakeRepository stockTakeRepository;
    private StockTakeRepository.StockTakeRepositoryListener stockTakeRepositoryListener;
    private MinStockRepository minStockRepository;
    private MinStockRepository.MinStockRepositoryListener minStockRepositoryListener;
    private FacingRepository facingRepository;
    private FacingRepository.FacingRepositoryListener facingRepositoryListener;
    private PromotionRepository promotionRepository;
    private PromotionRepository.PromotionRepositoryListener promotionRepositoryListener;
    private OutOfStockRepository outOfStockRepository;
    private OutOfStockRepository.OutOfStockRepositoryListener outOfStockRepositoryListener;
    private TransformQuantityRepository transformQuantityRepository;
    private TransformQuantityRepository.TransformQuantityRepositoryListener transformQuantityRepositoryListener;
    private ItemListRepository itemListRepository;
    private ItemListRepository.ItemListRepositoryListener itemListRepositoryListener;
    private InternalSalesRepository internalSalesRepository;
    private InternalSalesRepository.InternalSalesRepositoryListener internalSalesRepositoryListener;
    private InternalSalesCorrectionRepository internalSalesCorrectionRepository;
    private InternalSalesCorrectionRepository.InternalSalesCorrectionRepositoryListener internalSalesCorrectionRepositoryListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_sub_menu);

        /**
         * If menu items are dynamic
         */

        setContentView(R.layout.activity_it);

        if (getIntent().hasExtra("menuBean")) {
            selectedMenuBean = (MenuBean) getIntent().getSerializableExtra("menuBean");
        }

        initViews();

        if (getSupportActionBar() != null) {
            setTitle(R.string.title_it, R.color.white);
            getSupportActionBar().setTitle("");
            getSupportActionBar().setHomeButtonEnabled(true);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowTitleEnabled(true);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == REQ_FILE_TRANSFER && resultCode == RESULT_OK) {
            if (data != null && data.hasExtra("result")) {
                if (data.getIntExtra("result", 0) == AppConstants.FILE_TRANSFER_RESULT_SEND_DATA_COMPLETED
                        || data.getIntExtra("result", 0) == AppConstants.FILE_TRANSFER_RESULT_CONTINUE) {
                    if (selectedSubMenuBean != null) {
                        switch (selectedSubMenuBean.getCode()) {
                            case ITMenuUtil.CODE_WASTE:
                                navigateToWasteActivity();
                                break;
                            case ITMenuUtil.CODE_LABEL_SURVEY:
                                navigateToLabelSurveyActivity();
                                break;
                            case ITMenuUtil.CODE_STOCK_TAKE:
                                navigateToStockTakeActivity();
                                break;
                            case ITMenuUtil.CODE_MIN_STOCK:
                                navigateToMinStockActivity();
                                break;
                            case ITMenuUtil.CODE_FACING:
                                navigateToFacingActivity();
                                break;
                            case ITMenuUtil.CODE_PROMOTION:
                                navigateToPromotionActivity();
                                break;
                            case ITMenuUtil.CODE_OUT_OF_STOCK:
                                navigateToOutOfStockActivity();
                                break;
                            case ITMenuUtil.CODE_TRANSFORM_QUANTITY:
                                navigateToTransformQuantityActivity();
                                break;
                            case ITMenuUtil.CODE_ITEM_LIST:
                                navigateToItemListActivity();
                                break;
                            case ITMenuUtil.CODE_INTERNAL_SALES:
                                navigateToInternalSalesActivity();
                                break;
                            case ITMenuUtil.CODE_INTERNAL_SALES_CORRECT:
                                navigateToInternalSalesCorrectionActivity();
                                break;
                        }
                    }
                }
            }
        }
        if (requestCode == REQ_AUTHENTICATE && resultCode == RESULT_OK) {
            if (data != null && data.hasExtra("result")) {
                if (data.getIntExtra("result", 0) == AppConstants.AUTHENTICATION_RESULT_SUCCESS) {
                    if (selectedSubMenuBean != null) {
                        switch (selectedSubMenuBean.getCode()) {
                            case ITMenuUtil.CODE_RESTORE:
                                navigateToITRestoreActivity();
                                break;
                        }
                    }
                }
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();

        menuListBean = ITMenuUtil.getAllMenu();
        populateMenu();
    }

    private void initViews() {

        rvIT = findViewById(R.id.rv_it);
        rvIT.setHasFixedSize(true);
        rvIT.setLayoutManager(new GridLayoutManager(getApplicationContext(), 2));

        initWasteRepository();
        initLabelSurveyRepository();
        initStockTakeRepository();
        initMinStockRepository();
        initFacingRepository();
        initPromotionRepository();
        initOutOfStockRepository();
        initTransformQuantityRepository();
        initItemListRepository();
        initInternalSalesRepository();
        initInternalSalesCorrectionRepository();

        confirmationDialogListener = new ConfirmationDialog.ConfirmationDialogActionListener() {
            @Override
            public void onYesClicked() {

                if (selectedSubMenuBean != null) {
                    switch (selectedSubMenuBean.getCode()) {
                        case ITMenuUtil.CODE_WASTE:
                            wasteRepository.deleteAllUnsentWastes(wasteRepositoryListener);
                            navigateToWasteActivity();
                            break;
                        case ITMenuUtil.CODE_LABEL_SURVEY:
                            labelSurveyRepository.deleteAllUnsentLabelSurveys(labelSurveyRepositoryListener);
                            navigateToWasteActivity();
                            break;
                        case ITMenuUtil.CODE_STOCK_TAKE:
                            stockTakeRepository.deleteAllUnsentStockTakes(stockTakeRepositoryListener);
                            navigateToWasteActivity();
                            break;
                        case ITMenuUtil.CODE_MIN_STOCK:
                            minStockRepository.deleteAllUnsentMinStocks(minStockRepositoryListener);
                            navigateToWasteActivity();
                            break;
                        case ITMenuUtil.CODE_FACING:
                            facingRepository.deleteAllUnsentFacings(facingRepositoryListener);
                            navigateToWasteActivity();
                            break;
                        case ITMenuUtil.CODE_PROMOTION:
                            promotionRepository.deleteAllUnsentPromotions(promotionRepositoryListener);
                            navigateToWasteActivity();
                            break;
                        case ITMenuUtil.CODE_OUT_OF_STOCK:
                            outOfStockRepository.deleteAllUnsentOutOfStocks(outOfStockRepositoryListener);
                            navigateToWasteActivity();
                            break;
                        case ITMenuUtil.CODE_TRANSFORM_QUANTITY:
                            transformQuantityRepository.deleteAllUnsentTransformQuantitys(transformQuantityRepositoryListener);
                            navigateToWasteActivity();
                            break;
                        case ITMenuUtil.CODE_ITEM_LIST:
                            itemListRepository.deleteAllUnsentItemLists(itemListRepositoryListener);
                            navigateToWasteActivity();
                            break;
                        case ITMenuUtil.CODE_INTERNAL_SALES:
                            internalSalesRepository.deleteAllUnsentInternalSales(internalSalesRepositoryListener);
                            navigateToWasteActivity();
                            break;
                        case ITMenuUtil.CODE_INTERNAL_SALES_CORRECT:
                            internalSalesCorrectionRepository.deleteAllUnsentInternalSalesCorrection(internalSalesCorrectionRepositoryListener);
                            navigateToWasteActivity();
                            break;
                    }
                }

            }

            @Override
            public void onNoClicked() {
                if (selectedSubMenuBean != null) {
                    switch (selectedSubMenuBean.getCode()) {
                        case ITMenuUtil.CODE_WASTE:
                        case ITMenuUtil.CODE_LABEL_SURVEY:
                        case ITMenuUtil.CODE_STOCK_TAKE:
                        case ITMenuUtil.CODE_MIN_STOCK:
                        case ITMenuUtil.CODE_FACING:
                        case ITMenuUtil.CODE_PROMOTION:
                        case ITMenuUtil.CODE_OUT_OF_STOCK:
                        case ITMenuUtil.CODE_TRANSFORM_QUANTITY:
                        case ITMenuUtil.CODE_ITEM_LIST:
                        case ITMenuUtil.CODE_INTERNAL_SALES:
                        case ITMenuUtil.CODE_INTERNAL_SALES_CORRECT:
                            startActivityForResult(new Intent(getApplicationContext(), FileTransferActivity.class)
                                    .putExtra("menuBean", selectedMenuBean)
                                    .putExtra("subMenuBean", selectedSubMenuBean), REQ_FILE_TRANSFER);
                            break;
                    }
                }
            }
        };
    }

    private void initWasteRepository() {
        wasteRepository = WasteRepository.getInstance();

        wasteRepositoryListener = new WasteRepository.WasteRepositoryListener() {

            @Override
            public void onAddCompleted(WasteBean wasteBean) {

            }

            @Override
            public void onAllListLoaded(ArrayList<WasteBean> wasteList) {

            }

            @Override
            public void onAllSentListLoaded(ArrayList<WasteBean> wasteList) {

            }

            @Override
            public void onAllUnsentListLoaded(ArrayList<WasteBean> wasteList) {
                if (wasteList != null && !wasteList.isEmpty()) {
                    showConfirmationDialog();
                } else {
                    navigateToWasteActivity();
                }
            }

            @Override
            public void onDeleteAllCompleted() {

            }

            @Override
            public void onDeleteAllSentCompleted() {

            }

            @Override
            public void onDeleteAllUnsentCompleted() {

            }

            @Override
            public void onDeleteCompleted(WasteBean wasteBean) {

            }

            @Override
            public void onFailed() {

            }

            @Override
            public void onUpdateCompleted(WasteBean wasteBean) {

            }

            @Override
            public void onWasteBeanLoaded(WasteBean wasteBean) {

            }
        };

    }

    private void initLabelSurveyRepository() {

        labelSurveyRepository = LabelSurveyRepository.getInstance();

        labelSurveyRepositoryListener = new LabelSurveyRepository.LabelSurveyRepositoryListener() {
            @Override
            public void onAddCompleted(LabelSurveyBean labelSurveyBean) {

            }

            @Override
            public void onAllListLoaded(ArrayList<LabelSurveyBean> labelSurveyList) {

            }

            @Override
            public void onAllSentListLoaded(ArrayList<LabelSurveyBean> labelSurveyList) {

            }

            @Override
            public void onAllUnsentListLoaded(ArrayList<LabelSurveyBean> labelSurveyList) {
                if (labelSurveyList != null && !labelSurveyList.isEmpty()) {
                    showConfirmationDialog();
                } else {
                    navigateToLabelSurveyActivity();
                }
            }

            @Override
            public void onDeleteAllCompleted() {

            }

            @Override
            public void onDeleteAllSentCompleted() {

            }

            @Override
            public void onDeleteAllUnsentCompleted() {

            }

            @Override
            public void onDeleteCompleted(LabelSurveyBean labelSurveyBean) {

            }

            @Override
            public void onFailed() {

            }

            @Override
            public void onUpdateCompleted(LabelSurveyBean labelSurveyBean) {

            }

            @Override
            public void onLabelSurveyBeanLoaded(LabelSurveyBean labelSurveyBean) {

            }
        };

    }

    private void initStockTakeRepository() {

        stockTakeRepository = StockTakeRepository.getInstance();

        stockTakeRepositoryListener = new StockTakeRepository.StockTakeRepositoryListener() {
            @Override
            public void onAddCompleted(StockTakeBean stockTakeBean) {

            }

            @Override
            public void onAllListLoaded(ArrayList<StockTakeBean> stockTakeList) {

            }

            @Override
            public void onAllSentListLoaded(ArrayList<StockTakeBean> stockTakeList) {

            }

            @Override
            public void onAllUnsentListLoaded(ArrayList<StockTakeBean> stockTakeList) {
                if (stockTakeList != null && !stockTakeList.isEmpty()) {
                    showConfirmationDialog();
                } else {
                    navigateToStockTakeActivity();
                }
            }

            @Override
            public void onDeleteAllCompleted() {

            }

            @Override
            public void onDeleteAllSentCompleted() {

            }

            @Override
            public void onDeleteAllUnsentCompleted() {

            }

            @Override
            public void onDeleteCompleted(StockTakeBean stockTakeBean) {

            }

            @Override
            public void onFailed() {

            }

            @Override
            public void onUpdateCompleted(StockTakeBean stockTakeBean) {

            }

            @Override
            public void onStockTakeBeanLoaded(StockTakeBean stockTakeBean) {

            }
        };

    }

    private void initMinStockRepository() {

        minStockRepository = MinStockRepository.getInstance();

        minStockRepositoryListener = new MinStockRepository.MinStockRepositoryListener() {
            @Override
            public void onAddCompleted(MinStockBean minStockBean) {

            }

            @Override
            public void onAllListLoaded(ArrayList<MinStockBean> minStockList) {

            }

            @Override
            public void onAllSentListLoaded(ArrayList<MinStockBean> minStockList) {

            }

            @Override
            public void onAllUnsentListLoaded(ArrayList<MinStockBean> minStockList) {
                if (minStockList != null && !minStockList.isEmpty()) {
                    showConfirmationDialog();
                } else {
                    navigateToMinStockActivity();
                }
            }

            @Override
            public void onDeleteAllCompleted() {

            }

            @Override
            public void onDeleteAllSentCompleted() {

            }

            @Override
            public void onDeleteAllUnsentCompleted() {

            }

            @Override
            public void onDeleteCompleted(MinStockBean minStockBean) {

            }

            @Override
            public void onFailed() {

            }

            @Override
            public void onUpdateCompleted(MinStockBean minStockBean) {

            }

            @Override
            public void onMinStockBeanLoaded(MinStockBean minStockBean) {

            }
        };

    }

    private void initFacingRepository() {

        facingRepository = FacingRepository.getInstance();

        facingRepositoryListener = new FacingRepository.FacingRepositoryListener() {
            @Override
            public void onAddCompleted(FacingBean facingBean) {

            }

            @Override
            public void onAllListLoaded(ArrayList<FacingBean> facingList) {

            }

            @Override
            public void onAllSentListLoaded(ArrayList<FacingBean> facingList) {

            }

            @Override
            public void onAllUnsentListLoaded(ArrayList<FacingBean> facingList) {
                if (facingList != null && !facingList.isEmpty()) {
                    showConfirmationDialog();
                } else {
                    navigateToFacingActivity();
                }
            }

            @Override
            public void onDeleteAllCompleted() {

            }

            @Override
            public void onDeleteAllSentCompleted() {

            }

            @Override
            public void onDeleteAllUnsentCompleted() {

            }

            @Override
            public void onDeleteCompleted(FacingBean facingBean) {

            }

            @Override
            public void onFailed() {

            }

            @Override
            public void onUpdateCompleted(FacingBean facingBean) {

            }

            @Override
            public void onFacingBeanLoaded(FacingBean facingBean) {

            }
        };

    }

    private void initPromotionRepository() {

        promotionRepository = PromotionRepository.getInstance();

        promotionRepositoryListener = new PromotionRepository.PromotionRepositoryListener() {
            @Override
            public void onAddCompleted(PromotionBean promotionBean) {

            }

            @Override
            public void onAllListLoaded(ArrayList<PromotionBean> promotionList) {

            }

            @Override
            public void onAllSentListLoaded(ArrayList<PromotionBean> promotionList) {

            }

            @Override
            public void onAllUnsentListLoaded(ArrayList<PromotionBean> promotionList) {
                if (promotionList != null && !promotionList.isEmpty()) {
                    showConfirmationDialog();
                } else {
                    navigateToPromotionActivity();
                }
            }

            @Override
            public void onDeleteAllCompleted() {

            }

            @Override
            public void onDeleteAllSentCompleted() {

            }

            @Override
            public void onDeleteAllUnsentCompleted() {

            }

            @Override
            public void onDeleteCompleted(PromotionBean promotionBean) {

            }

            @Override
            public void onFailed() {

            }

            @Override
            public void onUpdateCompleted(PromotionBean promotionBean) {

            }

            @Override
            public void onPromotionBeanLoaded(PromotionBean promotionBean) {

            }
        };

    }

    private void initOutOfStockRepository() {

        outOfStockRepository = OutOfStockRepository.getInstance();

        outOfStockRepositoryListener = new OutOfStockRepository.OutOfStockRepositoryListener() {
            @Override
            public void onAddCompleted(OutOfStockBean outOfStockBean) {

            }

            @Override
            public void onAllListLoaded(ArrayList<OutOfStockBean> outOfStockList) {

            }

            @Override
            public void onAllSentListLoaded(ArrayList<OutOfStockBean> outOfStockList) {

            }

            @Override
            public void onAllUnsentListLoaded(ArrayList<OutOfStockBean> outOfStockList) {
                if (outOfStockList != null && !outOfStockList.isEmpty()) {
                    showConfirmationDialog();
                } else {
                    navigateToOutOfStockActivity();
                }
            }

            @Override
            public void onDeleteAllCompleted() {

            }

            @Override
            public void onDeleteAllSentCompleted() {

            }

            @Override
            public void onDeleteAllUnsentCompleted() {

            }

            @Override
            public void onDeleteCompleted(OutOfStockBean outOfStockBean) {

            }

            @Override
            public void onFailed() {

            }

            @Override
            public void onUpdateCompleted(OutOfStockBean outOfStockBean) {

            }

            @Override
            public void onOutOfStockBeanLoaded(OutOfStockBean outOfStockBean) {

            }
        };

    }

    private void initTransformQuantityRepository() {

        transformQuantityRepository = TransformQuantityRepository.getInstance();

        transformQuantityRepositoryListener = new TransformQuantityRepository.TransformQuantityRepositoryListener() {
            @Override
            public void onAddCompleted(TransformQuantityBean transformQuantityBean) {

            }

            @Override
            public void onAllListLoaded(ArrayList<TransformQuantityBean> transformQuantityList) {

            }

            @Override
            public void onAllSentListLoaded(ArrayList<TransformQuantityBean> transformQuantityList) {

            }

            @Override
            public void onAllUnsentListLoaded(ArrayList<TransformQuantityBean> transformQuantityList) {
                if (transformQuantityList != null && !transformQuantityList.isEmpty()) {
                    showConfirmationDialog();
                } else {
                    navigateToTransformQuantityActivity();
                }
            }

            @Override
            public void onDeleteAllCompleted() {

            }

            @Override
            public void onDeleteAllSentCompleted() {

            }

            @Override
            public void onDeleteAllUnsentCompleted() {

            }

            @Override
            public void onDeleteCompleted(TransformQuantityBean transformQuantityBean) {

            }

            @Override
            public void onFailed() {

            }

            @Override
            public void onUpdateCompleted(TransformQuantityBean transformQuantityBean) {

            }

            @Override
            public void onTransformQuantityBeanLoaded(TransformQuantityBean transformQuantityBean) {

            }
        };

    }

    private void initItemListRepository() {

        itemListRepository = ItemListRepository.getInstance();

        itemListRepositoryListener = new ItemListRepository.ItemListRepositoryListener() {
            @Override
            public void onAddCompleted(ItemListBean itemListBean) {

            }

            @Override
            public void onAllListLoaded(ArrayList<ItemListBean> itemListList) {

            }

            @Override
            public void onAllSentListLoaded(ArrayList<ItemListBean> itemListList) {

            }

            @Override
            public void onAllUnsentListLoaded(ArrayList<ItemListBean> itemListList) {
                if (itemListList != null && !itemListList.isEmpty()) {
                    showConfirmationDialog();
                } else {
                    navigateToItemListActivity();
                }
            }

            @Override
            public void onDeleteAllCompleted() {

            }

            @Override
            public void onDeleteAllSentCompleted() {

            }

            @Override
            public void onDeleteAllUnsentCompleted() {

            }

            @Override
            public void onDeleteCompleted(ItemListBean itemListBean) {

            }

            @Override
            public void onFailed() {

            }

            @Override
            public void onUpdateCompleted(ItemListBean itemListBean) {

            }

            @Override
            public void onItemListBeanLoaded(ItemListBean itemListBean) {

            }
        };

    }

    private void initInternalSalesRepository() {

        internalSalesRepository = InternalSalesRepository.getInstance();

        internalSalesRepositoryListener = new InternalSalesRepository.InternalSalesRepositoryListener() {
            @Override
            public void onAddCompleted(InternalSalesBean internalSalesBean) {

            }

            @Override
            public void onAllListLoaded(ArrayList<InternalSalesBean> internalSalesList) {

            }

            @Override
            public void onAllSentListLoaded(ArrayList<InternalSalesBean> internalSalesList) {

            }

            @Override
            public void onAllUnsentListLoaded(ArrayList<InternalSalesBean> internalSalesList) {
                if (internalSalesList != null && !internalSalesList.isEmpty()) {
                    showConfirmationDialog();
                } else {
                    navigateToInternalSalesActivity();
                }
            }

            @Override
            public void onDeleteAllCompleted() {

            }

            @Override
            public void onDeleteAllSentCompleted() {

            }

            @Override
            public void onDeleteAllUnsentCompleted() {

            }

            @Override
            public void onDeleteCompleted(InternalSalesBean internalSalesBean) {

            }

            @Override
            public void onFailed() {

            }

            @Override
            public void onUpdateCompleted(InternalSalesBean internalSalesBean) {

            }

            @Override
            public void onInternalSalesBeanLoaded(InternalSalesBean internalSalesBean) {

            }
        };

    }

    private void initInternalSalesCorrectionRepository() {

        internalSalesCorrectionRepository = InternalSalesCorrectionRepository.getInstance();

        internalSalesCorrectionRepositoryListener = new InternalSalesCorrectionRepository.InternalSalesCorrectionRepositoryListener() {
            @Override
            public void onAddCompleted(InternalSalesCorrectionBean internalSalesCorrectionBean) {

            }

            @Override
            public void onAllListLoaded(ArrayList<InternalSalesCorrectionBean> internalSalesCorrectionList) {

            }

            @Override
            public void onAllSentListLoaded(ArrayList<InternalSalesCorrectionBean> internalSalesCorrectionList) {

            }

            @Override
            public void onAllUnsentListLoaded(ArrayList<InternalSalesCorrectionBean> internalSalesCorrectionList) {
                if (internalSalesCorrectionList != null && !internalSalesCorrectionList.isEmpty()) {
                    showConfirmationDialog();
                } else {
                    navigateToInternalSalesCorrectionActivity();
                }
            }

            @Override
            public void onDeleteAllCompleted() {

            }

            @Override
            public void onDeleteAllSentCompleted() {

            }

            @Override
            public void onDeleteAllUnsentCompleted() {

            }

            @Override
            public void onDeleteCompleted(InternalSalesCorrectionBean internalSalesCorrectionBean) {

            }

            @Override
            public void onFailed() {

            }

            @Override
            public void onUpdateCompleted(InternalSalesCorrectionBean internalSalesCorrectionBean) {

            }

            @Override
            public void onInternalSalesCorrectionBeanLoaded(InternalSalesCorrectionBean internalSalesCorrectionBean) {

            }
        };

    }

    public void showConfirmationDialog() {
        confirmationDialog = new ConfirmationDialog(this);
        confirmationDialog.setConfirmationDialogActionListener(confirmationDialogListener);
        confirmationDialog.show(R.string.message_file_already_exists);
    }

    private void populateMenu() {

        if (adapter == null) {
            adapter = new MenuListRecyclerAdapter(this, menuListBean);
            adapter.setMenuListRecyclerAdapterListener(new MenuListRecyclerAdapter.MenuListRecyclerAdapterListener() {
                @Override
                public void onMenuClicked(int position, MenuBean menuBean) {

                    selectedSubMenuBean = menuBean;
                    Intent intent;
                    switch (menuBean.getCode()) {
                        case ITMenuUtil.CODE_WASTE:
                            wasteRepository.getAllUnsentWastes(wasteRepositoryListener);
                            break;

                        case ITMenuUtil.CODE_LABEL_SURVEY:
                            labelSurveyRepository.getAllUnsentLabelSurveys(labelSurveyRepositoryListener);
                            break;

                        case ITMenuUtil.CODE_STOCK_TAKE:
                            stockTakeRepository.getAllUnsentStockTakes(stockTakeRepositoryListener);
                            break;

                        case ITMenuUtil.CODE_PRINT_LABELS:
                            navigateToSelectLabelTypeActivity();
                            break;

                        case ITMenuUtil.CODE_MIN_STOCK:
                            minStockRepository.getAllUnsentMinStocks(minStockRepositoryListener);
                            break;

                        case ITMenuUtil.CODE_FACING:
                            facingRepository.getAllUnsentFacings(facingRepositoryListener);
                            break;

                        case ITMenuUtil.CODE_PROMOTION:
                            promotionRepository.getAllUnsentPromotions(promotionRepositoryListener);
                            break;

                        case ITMenuUtil.CODE_OUT_OF_STOCK:
                            outOfStockRepository.getAllUnsentOutOfStocks(outOfStockRepositoryListener);
                            break;

                        case ITMenuUtil.CODE_TRANSFORM_QUANTITY:
                            transformQuantityRepository.getAllUnsentTransformQuantities(transformQuantityRepositoryListener);
                            break;

                        case ITMenuUtil.CODE_ITEM_LIST:
                            itemListRepository.getAllUnsentItemLists(itemListRepositoryListener);
                            break;

                        case ITMenuUtil.CODE_INTERNAL_SALES:
                            internalSalesRepository.getAllUnsentInternalSales(internalSalesRepositoryListener);
                            break;
                        case ITMenuUtil.CODE_INTERNAL_SALES_CORRECT:
                            internalSalesCorrectionRepository.getAllUnsentInternalSalesCorrection(internalSalesCorrectionRepositoryListener);
                            break;
                        case ITMenuUtil.CODE_EXIT:
                            finish();
                            break;
                        case ITMenuUtil.CODE_RESTORE: {
                            startActivityForResult(new Intent(ITActivity.this, AuthenticateActivity.class)
                                    .putExtra("isSuperAdminRequired", false), REQ_AUTHENTICATE);
                        }
                        break;
                    }
                }

                @Override
                public void onSwipeRefreshingChange(boolean isSwipeRefreshing) {
                    swipeView.setRefreshing(isSwipeRefreshing);

                }

                @Override
                public void onSnackBarShow(String message) {
                    Snackbar.make(coordinatorLayout, message, Snackbar.LENGTH_LONG)
                            .setAction(R.string.btn_dismiss, snackBarDismissOnClickListener).show();

                }

            });
            rvIT.setAdapter(adapter);
        } else {
            adapter.setLoadMore(false);
            adapter.setMenuListBean(menuListBean);
            adapter.notifyDataSetChanged();
        }

        setProgressScreenVisibility(false, false);

    }


    private void navigateToWasteActivity() {
        startActivity(new Intent(getApplicationContext(), WasteActivity.class)
                .putExtra("menuBean", selectedMenuBean)
                .putExtra("subMenuBean", selectedSubMenuBean));
    }

    private void navigateToLabelSurveyActivity() {
        startActivity(new Intent(getApplicationContext(), LabelSurveyActivity.class)
                .putExtra("menuBean", selectedMenuBean)
                .putExtra("subMenuBean", selectedSubMenuBean));
    }

    private void navigateToStockTakeActivity() {
        startActivity(new Intent(getApplicationContext(), StockTakeActivity.class)
                .putExtra("menuBean", selectedMenuBean)
                .putExtra("subMenuBean", selectedSubMenuBean));
    }

    private void navigateToMinStockActivity() {
        startActivity(new Intent(getApplicationContext(), MinStockActivity.class)
                .putExtra("menuBean", selectedMenuBean)
                .putExtra("subMenuBean", selectedSubMenuBean));
    }

    private void navigateToFacingActivity() {
        startActivity(new Intent(getApplicationContext(), FacingActivity.class)
                .putExtra("menuBean", selectedMenuBean)
                .putExtra("subMenuBean", selectedSubMenuBean));
    }

    private void navigateToPromotionActivity() {
        startActivity(new Intent(getApplicationContext(), PromotionActivity.class)
                .putExtra("menuBean", selectedMenuBean)
                .putExtra("subMenuBean", selectedSubMenuBean));
    }

    private void navigateToOutOfStockActivity() {
        startActivity(new Intent(getApplicationContext(), OutOfStockActivity.class)
                .putExtra("menuBean", selectedMenuBean)
                .putExtra("subMenuBean", selectedSubMenuBean));
    }

    private void navigateToTransformQuantityActivity() {
        startActivity(new Intent(getApplicationContext(), TransformQuantityActivity.class)
                .putExtra("menuBean", selectedMenuBean)
                .putExtra("subMenuBean", selectedSubMenuBean));
    }

    private void navigateToItemListActivity() {
        startActivity(new Intent(getApplicationContext(), ItemListActivity.class)
                .putExtra("menuBean", selectedMenuBean)
                .putExtra("subMenuBean", selectedSubMenuBean));
    }

    private void navigateToInternalSalesActivity() {
        startActivity(new Intent(getApplicationContext(), InternalSalesActivity.class)
                .putExtra("menuBean", selectedMenuBean)
                .putExtra("subMenuBean", selectedSubMenuBean));
    }

    private void navigateToInternalSalesCorrectionActivity() {
        startActivity(new Intent(getApplicationContext(), InternalSalesCorrectionActivity.class)
                .putExtra("menuBean", selectedMenuBean)
                .putExtra("subMenuBean", selectedSubMenuBean));
    }

    private void navigateToSelectLabelTypeActivity() {
        startActivity(new Intent(getApplicationContext(), SelectLabelTypeActivity.class)
                .putExtra("menuBean", selectedMenuBean)
                .putExtra("subMenuBean", selectedSubMenuBean));
    }

    private void navigateToITRestoreActivity() {
        startActivity(new Intent(getApplicationContext(), ITRestoreActivity.class)
                .putExtra("menuBean", selectedMenuBean));
    }


   /* private String readTextFile(InputStream inputStream) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

        byte buf[] = new byte[1024];
        int len;
        try {
            while ((len = inputStream.read(buf)) != -1) {
                outputStream.write(buf, 0, len);
            }
            outputStream.close();
            inputStream.close();
        } catch (IOException e) {

        }
        return outputStream.toString();
    }

    protected void processScannedData(String scanData) {
        XmlPullParserFactory pullParserFactory;
        String tempVal = null, subMenuVal = null;
        try {
            // init the parserfactory
            pullParserFactory = XmlPullParserFactory.newInstance();
            // get the parser
            XmlPullParser parser = pullParserFactory.newPullParser();

            parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
            parser.setInput(new StringReader(scanData));
            boolean submenuFlag = false;
            // parse the XML
            int eventType = parser.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT) {
                if (eventType == XmlPullParser.START_DOCUMENT) {
                    Log.d("Rajdeol", "Start document");
                } else if (eventType == XmlPullParser.START_TAG) {
                    tempVal = parser.getAttributeValue(null, "Menu");
                    subMenuVal = parser.getAttributeValue(null, "Submenu");
                    if (tempVal != null)
                        submenuFlag = false;
                    if (tempVal != null && tempVal.equalsIgnoreCase(selMenu)) {
                        submenuFlag = true;

                    }
                    if (submenuFlag && subMenuVal != null)
                        subMenuList.add(subMenuVal);

                } else if (eventType == XmlPullParser.END_TAG) {
                    Log.d("Rajdeol", "End tag " + parser.getName());

                } else if (eventType == XmlPullParser.TEXT) {
                    Log.d("Rajdeol", "Text " + parser.getText());

                }
                eventType = parser.next();
            }

            // display the data on screen
            displayScannedData();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }// EO function

    private void displayScannedData() {
        String str = "";
        for (String model :
                subMenuList) {
            str += model;
        }
        //Toast.makeText(this, ""+ str, Toast.LENGTH_SHORT).show();
    }*/
}
