package in.techware.carrefour.activity.order;

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

public class OrderActivity extends BaseAppCompatNoDrawerActivity {

    RecyclerView rvOrder;
    private MenuListBean menuListBean;

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

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

        initViews();

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

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

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

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


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

            @Override
            public void onNoClicked() {
            }
        });
    }

    public void showConfirmationDialog(String s) {
        confirmationDialog.show(R.string.new_order_data_message_label);
    }

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

            @Override
            public void onNoClicked() {

            }
        });
        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 OrderMenuUtil.CODE_NEW_ORDER: {
                            intent = new Intent(getApplicationContext(), NewOrderActivity.class);
                            intent.putExtra("submenu", "1 - New Order");
                            startActivity(intent);
                        }
                        break;

                        case OrderMenuUtil.CODE_GET_ORDER: {
                            showConfirmationDialog("");
                        }
                        break;

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

                        case OrderMenuUtil.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();

                }

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

        setProgressScreenVisibility(false, false);
    }

}
