package com.example.salonappnew;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;

import com.example.salonappnew.models.MedicineWeek;
import com.example.salonappnew.models.MedicineWeekActivity;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;

import java.util.ArrayList;

public class MedicineWeekActivitiesAdapter extends BaseAdapter implements Filterable {




    //Firebase storage
    FirebaseAuth mFirebaseAuth;
    FirebaseStorage storage;
    StorageReference storageReference;
    private DatabaseReference mFDb;
    private FirebaseDatabase mFirebaseInstant;

    Context c;
    ArrayList<MedicineWeekActivity> originalArray,tmpArray;
    ///filter
    CustomFilter cs;
    ///filter
    String img = "";



    public MedicineWeekActivitiesAdapter(Context c, ArrayList<MedicineWeekActivity> originalArray){
        this.c = c;
        this.originalArray = originalArray;
        this.tmpArray = originalArray;
    }


    @Override
    public Object getItem(int position) {
        return originalArray.get(position);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View row = inflater.inflate(R.layout.medicines_list_week_activities,null);



        TextView txtDrugName =row.findViewById(R.id.txtDrugName);
        TextView txtM =row.findViewById(R.id.txtM);
        TextView txtA =row.findViewById(R.id.txtA);
        TextView txtN =row.findViewById(R.id.txtN);


        txtDrugName.setText(originalArray.get(position).getTxtDrugName());
        txtM.setText(originalArray.get(position).getTxtM());
        txtA.setText(originalArray.get(position).getTxtA());
        txtN.setText(originalArray.get(position).getTxtN());


        row.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        return row;
    }

    @Override
    public int getCount() {
        return originalArray.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    ///Codes for filter
    @Override
    public Filter getFilter() {

        if(cs == null){
            cs = new CustomFilter();
        }

        return cs;
    }

    class CustomFilter extends Filter {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();

            if(constraint != null && constraint.length() > 0) {
                constraint = constraint.toString().toUpperCase();


                ArrayList<MedicineWeek> filters = new ArrayList<>();

                for (int i = 0; i < tmpArray.size(); i++) {
                    if (tmpArray.get(i).getTxtDrugName().toUpperCase().contains(constraint)) {
                        MedicineWeek singleRow = new MedicineWeek(tmpArray.get(i).getTxtDrugName(),tmpArray.get(i).getTxtDrugName());
                        filters.add(singleRow);


                    }



                }
                results.count = filters.size();
                results.values = filters;

            }else {
                results.count = tmpArray.size();
                results.values = tmpArray;

            }
            return results;
        }


        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            originalArray  = (ArrayList<MedicineWeekActivity>)results.values;
            notifyDataSetChanged();
        }
    }
    ///Codes for filter


}