package es.upvmaster.fullmediaplayer;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import static es.upvmaster.fullmediaplayer.Libreria.*;

public class ControlParental extends AppCompatActivity {

    boolean controlParentalActivado;
    SharedPreferences prefs;

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

        prefs = getSharedPreferences("Preferencias", Context.MODE_PRIVATE);
        controlParentalActivado = prefs.getBoolean("controlParentalActivado",false);

        crearDialogoControlParental().show();
    }

    public AlertDialog crearDialogoControlParental() {
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);

        LayoutInflater inflater = this.getLayoutInflater();

        View v = inflater.inflate(R.layout.dialog_parental, null);
        TextView textoTitulo = (TextView) v.findViewById(R.id.text_titulo);
        final EditText pinParental = (EditText) v.findViewById(R.id.pin_parental);
        Button botonAceptar = (Button) v.findViewById(R.id.boton_aceptar);
        Button botonCancelar = (Button) v.findViewById(R.id.boton_cancelar);

        if (controlParentalActivado) {
            textoTitulo.setText(getResources().getText(R.string.control_parental_activado));
            botonAceptar.setText(getResources().getString(R.string.desactivar));
        } else {
            textoTitulo.setText(getResources().getText(R.string.control_parental_desactivado));
            botonAceptar.setText(getResources().getString(R.string.activar));
        }

        builder.setView(v);
        final AlertDialog alertDialog = builder.create();
        alertDialog.setCancelable(false);

        botonAceptar.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        String pin = pinParental.getText().toString();
                        if (esNumericoCorrecto(pin,4)){
                            SharedPreferences.Editor editor = prefs.edit();
                            if (controlParentalActivado) {
                                String controlParentalPin = prefs.getString("controlParentalPin","");
                                if (controlParentalPin.equals(pin)) {
                                    editor.putString("controlParentalPin", "");
                                    editor.putBoolean("controlParentalActivado",false);
                                    editor.commit();
                                    finish();
                                } else {
                                    mostrarMensaje(ControlParental.this,getResources().getString(R.string.pin_incorrecto));
                                }

                            } else {
                               editor.putString("controlParentalPin", pin);
                                editor.putBoolean("controlParentalActivado",true);
                                editor.commit();
                                finish();
                            }
                        }
                        else {
                            mostrarMensaje(ControlParental.this,getResources().getString(R.string.formato_pin_incorrecto));
                        }
                    }
                }
        );

        botonCancelar.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        alertDialog.dismiss();
                        finish();
                    }
                }

        );


        return alertDialog;
    }
}
