package com.example.moslah_hamza.statsdemo;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

import java.util.List;

/**
 * Created by Moslah_Hamza on 06/05/2016.
 */
public class CategAdapter extends RecyclerView.Adapter<CategAdapter.ViewHolder> {
    private OnItemClickListener callback;
    private List<Categorie> CategorieName;
    private Context mContext;
    DataBaseHandler db;

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.catg_name, parent, false);
        ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        holder.Catg_name.setText(CategorieName.get(position).getDescription());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                callback.onItemClick(holder.itemView,position);
            }
        });
        holder.popup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PopupMenu popup = new PopupMenu(mContext, holder.popup);
                final MenuInflater inflater = popup.getMenuInflater();
                inflater.inflate(R.menu.popup, popup.getMenu());
                popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        Categorie cat= CategorieName.get(position);
                        Intent intent;
                        switch(item.getItemId()) {
                            case R.id.update:
                                intent = new Intent(mContext, Edit.class);
                                intent.putExtra("TAG", cat.getDescription());
                                intent.putExtra("TAG2", cat.getPriority());
                                intent.putExtra("TAG3", cat.getType());
                                mContext.startActivity(intent);
                                break;
                            case R.id.remove:
                                db.deleteCategorie(cat);
                                intent = new Intent(mContext, CategoryAct.class);
                                mContext.startActivity(intent);
                                break;
                            case R.id.limit:
                                intent = new Intent(mContext, LimitActivity.class);
                                intent.putExtra("CAT",cat.getDescription());
                                mContext.startActivity(intent);
                                break;
                        }
                        return false;
                    }
                });
                popup.show();
            }
        });
    }

    @Override
    public int getItemCount() {
        return CategorieName.size();
    }

    public CategAdapter(List<Categorie> list) {
        this.CategorieName = list;
    }

    public CategAdapter(List<Categorie> list, Context context) {
        this.CategorieName = list;
        this.callback = (OnItemClickListener) context;
        this.mContext=context;
        db = new DataBaseHandler(context);
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        private TextView Catg_name;
        private ImageButton popup;

        public ViewHolder(View itemView) {
            super(itemView);
            this.Catg_name = (TextView) itemView.findViewById(R.id.catgName);
            this.popup = (ImageButton) itemView.findViewById(R.id.popup);
        }
    }
}