package in.techware.carrefour.activity.receive;

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.AuthenticateActivity;
import in.techware.carrefour.activity.BaseAppCompatNoDrawerActivity;
import in.techware.carrefour.activity.it.CompetitionActivity;
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.ReceiveMenuUtil;

public class ReceiveActivity extends BaseAppCompatNoDrawerActivity {

    RecyclerView rvReceive;
    private MenuListBean menuListBean;

    private ConfirmationDialog confirmationDialog;
    private MenuBean selectedMenuBean;
    private MenuListRecyclerAdapter adapter;

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

        initViews();

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

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

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


    private void initViews() {

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

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

            }

            @Override
            public void onNoClicked() {

            }
        });
    }

    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 ReceiveMenuUtil.CODE_RECEIVING: {
                            intent = new Intent(getApplicationContext(), ReceiveSelectedActivity.class);
                            startActivity(intent);
                        }
                        break;

                        case ReceiveMenuUtil.CODE_RETURN: {
                            intent = new Intent(getApplicationContext(), ReturnActivity.class);
                            startActivity(intent);
                        }
                        break;

                        case ReceiveMenuUtil.CODE_PRICE_SURVEY: {
                            intent = new Intent(getApplicationContext(), PriceSurveyActivity.class);
                            startActivity(intent);
                        }
                        break;

                        case ReceiveMenuUtil.CODE_RESTORE: {
                            intent = new Intent(getApplicationContext(), ReceiveRestoreActivity.class);
                            startActivity(intent);
                        }
                        break;

                        case ReceiveMenuUtil.CODE_STORE_TO_OFFSITE: {
                            intent = new Intent(getApplicationContext(), StoreToOffsiteActivity.class);
                            startActivity(intent);
                        }
                        break;

                        case ReceiveMenuUtil.CODE_OFFSITE_TO_STORE: {
                            intent = new Intent(getApplicationContext(), OffsiteToStoreActivity.class);
                            startActivity(intent);
                        }
                        break;

                        case ReceiveMenuUtil.CODE_COMPETITION: {
                            intent = new Intent(getApplicationContext(), CompetitionActivity.class);
                            startActivity(intent);
                        }
                        break;
                        case ReceiveMenuUtil.CODE_EXIT: {
                            intent = new Intent(getApplicationContext(), AuthenticateActivity.class);
                            startActivity(intent);
                        }
                        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();

                }

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

        setProgressScreenVisibility(false, false);
    }

}
