package com.example.moslah_hamza.statsdemo;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Moslah_Hamza on 28/02/2016.
 */
public class NewAdapter extends RecyclerView.Adapter<NewAdapter.ViewHolder> {
    private SparseBooleanArray selectedItems;
    private List<Float> sommes;
    private List<View> views;
    private List<String> categories;
    private Context context;
    int screenWidth,screenHeight,px;
    int position;

    public List<View> getViews() {
        return views;
    }

    public void setViews(List<View> views) {
        this.views = views;
    }

    public void toggleSelection(int pos) {
        if (selectedItems.get(pos, false)) {
            selectedItems.delete(pos);
        }
        else {
            selectedItems.put(pos, true);
        }
        notifyItemChanged(pos);
    }

    public void clearSelections() {
        selectedItems.clear();
        notifyDataSetChanged();
    }

    public int getSelectedItemCount() {
        return selectedItems.size();
    }

    public List<Integer> getSelectedItems() {
        List<Integer> items =
                new ArrayList<Integer>(selectedItems.size());
        for (int i = 0; i < selectedItems.size(); i++) {
            items.add(selectedItems.keyAt(i));
        }
        return items;
    }

    // Provide a reference to the views for each data item
    // Complex data items may need more than one view per item, and
    // you provide access to all the views for a data item in a view holder
    public static class ViewHolder extends RecyclerView.ViewHolder {
        // each data item is just a string in this case
        TextView textView1;
        View stat;
        TextView textView2;

        public ViewHolder(View view) {
            super(view);
            //Log.d("new view holder "," created");
            this.textView1 = (TextView) view.findViewById(R.id.cat);
            this.stat = view.findViewById(R.id.statsview);
            this.textView2 = (TextView) view.findViewById(R.id.percent);
        }

        /*public void bind(final TextView item, final AdapterView.OnItemClickListener listener, final int position) {
            item.setOnClickListener(new View.OnClickListener() {
                @Override public void onClick(View v) {
                    listener.onItemClick(item,position);
                }
            });
        }*/
    }

    // Provide a suitable constructor (depends on the kind of dataset)
    public NewAdapter(List<Float> sommes,List<String> categories, Context context,int screenWidth,int screenHeight,int px) {
        this.sommes = sommes;
        this.screenWidth=screenWidth;
        this.screenHeight=screenHeight;
        this.px=px;
        this.categories = categories;
        this.context=context;
        this.views=new ArrayList<View>();
    }

    // Create new views (invoked by the layout manager)
    @Override
    public NewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                    int viewType) {
        // create a new view
        View v= LayoutInflater.from(parent.getContext())
                .inflate(R.layout.single_item, parent, false);
        views.add(v);
        // set the view's size, margins, paddings and layout parameters
        ViewHolder vh = new ViewHolder(v);
        //Log.d("new view holder ","  instance created");
        return vh;
    }

    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        // - get element from your dataset at this position
        // - replace the contents of the view with that element
        this.position=position;
        holder.textView1.setText(categories.get(position));
        holder.textView2.setText(String.valueOf(sommes.get(position)) + "%");

        Log.d("Height ", String.valueOf(px));

        int width= Math.round(screenWidth * (sommes.get(position)) / 100);
        Log.d("Width 1 ", String.valueOf(sommes.get(position)));
        holder.stat.setLayoutParams(new LinearLayout.LayoutParams(width, px));
        holder.stat.setBackgroundColor(context.getResources().getColor(R.color.colorPrimary));
    }

    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return sommes.size();
    }

    public void updateData(List<Float> sommes,List<String> categories, Context context){

    }

    /*public void removeAt(int position) {
        mDates.remove(position);
        mDipenses.remove(position);
        notifyItemRemoved(position);
        notifyItemRangeChanged(position, mDates.size());
        notifyItemRangeChanged(position, mDipenses.size());
    }*/
}
