package in.techware.carrefour.activity.it;

import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import in.techware.carrefour.R;
import in.techware.carrefour.activity.BaseAppCompatNoDrawerActivity;
import in.techware.carrefour.adapters.MenuListRecyclerAdapter;
import in.techware.carrefour.config.Config;
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.PrintLabelRepository;
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.dialogs.PopupMessage;
import in.techware.carrefour.dialogs.SelectPrintLabelTypeDialog;
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.PrintLabelBean;
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.net.WSTasks.FTPUploadTask;
import in.techware.carrefour.util.AppConstants;
import in.techware.carrefour.util.BarcodeUtil;
import in.techware.carrefour.util.FileTransferUtil;
import in.techware.carrefour.util.menuUtils.ITMenuUtil;
import in.techware.carrefour.util.menuUtils.ITRestoreMenuUtil;
import in.techware.carrefour.viewModels.FileTransferViewModel;
import in.techware.carrefour.viewModels.MinStockViewModel;

import static in.techware.carrefour.util.FileOp.SENT_FILES;
import static in.techware.carrefour.util.FileOp.SEPARATOR;

public class ITRestoreActivity extends BaseAppCompatNoDrawerActivity {

    private static final int REQ_PIT_NUMBER = 1;

    private RecyclerView rvITRestore;
    private FileTransferViewModel viewModel;
    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;
    private PrintLabelRepository printLabelRepository;
    private PrintLabelRepository.PrintLabelRepositoryListener printLabelRepositoryListener;
    private SelectPrintLabelTypeDialog selectPrintLabelTypeDialog;

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


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

        viewModel = (FileTransferViewModel) ViewModelProviders.of(this).get(FileTransferViewModel.class);

        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 onResume() {
        super.onResume();

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

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

        if (requestCode == REQ_PIT_NUMBER && resultCode == RESULT_OK) {
            viewModel.setPitNumber(data.getStringExtra("pitNumber"));
            switch (selectedSubMenuBean.getCode()) {
                case ITMenuUtil.CODE_STOCK_TAKE:
                    stockTakeRepository.getAllUnsentStockTakes(stockTakeRepositoryListener);
                    break;
            }


        }

    }

    private void initViews() {

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

        setWasteRepository();
        setLabelSurveyRepository();
        setStockTakeRepository();
        setPrintLabelRepository();
        setMinStockRepository();
        setFacingRepository();
        setPromotionRepository();
        setOutOfStockRepository();
        setTransformQuantityRepository();
        setItemListRepository();
        setInternalSalesRepository();
        setInternalSalesCorrectionRepository();
    }

    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.getAllSentWastes(wasteRepositoryListener);
                            break;

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

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

                        case ITMenuUtil.CODE_PRINT_LABELS:
                            showSelectLabelTypeDialog();
                            break;

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

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

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

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

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

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

