package com.example.salonappnew;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
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 android.widget.Toast;

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

import java.util.ArrayList;

public class MedicineShowWeekAdapter extends BaseAdapter implements Filterable {




    //Firebase storage
    FirebaseAuth mFirebaseAuth;
    FirebaseStorage storage;
    StorageReference storageReference;
    FirebaseFirestore db = FirebaseFirestore.getInstance();

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



    public MedicineShowWeekAdapter(Context c, ArrayList<MedicineWeek> 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,null);



        TextView txtWeekNumber =row.findViewById(R.id.txtWeekNumber);
        TextView txtWeekText =row.findViewById(R.id.txtWeekText);


        txtWeekNumber.setText(originalArray.get(position).getWeekNumber());
        txtWeekText.setText(originalArray.get(position).getWeekText());








        row.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //INTENT to medicine activities week -> MedicineWeekActivities.java
                Intent intent = new Intent(c, MedicineWeekActivities.class);
                //TODO update putExtra variables
                intent.putExtra("txtWeekNumber", originalArray.get(position).getWeekNumber());
                intent.putExtra("txtWeekText",  originalArray.get(position).getWeekText());
                c.startActivity(intent);
                Toast.makeText(c, originalArray.get(position).getWeekText() + " was clicked", Toast.LENGTH_SHORT).show();
                ((Activity) c).finish();
            }
        });

        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).getWeekNumber().toUpperCase().contains(constraint)) {
                        MedicineWeek singleRow = new MedicineWeek(tmpArray.get(i).getWeekNumber(),tmpArray.get(i).getWeekText());
                        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<MedicineWeek>)results.values;
            notifyDataSetChanged();
        }
    }
    ///Codes for filter


}