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.DailyActivity;
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 DailyActivitiesAdapter extends BaseAdapter implements Filterable {




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

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



    public DailyActivitiesAdapter(Context c, ArrayList<DailyActivity> 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.daily_activities_list,null);



        TextView txtTaskText =row.findViewById(R.id.txtTaskText);
        TextView txtTaskTitle =row.findViewById(R.id.txtTaskTitle);


        txtTaskText.setText(originalArray.get(position).getTaskText());
        txtTaskTitle.setText(originalArray.get(position).getTaskName());





        row.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(c, DayActivity.class);
//                //TODO update putExtra variables
                intent.putExtra("docId", originalArray.get(position).getDocId());
                intent.putExtra("weekText", originalArray.get(position).getWeekText());
                intent.putExtra("weekNumber", originalArray.get(position).getWeekNumber());
                c.startActivity(intent);
                Toast.makeText(c, originalArray.get(position).getTaskName() + " 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<DailyActivity> filters = new ArrayList<>();

                for (int i = 0; i < tmpArray.size(); i++) {
                    if (tmpArray.get(i).getTaskText().toUpperCase().contains(constraint)) {
                        DailyActivity singleRow = new DailyActivity(tmpArray.get(i).getTaskText(),tmpArray.get(i).getTaskText(),tmpArray.get(i).getDocId(),"");
                        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<DailyActivity>)results.values;
            notifyDataSetChanged();
        }
    }
    ///Codes for filter


}