package com.example.salonappnew;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

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 MedicineWeekActivities extends AppCompatActivity {
    ArrayList<MedicineWeekActivity> myList;
    MedicineWeekActivitiesAdapter myAdapter;
    FirebaseFirestore db = FirebaseFirestore.getInstance();
    FirebaseAuth mFirebaseAuth = FirebaseAuth.getInstance();
    Button viewDailyButton;
    TextView txtBack;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_medicine_week_activities);

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

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

        txtBack.setText("< Back");

        //Get extra from prev intents

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

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



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


        //start Take data

        FirebaseUser user = mFirebaseAuth.getCurrentUser();

        db.collection("weekMedicines").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());

                        MedicineWeekActivity medicine = document.toObject(MedicineWeekActivity.class);
                        myList.add(medicine);
                    }
                    myAdapter = new MedicineWeekActivitiesAdapter(MedicineWeekActivities.this,myList);

                    customerList.setAdapter(myAdapter);

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

        //End take data


        txtBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
                openNewDashboard();
            }
        });

    }
    public void openNewDashboard(){
        Intent intent = new Intent(this, ADHDHome.class);
        startActivity(intent);
    }
}