package com.example.miguel.saludoactivitylabappint;


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

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


public class MainActivity extends AppCompatActivity implements View.OnClickListener, ContersUI{

    Button btnSaludo, btnSalir;
    Switch swForResult;
    EditText etText;

    MyApplication myApplication;
    private Counters mCounters;


    int iOnStart, iOnCreate, iOnStop, iOnDestroy, iOnResume, iOnRestart, iOnPause;

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

    static final int FOR_RESULT_REQUEST = 1; // The request code

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

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

        btnSaludo = (Button) findViewById(R.id.btnSaludo);
        btnSalir=(Button)findViewById(R.id.btnSalir);
        swForResult=(Switch) findViewById(R.id.swForResult);
        etText = (EditText) findViewById(R.id.edtSaludo);

        btnSaludo.setOnClickListener(this);
        btnSalir.setOnClickListener(this);

        ActionBar actionBar = getSupportActionBar();
        actionBar.setIcon(R.mipmap.ic_tiger);
        actionBar.setDisplayShowHomeEnabled(true);

        // Activar flecha ir atrás (ir a la Parent Activity declarada en el manifest)
        //actionBar.setDisplayHomeAsUpEnabled(true);

        // Update the appropriate count variable
        mCounters.incOnCreate();

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

        UpdateCountersUI();
    }

    @Override
    protected void onStart() {
        super.onStart();

        // Update the appropriate count variable
        mCounters.incOnStart();

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

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

    @Override
    protected void onResume() {
        super.onResume();

        // Update the appropriate count variable
        mCounters.incOnResume();

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

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

    @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 onClick(View v) {
        switch (v.getId()){
            case R.id.btnSaludo: //Boton Saludo, envía datos a la activiti Saludo
                String strSaludo = etText.getText().toString();
                Boolean forResult = swForResult.isChecked();

                if (!(strSaludo.isEmpty())) {
                    Intent intent = new Intent(this, Saludo.class);
                    intent.putExtra("txtSaludo", strSaludo);

                    if (forResult) {
                        startActivityForResult(intent,FOR_RESULT_REQUEST);
                    }
                    else {
                        startActivity(intent);
                    }
                } else {
                    Toast.makeText(getApplicationContext(), "Rellena el campo de texto", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.btnSalir: //Boton Salir, Termina la aplicación
                finish();
                break;
            default:
                break;
            }
    } //onClick


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == FOR_RESULT_REQUEST) {
            if(resultCode == Activity.RESULT_OK){

                etText.setText(data.getStringExtra("txtResult"));
            }
            if (resultCode == Activity.RESULT_CANCELED) {
                //Write your code if there's no result
            }
        }
    }//onActivityResult


    @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()));
    }
}

