package com.example.miguel.saludoactivitylabappint;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import saludoactivitylabappint.R;

import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


public class Saludo extends AppCompatActivity implements ContersUI{

    Button btnSalir;
    TextView etName;

    MyApplication myApplication;
    private Counters mCounters;

    // String for LogCat documentation
    private final static String TAG = "Lab-Saludo";


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

        myApplication = ((MyApplication)this.getApplication());
        mCounters = myApplication.countersSaludo;

        btnSalir=(Button)findViewById(R.id.btnSalir);
        etName= (TextView)findViewById(R.id.etName);

        Bundle extras = getIntent().getExtras();
        if (extras != null){
            String saludostring=extras.getString("txtSaludo");
            etName.setText(saludostring);
        }

        btnSalir.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                ComponentName callingActivity = Saludo.this.getCallingActivity();
                if (callingActivity != null) {
                    SetActivityResult();
                }
                else {
                    Toast.makeText(Saludo.this,"Sin retorno de información",Toast.LENGTH_LONG).show();
                }
                finish();
            }//onClick
        }); //setOnClickListener

        // Update the appropriate count variable
        mCounters.incOnCreate();
        Log.d(TAG, "Entering OnCreate(), increasing counter, set to " + Integer.toString(mCounters.getOnCreate()));
        UpdateCountersUI();
    }//onCreate

    @Override
    protected void onStart() {
        super.onStart();
        // Update the appropriate count variable
        mCounters.incOnStart();
        Log.d(TAG, "Entering OnStart(), increasing counter, set to " + Integer.toString(mCounters.getOnStart()));
        UpdateCountersUI();
    }

    @Override
    protected void onResume() {
        super.onResume();
        // Update the appropriate count variable
        mCounters.incOnResume();
        Log.d(TAG, "Entering OnResume(), increasing counter, set to " + Integer.toString(mCounters.getOnResume()));
        UpdateCountersUI();
    }

    private void SetActivityResult(){
        Toast.makeText(Saludo.this, "Retornando información", Toast.LENGTH_LONG).show();
        Intent returnIntent = new Intent();
        returnIntent.putExtra("txtResult",etName.getText().toString());
        setResult(Activity.RESULT_OK, returnIntent);
    } //SetActivityResult

    @Override
    protected void onPause() {
        super.onPause();
        // Update the appropriate count variable
        mCounters.incOnPause();

        // LogCat message
        Log.d(TAG, "Entering OnPause(), increasing counter, set to " + Integer.toString(mCounters.getOnPause()));

        //LLamamos a UpdateUI pero no lo vemos pues no es visible todavía la Activity.
        UpdateCountersUI();
    }

    @Override
    protected void onStop() {
        super.onStop();
        // Update the appropriate count variable
        mCounters.incOnStop();

        // LogCat message
        Log.d(TAG, "Entering OnStop(), increasing counter, set to " + Integer.toString(mCounters.getOnStop()));

        //LLamamos a UpdateUI pero no lo vemos pues no es visible todavía la Activity.
        UpdateCountersUI();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        // Update the appropriate count variable
        mCounters.incOnRestart();

        // LogCat message
        Log.d(TAG, "Entering OnRestart(), increasing counter, set to " + Integer.toString(mCounters.getOnRestart()));

        //LLamamos a UpdateUI pero no lo vemos pues no es visible todavía la Activity.
        UpdateCountersUI();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // Update the appropriate count variable
        mCounters.incOnDestroy();

        // LogCat message
        Log.d(TAG, "Entering OnDestroy(), increasing counter, set to " + Integer.toString(mCounters.getOnDestroy()));
    }

    @Override
    public void onBackPressed(){
        ComponentName callingActivity = Saludo.this.getCallingActivity();
        if (callingActivity != null) {
            SetActivityResult();
        }
        else {
            Toast.makeText(Saludo.this,"Sin retorno de información",Toast.LENGTH_LONG).show();
        }
        super.onBackPressed();
    }

    @Override
    public void UpdateCountersUI() {
        //OnCreate
        TextView tvOnCreate = (TextView) findViewById(R.id.tvOnCreate);
        tvOnCreate.setText(Integer.toString(mCounters.getOnCreate()));
        //OnStart
        TextView tvOnStart = (TextView) findViewById(R.id.tvOnStart);
        tvOnStart.setText(Integer.toString(mCounters.getOnStart()));
        //OnResume
        TextView tvOnResume = (TextView) findViewById(R.id.tvOnResume);
        tvOnResume.setText(Integer.toString(mCounters.getOnResume()));
        //OnRestart
        TextView tvOnRestart = (TextView) findViewById(R.id.tvOnRestart);
        tvOnRestart.setText(Integer.toString(mCounters.getOnRestart()));
        //OnPause
        TextView tvOnPause = (TextView) findViewById(R.id.tvOnPause);
        tvOnPause.setText(Integer.toString(mCounters.getOnPause()));
        //OnStop
        TextView tvOnStop = (TextView) findViewById(R.id.tvOnStop);
        tvOnStop.setText(Integer.toString(mCounters.getOnStop()));
        //OnDestroy
        TextView tvOnDestroy = (TextView) findViewById(R.id.tvOnDestroy);
        tvOnDestroy.setText(Integer.toString(mCounters.getOnDestroy()));
    }
}
