package in.techware.carrefour.adapters;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.HapticFeedbackConstants;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import in.techware.carrefour.R;
import in.techware.carrefour.model.MenuBean;
import in.techware.carrefour.model.MenuListBean;

/**
 * Created by Jemsheer K D on 23 June, 2018.
 * Package in.techware.carrefour.adapters
 * Project Carrefour
 */
public class MenuListRecyclerAdapter extends RecyclerView.Adapter<MenuListRecyclerAdapter.ViewHolder> {

    private static final String TAG = "MenuListRAda";
    private final Activity mContext;
    private MenuListRecyclerAdapterListener menuListRecyclerAdapterListener;
    private MenuListBean menuListBean;
    private boolean isLoadMore;

    public MenuListRecyclerAdapter(Activity mContext, MenuListBean menuListBean) {
        this.mContext = mContext;
        this.menuListBean = menuListBean;

    }

    public MenuListBean getMenuListBean() {
        return menuListBean;
    }

    public void setMenuListBean(MenuListBean menuListBean) {
        this.menuListBean = menuListBean;
    }

    public boolean isLoadMore() {
        return isLoadMore;
    }

    public void setLoadMore(boolean loadMore) {
        isLoadMore = loadMore;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        final LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View itemLayout = inflater.inflate(R.layout.item_menu, parent, false);
        return new ViewHolder(itemLayout);

    }


    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        setLayoutMenu(holder, position);

    }

    @Override
    public int getItemCount() {
        // menu count.. to be changed.
        int count;
        try {
            count = menuListBean.getMenus().size();
        } catch (Exception e) {
            return 0;
        }
        return count;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    private void setLayoutMenu(final ViewHolder holder, final int position) {
        final MenuBean bean = menuListBean.getMenus().get(position);
        holder.txtTitle.setText(bean.getTitle());
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView txtTitle;

        ViewHolder(View lytItem) {
            super(lytItem);

            txtTitle = (TextView) lytItem.findViewById(R.id.txt_item_menu);


            lytItem.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
//                    mVibrator.vibrate(25);

                    MenuBean bean = menuListBean.getMenus().get(getLayoutPosition());
                    /* mContext.mContext.startActivity(new Intent(mContext, DetailsActivity.class)
                            .putExtra("bean", bean));*/
                    menuListRecyclerAdapterListener.onMenuClicked(getLayoutPosition(), bean);

                }
            });
        }
    }

    public static interface MenuListRecyclerAdapterListener {

        void onMenuClicked(int position, MenuBean menuBean);

        void onSwipeRefreshingChange(boolean isSwipeRefreshing);

        void onSnackBarShow(String message);
    }

    public MenuListRecyclerAdapterListener getMenuListRecyclerAdapterListener() {
        return menuListRecyclerAdapterListener;
    }


    public void setMenuListRecyclerAdapterListener(MenuListRecyclerAdapterListener menuListRecyclerAdapterListener) {
        this.menuListRecyclerAdapterListener = menuListRecyclerAdapterListener;
    }
}
