package com.example.salonappnew;

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

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.salonappnew.models.DailyActivity;
import com.example.salonappnew.models.User;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.QueryDocumentSnapshot;
import com.google.firebase.firestore.QuerySnapshot;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

public class DayActivity extends AppCompatActivity {
    FirebaseFirestore db = FirebaseFirestore.getInstance();
    FirebaseAuth mFirebaseAuth = FirebaseAuth.getInstance();
    TextView textWeekText,txtWeekNumber,txtTaskName,txtTaskText;
    EditText editComment;
    RadioGroup radioGroup;
    RadioButton radioButtonComplete,radioButtonInProgress,radioButtonNotComplete;
    String progressStatus,dComment;
    Button btnSave;
    DailyActivity dailyActivity;
    TextView txtBack;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_day);

        txtBack = findViewById(R.id.txtBack);

        txtBack.setText("< Back");

        textWeekText = findViewById(R.id.textWeekText);
        txtWeekNumber = findViewById(R.id.txtWeekNumber);
        txtTaskName = findViewById(R.id.txtTaskName);
        txtTaskText = findViewById(R.id.txtTaskText);
        editComment = findViewById(R.id.editComment);
        radioGroup = findViewById(R.id.radioGroup);
        radioButtonComplete = findViewById(R.id.radioButtonComplete);
        radioButtonInProgress = findViewById(R.id.radioButtonInProgress);
        radioButtonNotComplete = findViewById(R.id.radioButtonNotComplete);
        btnSave = findViewById(R.id.btnSave);


//        radioGroup.clearCheck();
        radioGroup.setOnCheckedChangeListener(
                new RadioGroup
                        .OnCheckedChangeListener() {
                    @Override

                    // The flow will come here when
                    // any of the radio buttons in the radioGroup
                    // has been clicked

                    // Check which radio button has been clicked
                    public void onCheckedChanged(RadioGroup group,
                                                 int checkedId)
                    {

                        Log.d("chata","radia button"+checkedId);
                        // Get the selected Radio Button
                        RadioButton
                                radioButton
                                = (RadioButton)group
                                .findViewById(checkedId);
                        Log.d("chata",radioButton.getText().toString());
                        progressStatus = radioButton.getText().toString();
                        Toast.makeText(DayActivity.this,
                                radioButton.getText(),
                                Toast.LENGTH_SHORT)
                                .show();
                    }
                });
        Intent intent = getIntent();
        String docId = intent.getStringExtra("docId");
        String weekNumber = intent.getStringExtra("weekNumber");
        String weekText = intent.getStringExtra("weekText");


        textWeekText.setText(weekText);
        txtWeekNumber.setText(weekNumber);
        Log.d("chata","doc id is "+docId);

        //Start take data
        FirebaseUser user = mFirebaseAuth.getCurrentUser();



        db.collection("dailyActivities").document(docId).get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
            @Override
            public void onSuccess(DocumentSnapshot documentSnapshot) {
                dailyActivity = documentSnapshot.toObject(DailyActivity.class);

                txtTaskName.setText(dailyActivity.getTaskName());
                txtTaskText.setText(dailyActivity.getTaskText());
                editComment.setText(dailyActivity.getpComment());
                dComment = dailyActivity.getdComment();

                if(dailyActivity.getProgressStatus().equals("Complete")){
                    radioButtonComplete.setChecked(true);
                    radioButtonInProgress.setChecked(false);
                    radioButtonNotComplete.setChecked(false);
                }else if(dailyActivity.getProgressStatus().equals("In Progress")){
                    radioButtonComplete.setChecked(false);
                    radioButtonInProgress.setChecked(true);
                    radioButtonNotComplete.setChecked(false);
                }else{
                    radioButtonComplete.setChecked(false);
                    radioButtonInProgress.setChecked(false);
                    radioButtonNotComplete.setChecked(true);
                }

            }
        });
        //End take data

        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                Calendar c = Calendar.getInstance();

                int month =
                        c.get(Calendar.MONTH);
                String comment = editComment.getText().toString();
                Map<String, Object> data = new HashMap<>();
                data.put("taskName", dailyActivity.getTaskName());
                data.put("taskText", dailyActivity.getTaskText());
                data.put("progressStatus", progressStatus);
                data.put("pComment",comment );
                data.put("month",month+"" );
                data.put("weekText",weekText );
                data.put("dComment",dComment );
                data.put("uid", user.getUid());

                db.collection("dailyActivities").document(docId)
                        .set(data)
                        .addOnSuccessListener(new OnSuccessListener<Void>() {
                            @Override
                            public void onSuccess(Void aVoid) {
                                Log.d("chata", "DocumentSnapshot successfully written!");
                                Toast.makeText(DayActivity.this,
                                        "Done",
                                        Toast.LENGTH_SHORT)
                                        .show();

                                try{

                                    Intent intent = new Intent(DayActivity.this, DailyActivities.class);
                                    intent.putExtra("txtWeekNumber", weekNumber);
                                    intent.putExtra("txtWeekText",  weekText);

                                    startActivity(intent);

                                }finally {
                                    finish();
                                }

                            }
                        })
                        .addOnFailureListener(new OnFailureListener() {
                            @Override
                            public void onFailure(@NonNull Exception e) {
                                Log.w("chata", "Error writing document", e);
                            }
                        });
            }
        });

        txtBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(DayActivity.this, DailyActivities.class);
//                //TODO update putExtra variables
                intent.putExtra("docId", docId);
                intent.putExtra("weekText", weekText);
                intent.putExtra("weekNumber", weekNumber);
                startActivity(intent);
                finish();
            }
        });




    }
}