                        case ITMenuUtil.CODE_INTERNAL_SALES:
                            internalSalesRepository.getAllSentInternalSales(internalSalesRepositoryListener);
                            break;
                        case ITMenuUtil.CODE_INTERNAL_SALES_CORRECT:
                            internalSalesCorrectionRepository.
                                    getAllSentInternalSalesCorrection(internalSalesCorrectionRepositoryListener);
                            break;
                        case ITMenuUtil.CODE_EXIT:
                            finish();
                            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();

                }

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

        setProgressScreenVisibility(false, false);
    }

    private void showSelectLabelTypeDialog() {
        selectPrintLabelTypeDialog = new SelectPrintLabelTypeDialog(ITRestoreActivity.this);
        selectPrintLabelTypeDialog.setSelectPrintLabelTypeDialogDialogActionListener(
                new SelectPrintLabelTypeDialog.SelectPrintLabelTypeDialogDialogActionListener() {
                    @Override
                    public void onLabelTypeSelected(int labelType) {
                        printLabelRepository.getAllSentPrintLabels(labelType, printLabelRepositoryListener);
                    }

                    @Override
                    public void actionFailed(String errorMsg) {
                        Toast.makeText(ITRestoreActivity.this, errorMsg, Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onSnackBarShow(String message) {
                        Toast.makeText(ITRestoreActivity.this, message, Toast.LENGTH_SHORT).show();
                    }
                }
        );
    }


    private void setWasteRepository() {
        wasteRepository = new WasteRepository();
        wasteRepositoryListener = new WasteRepository.WasteRepositoryListener() {
            @Override
            public void onAddCompleted(WasteBean wasteBean) {
            }

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

            @Override
            public void onAllSentListLoaded(ArrayList<WasteBean> wasteList) {
                if (wasteList != null && !wasteList.isEmpty()) {
                    StringBuilder stringBuilder = new StringBuilder();
                    ArrayList<WasteBean> list = FileTransferUtil.getUniqueWasteList(wasteList);
                    for (WasteBean bean : list) {
                        if (wasteList.indexOf(bean) != 0) {
                            stringBuilder.append("\n");
                        }
                        stringBuilder.append("DM")
                                .append(BarcodeUtil.getFormattedBarcode(bean.getBarcode()))
                                .append(AppConstants.SPACE)
                                .append(BarcodeUtil.getFormattedQuantity(bean.getQuantity(), 8, true))
                                .append(bean.getOriginTypeCode());
                    }

                    viewModel.setWasteList(wasteList);
                    String fileName = AppConstants.getFileName(selectedMenuBean, selectedSubMenuBean, 0);
                    fop.writeFile(stringBuilder.toString(), fileName);
                    uploadFile(fileName, "");

                }
            }

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

            @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 setLabelSurveyRepository() {
        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) {
                if (labelSurveyList != null && !labelSurveyList.isEmpty()) {
                    StringBuilder stringBuilder = new StringBuilder();
                    for (LabelSurveyBean bean : labelSurveyList) {
                        if (labelSurveyList.indexOf(bean) != 0) {
                            stringBuilder.append("\n");
                        }
                        stringBuilder
                                .append(BarcodeUtil.getFormattedBarcode(bean.getBarcode()))
                                .append(BarcodeUtil.getFormattedPrice(bean.getPrice()))
                                .append(Config.getInstance().getAppSettingsBean().getScanFlag());
                    }

                    viewModel.setLabelSurveyList(labelSurveyList);
                    String fileName = AppConstants.getFileName(selectedMenuBean, selectedSubMenuBean, 0);
                    fop.writeFile(stringBuilder.toString(), fileName);
                    uploadFile(fileName, "");

                }
            }

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

            @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 onLabelSurveyBeanLoaded(LabelSurveyBean labelSurveyBean) {
            }

            @Override
            public void onUpdateCompleted(LabelSurveyBean labelSurveyBean) {
            }
        };
    }

    private void setStockTakeRepository() {
        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) {
                if (stockTakeList != null && !stockTakeList.isEmpty()) {
                    ArrayList<StockTakeBean> list = FileTransferUtil.getUniqueStockTakeList(stockTakeList);
                    StringBuilder stringBuilder = new StringBuilder();
                    for (StockTakeBean bean : list) {
                        if (stockTakeList.indexOf(bean) != 0) {
                            stringBuilder.append("\n");
                        }
                        stringBuilder.append("IG")
                                .append(BarcodeUtil.getFormattedBarcode(bean.getBarcode()))
                                .append(BarcodeUtil.getFormattedQuantity(bean.getQuantity(), 8, true))
                                .append(bean.getZoneType())
                                .append(Config.getInstance().getAppSettingsBean().getScanFlag());
                    }

                    viewModel.setStockTakeList(stockTakeList);
                    String fileName = AppConstants.getFileName(selectedMenuBean, selectedSubMenuBean, 0);
                    fop.writeFile(stringBuilder.toString(), fileName + viewModel.getPitNumber());
                    uploadFile(fileName + viewModel.getPitNumber(), "");

                }
            }

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

            @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 onStockTakeBeanLoaded(StockTakeBean stockTakeBean) {
            }

            @Override
            public void onUpdateCompleted(StockTakeBean stockTakeBean) {
            }
        };
    }

    private void setMinStockRepository() {
        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) {
                if (minStockList != null && !minStockList.isEmpty()) {
                    ArrayList<MinStockBean> list = FileTransferUtil.getUniqueMinStockList(minStockList);
                    StringBuilder stringBuilder = new StringBuilder();
                    for (MinStockBean bean : list) {
                        if (minStockList.indexOf(bean) != 0) {
                            stringBuilder.append("\n");
                        }
                        stringBuilder.append("SM")
                                .append(BarcodeUtil.getFormattedBarcode(bean.getBarcode()))
                                .append(BarcodeUtil.getFormattedQuantity(bean.getQuantity(), 8, true));
                    }

                    viewModel.setMinStockList(minStockList);
                    String fileName = AppConstants.getFileName(selectedMenuBean, selectedSubMenuBean, 0);
                    fop.writeFile(stringBuilder.toString(), fileName);
                    uploadFile(fileName, "");

                }
            }

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

            @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 onMinStockBeanLoaded(MinStockBean minStockBean) {
            }

            @Override
            public void onUpdateCompleted(MinStockBean minStockBean) {
            }
        };
    }

    private void setFacingRepository() {
        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) {
                if (facingList != null && !facingList.isEmpty()) {
                    ArrayList<FacingBean> list = FileTransferUtil.getUniqueFacingList(facingList);
                    StringBuilder stringBuilder = new StringBuilder();
                    StringBuilder stringBuilderLabelMake = new StringBuilder();
                    for (FacingBean bean : list) {
                        if (facingList.indexOf(bean) != 0) {
                            stringBuilder.append("\n");
                            stringBuilderLabelMake.append("\n");
                        }
                        stringBuilder.append("FA")
                                .append(BarcodeUtil.getFormattedBarcode(bean.getBarcode()))
                                .append(BarcodeUtil.getFormattedQuantity(bean.getQuantity(), 8, true));
                        stringBuilderLabelMake.append("ET")
                                .append(BarcodeUtil.getFormattedBarcode(bean.getBarcode()))
                                .append("00102");
                    }

                    viewModel.setFacingList(facingList);
                    String fileName = AppConstants.getFileName(selectedMenuBean, selectedSubMenuBean, 0);
                    fop.writeFile(stringBuilder.toString(), fileName);
                    uploadFile(fileName, "");

                }
            }

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

            @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 onFacingBeanLoaded(FacingBean facingBean) {
            }

            @Override
            public void onUpdateCompleted(FacingBean facingBean) {
            }
        };
    }

    private void setPromotionRepository() {
        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) {
                if (promotionList != null && !promotionList.isEmpty()) {
                    StringBuilder stringBuilder = new StringBuilder();
                    for (PromotionBean bean : promotionList) {
                        if (promotionList.indexOf(bean) != 0) {
                            stringBuilder.append("\n");
                        }
                        stringBuilder.append("PR")
                                .append(BarcodeUtil.getFormattedBarcode(bean.getBarcode()))
                                .append("00000000")
                                .append(bean.getGondola());
                    }

                    viewModel.setPromotionList(promotionList);
                    String fileName = AppConstants.getFileName(selectedMenuBean, selectedSubMenuBean, 0);
                    fop.writeFile(stringBuilder.toString(), fileName);
                    uploadFile(fileName, "");

                }
            }

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

            @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 onPromotionBeanLoaded(PromotionBean promotionBean) {
            }

            @Override
            public void onUpdateCompleted(PromotionBean promotionBean) {
            }
        };
    }

    private void setOutOfStockRepository() {
        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) {
                if (outOfStockList != null && !outOfStockList.isEmpty()) {
                    StringBuilder stringBuilder = new StringBuilder();
                    for (OutOfStockBean bean : outOfStockList) {
                        if (outOfStockList.indexOf(bean) != 0) {
                            stringBuilder.append("\n");
                        }
                        stringBuilder.append("OU")
                                .append(BarcodeUtil.getFormattedBarcode(bean.getBarcode()))
                                .append("00000000");
                    }

                    viewModel.setOutOfStockList(outOfStockList);
                    String fileName = AppConstants.getFileName(selectedMenuBean, selectedSubMenuBean, 0);
                    fop.writeFile(stringBuilder.toString(), fileName);
                    uploadFile(fileName, "");

                }
            }

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

            @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 onOutOfStockBeanLoaded(OutOfStockBean outOfStockBean) {
            }

            @Override
            public void onUpdateCompleted(OutOfStockBean outOfStockBean) {
            }
        };
    }

    private void setTransformQuantityRepository() {
        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) {
                if (transformQuantityList != null && !transformQuantityList.isEmpty()) {
                    StringBuilder stringBuilder = new StringBuilder();
                    for (TransformQuantityBean bean : transformQuantityList) {
                        if (transformQuantityList.indexOf(bean) != 0) {
                            stringBuilder.append("\n");
                        }
                        stringBuilder.append("IL")
                                .append(BarcodeUtil.getFormattedBarcode(bean.getBarcode()));
                    }

                    viewModel.setTransformQuantityList(transformQuantityList);
                    String fileName = AppConstants.getFileName(selectedMenuBean, selectedSubMenuBean, 0);
                    fop.writeFile(stringBuilder.toString(), fileName);
                    uploadFile(fileName, "");

                }
            }

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

            @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 onTransformQuantityBeanLoaded(TransformQuantityBean transformQuantityBean) {
            }

            @Override
            public void onUpdateCompleted(TransformQuantityBean transformQuantityBean) {
            }
        };
    }

    private void setItemListRepository() {
        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) {
                if (itemListList != null && !itemListList.isEmpty()) {
                    ArrayList<ItemListBean> list = FileTransferUtil.getUniqueItemListList(itemListList);
                    StringBuilder stringBuilder = new StringBuilder();
                    for (ItemListBean bean : list) {
                        if (itemListList.indexOf(bean) != 0) {
                            stringBuilder.append("\n");
                        }
                        stringBuilder.append("IL")
                                .append(BarcodeUtil.getFormattedBarcode(bean.getBarcode()))
                                .append(BarcodeUtil.getFormattedQuantity(bean.getQuantity(), 11, true));
                    }

                    viewModel.setItemListList(itemListList);
                    String fileName = AppConstants.getFileName(selectedMenuBean, selectedSubMenuBean, 0);
                    fop.writeFile(stringBuilder.toString(), fileName);
                    uploadFile(fileName, "");

                }
            }

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

            @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 onItemListBeanLoaded(ItemListBean itemListBean) {
            }

            @Override
            public void onUpdateCompleted(ItemListBean itemListBean) {
            }
        };
    }

    private void setInternalSalesRepository() {
        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) {
                if (internalSalesList != null && !internalSalesList.isEmpty()) {
                    ArrayList<InternalSalesBean> list = FileTransferUtil.getUniqueInternalSaleList(internalSalesList);
                    StringBuilder stringBuilder = new StringBuilder();
                    for (InternalSalesBean bean : list) {
                        if (internalSalesList.indexOf(bean) != 0) {
                            stringBuilder.append("\n");
                        }
                        stringBuilder.append("TR")
                                .append(BarcodeUtil.getFormattedBarcode(bean.getBarcode()))
                                .append(BarcodeUtil.getFormattedQuantity(bean.getQuantity(), 11, true))
                                .append(bean.getDepartmentCode())
                                .append(bean.getSectionCode());
                    }

                    viewModel.setInternalSalesList(internalSalesList);
                    String fileName = AppConstants.getFileName(selectedMenuBean, selectedSubMenuBean, 0);
                    fop.writeFile(stringBuilder.toString(), fileName);
                    uploadFile(fileName, "");

                }
            }

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

            @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 onInternalSalesBeanLoaded(InternalSalesBean internalSalesBean) {
            }

            @Override
            public void onUpdateCompleted(InternalSalesBean internalSalesBean) {
            }
        };
    }

    private void setInternalSalesCorrectionRepository() {
        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) {
                        if (internalSalesCorrectionList != null && !internalSalesCorrectionList.isEmpty()) {
                            ArrayList<InternalSalesCorrectionBean> list =
                                    FileTransferUtil.getUniqueInternalSaleCorrectionList(internalSalesCorrectionList);
                            StringBuilder stringBuilder = new StringBuilder();
                            for (InternalSalesCorrectionBean bean : list) {
                                if (internalSalesCorrectionList.indexOf(bean) != 0) {
                                    stringBuilder.append("\n");
                                }
                                stringBuilder.append("TR")
                                        .append(BarcodeUtil.getFormattedBarcode(bean.getBarcode()))
                                        .append(BarcodeUtil.getFormattedQuantity(bean.getQuantity(), 11, true))
                                        .append(bean.getDepartmentCode())
                                        .append(bean.getSectionCode());
                            }

                            viewModel.setInternalSalesCorrectionList(internalSalesCorrectionList);
                            String fileName = AppConstants.getFileName(selectedMenuBean, selectedSubMenuBean, 0);
                            fop.writeFile(stringBuilder.toString(), fileName);
                            uploadFile(fileName, "");

                        }
                    }

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

                    @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 onInternalSalesCorrectionBeanLoaded(InternalSalesCorrectionBean internalSalesCorrectionBean) {
                    }

                    @Override
                    public void onUpdateCompleted(InternalSalesCorrectionBean internalSalesCorrectionBean) {
                    }
                };
    }

    private void setPrintLabelRepository() {
        printLabelRepository = PrintLabelRepository.getInstance();
        printLabelRepositoryListener =
                new PrintLabelRepository.PrintLabelRepositoryListener() {
                    @Override
                    public void onAddCompleted(PrintLabelBean printLabelBean) {
                    }

                    @Override
                    public void onAllListLoaded(ArrayList<PrintLabelBean> printLabelList) {
                    }

                    @Override
                    public void onAllSentListLoaded(int labelType, ArrayList<PrintLabelBean> printLabelList) {
                        if (printLabelList != null && !printLabelList.isEmpty()) {
                            viewModel.setPrintLabelList(printLabelList);

                            viewModel.setPrintLabelMap(new HashMap<String, ArrayList<PrintLabelBean>>());

                            for (PrintLabelBean printLabelBean : printLabelList) {
                                if (!viewModel.getPrintLabelMap().containsKey(printLabelBean.getPrinterLabel())) {
                                    viewModel.getPrintLabelMap().put(
                                            printLabelBean.getPrinterLabel(), new ArrayList<PrintLabelBean>());
                                }
                                ArrayList<PrintLabelBean> list = viewModel.getPrintLabelMap()
                                        .get(printLabelBean.getPrinterLabel());
                                viewModel.getPrintLabelMap().remove(printLabelBean.getPrinterLabel());
                                list.add(printLabelBean);
                                viewModel.getPrintLabelMap().put(printLabelBean.getPrinterLabel(), list);
                            }


                            Iterator<Map.Entry<String, ArrayList<PrintLabelBean>>> iterator =
                                    viewModel.getPrintLabelMap().entrySet().iterator();

                            while (iterator.hasNext()) {
                                Map.Entry<String, ArrayList<PrintLabelBean>> entry = iterator.next();

                                StringBuilder stringBuilder = new StringBuilder();
                                for (PrintLabelBean bean : printLabelList) {
                                    if (printLabelList.indexOf(bean) != 0) {
                                        stringBuilder.append("\n");
                                    }
                                    stringBuilder.append("ET")
                                            .append(BarcodeUtil.getFormattedBarcode(bean.getBarcode()))
                                            .append(BarcodeUtil.getFormattedQuantity(bean.getQuantity(), 3, false))
                                            .append("02");
                                }

                                viewModel.setPrintLabelList(printLabelList);
                                String fileName = AppConstants.getFileName(selectedMenuBean, selectedSubMenuBean, labelType);
                                String tempFileName = entry.getKey().equals("") ? fileName : fileName + entry.getKey();
                                fop.writeFile(stringBuilder.toString(), tempFileName);
                                uploadFile(tempFileName, entry.getKey());
                            }

                        }
                    }

                    @Override
                    public void onAllUnsentListLoaded(int labelType, ArrayList<PrintLabelBean> printLabelList) {

                    }

                    @Override
                    public void onDeleteAllCompleted() {
                    }

                    @Override
                    public void onDeleteAllSentCompleted(String key, int labelType) {
                    }

                    @Override
                    public void onDeleteAllUnsentCompleted(int labelType) {
                    }

                    @Override
                    public void onDeleteCompleted(PrintLabelBean printLabelBean) {
                    }

                    @Override
                    public void onFailed() {
                    }

                    @Override
                    public void onPrintLabelBeanLoaded(PrintLabelBean printLabelBean) {
                    }

                    @Override
                    public void onUpdateCompleted(PrintLabelBean printLabelBean) {
                    }
                };
    }

    private void uploadFile(final String fileName, final String key) {
        setProgressScreenVisibility(true, true);
        FTPUploadTask ftpUploadTask = new FTPUploadTask(fop.pathEx + SENT_FILES + SEPARATOR + fileName);
        ftpUploadTask.setFTPUploadTaskListener(new FTPUploadTask.FTPUploadTaskListener() {
            @Override
            public void dataDownloadedSuccessfully(Boolean result) {
                setProgressScreenVisibility(false, false);
                finishTask();
            }

            @Override
            public void dataDownloadFailed() {
                setProgressScreenVisibility(false, false);
                ConfirmationDialog confirmationDialog = new ConfirmationDialog(ITRestoreActivity.this);
                confirmationDialog.setConfirmationDialogActionListener(
                        new ConfirmationDialog.ConfirmationDialogActionListener() {
                            @Override
                            public void onYesClicked() {
                                uploadFile(fileName, key);
                            }

                            @Override
                            public void onNoClicked() {
                                finish();
                            }
                        });
                confirmationDialog.show(R.string.message_file_upload_failed);
            }

        });
        ftpUploadTask.execute();
    }

    private void finishTask() {
        new PopupMessage(ITRestoreActivity.this)
                .show(getString(R.string.message_file_sent_successfully), 5000);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                finish();
            }
        }, 5000);
    }

}
