package in.techware.carrefour.activity.merchandise;

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 in.techware.carrefour.R;
import in.techware.carrefour.activity.BaseAppCompatNoDrawerActivity;
import in.techware.carrefour.adapters.MenuListRecyclerAdapter;
import in.techware.carrefour.dialogs.ConfirmationDialog;
import in.techware.carrefour.model.MenuBean;
import in.techware.carrefour.model.MenuListBean;
import in.techware.carrefour.util.menuUtils.MerchandiseMenuUtil;

public class MerchandiseActivity extends BaseAppCompatNoDrawerActivity {

    RecyclerView rvMerchandise;
    private MenuListBean menuListBean;

    ConfirmationDialog confirmationDialog;
    private MenuListRecyclerAdapter adapter;
    private MenuBean selectedMenuBean;

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

        initViews();

        if (getSupportActionBar() != null) {
            setTitle(R.string.title_merchandise);
            getSupportActionBar().setTitle("");
            getSupportActionBar().setHomeButtonEnabled(true);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowTitleEnabled(true);
        }
    }

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

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


    private void initViews() {
        rvMerchandise = findViewById(R.id.rv_elie_restore);
        rvMerchandise.setHasFixedSize(true);
        rvMerchandise.setLayoutManager(new GridLayoutManager(getApplicationContext(), 2));

        confirmationDialog = new ConfirmationDialog(this);
        confirmationDialog.setConfirmationDialogActionListener(new ConfirmationDialog.ConfirmationDialogActionListener() {
            @Override
            public void onYesClicked() {
                finishAffinity();
            }

            @Override
            public void onNoClicked() {

            }
        });
    }


    public void showExitDialog() {
        confirmationDialog.show(R.string.message_do_you_really_want_to_exit);
    }

    private void populateMenu() {

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

                    selectedMenuBean = menuBean;
                    Intent intent;
                    switch (menuBean.getCode()) {
                        case MerchandiseMenuUtil.CODE_ITEM_LIST: {
                            intent = new Intent(getApplicationContext(), ItemListActivity.class);
                            intent.putExtra("submenu", "1 - ItemList");
                            startActivity(intent);
                        }
                        break;

                        case MerchandiseMenuUtil.CODE_PRINT_LABELS: {
                            intent = new Intent(getApplicationContext(), PrintLabelActivity.class);
                            intent.putExtra("submenu", "2 - Print Labels");
                            startActivity(intent);
                        }
                        break;

                        case MerchandiseMenuUtil.CODE_EXIT: {
                            showExitDialog();
                        }
                        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();

                }

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

        setProgressScreenVisibility(false, false);
    }

}
