package in.techware.carrefour.activity.elie;

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.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.ELIEMenuUtil;
import in.techware.carrefour.util.menuUtils.ReceiveMenuUtil;

public class ElieActivity extends BaseAppCompatNoDrawerActivity {

    RecyclerView rvElie;
    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_elie);

        initViews();

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

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

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

    private void initViews() {
        rvElie = findViewById(R.id.rv_elie);
        rvElie.setHasFixedSize(true);
        rvElie.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 showConfirmationDialog(String s) {
        confirmationDialog.show(R.string.message_do_you_really_want_to_exit);
        //selectedSubMenu = s;
    }

    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 ELIEMenuUtil.CODE_EXPIRY_CAPTURE: {
                            intent = new Intent(getApplicationContext(), AuthenticateActivity.class);
                            intent.putExtra("submenu", "Expiry Capture");
                            startActivity(intent);
                        }
                        break;

                        case ELIEMenuUtil.CODE_EXPIRED_ACTION: {
                            intent = new Intent(getApplicationContext(), AuthenticateActivity.class);
                            intent.putExtra("submenu", "Expired Action");
                            startActivity(intent);
                        }
                        break;

                        case ELIEMenuUtil.CODE_RESTORE: {
                            intent = new Intent(getApplicationContext(), AuthenticateActivity.class);
                            intent.putExtra("submenu", "Restore");
                            intent.putExtra("menu", "3 - ELIE");
                            startActivity(intent);
                        }
                        break;

                        case ELIEMenuUtil.CODE_EXIT: {
                            showConfirmationDialog("");
                        }
                        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();

                }

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

        setProgressScreenVisibility(false, false);
    }

}
