package com.example.salonappnew;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.example.salonappnew.models.DailyActivity;
import com.example.salonappnew.models.MedicineWeek;
import com.example.salonappnew.models.MedicineWeekActivity;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.QueryDocumentSnapshot;
import com.google.firebase.firestore.QuerySnapshot;

import java.util.ArrayList;

public class DailyActivities extends AppCompatActivity {
    ArrayList<DailyActivity> myList;
    DailyActivitiesAdapter myAdapter;
    FirebaseFirestore db = FirebaseFirestore.getInstance();
    FirebaseAuth mFirebaseAuth = FirebaseAuth.getInstance();
    TextView txtBack;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_daily_activities);

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

        txtBack.setText("< Back");

        Intent intent = getIntent();
        String weekNumber = intent.getStringExtra("txtWeekNumber");
        String weekText = intent.getStringExtra("txtWeekText");

        txtWeekNumber.setText(weekNumber);
        txtWeekText.setText(weekText);

        ListView customerList =(ListView) findViewById(R.id.listItems);

//        qiLfaPlXfwgIGsnOmCWq1iGFi2S2
        myList = new ArrayList<>();
        //Start database
        FirebaseUser user = mFirebaseAuth.getCurrentUser();
        db.collection("dailyActivities").whereEqualTo("uid",user.getUid()).whereEqualTo("weekText",weekText).get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if (task.isSuccessful()) {
                    myList = new ArrayList<>();
                    for (QueryDocumentSnapshot document : task.getResult()) {
                        Log.d("chata",document.toString());

                        DailyActivity dailyActivity = document.toObject(DailyActivity.class);
                        dailyActivity.setDocId(document.getId());
                        dailyActivity.setWeekNumber(weekNumber);
                        dailyActivity.setWeekText(weekText);
                        myList.add(dailyActivity);
                    }
                    myAdapter = new DailyActivitiesAdapter(DailyActivities.this,myList);

                    customerList.setAdapter(myAdapter);

                } else {
                    Log.d("chata", "Error getting documents: ", task.getException());
                }
            }
        });
        //End database

        txtBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent( DailyActivities.this, MedicineWeekActivities.class);
                intent.putExtra("txtWeekNumber", weekNumber);
                intent.putExtra("txtWeekText",  weekText);
                startActivity(intent);
            }
        });



    }
}