package br.com.codetec.saudesabor.utils.components.alert;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;

import br.com.codetec.saudesabor.R;

public class CustomAlert {
    private static CustomAlert instance;

    public static CustomAlert getInstance(){
        if(instance == null) instance = new CustomAlert();

        return instance;
    }
    public void show(Context context,
                     String title,
                     String msg,
                     String negativeButton,
                     String positiveButton,
                     DialogInterface.OnClickListener positiveListener){
        AlertDialog.Builder builder = new AlertDialog.Builder(context, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
//        builder.setTitle(title);
        builder.setMessage(msg)
                .setPositiveButton(negativeButton, null)
                .setNegativeButton(positiveButton, positiveListener);
        this.createBuilder(builder,context);
    }


    public void show(Context context,
                     String title,
                     String msg,
                     String positiveButton,
                     DialogInterface.OnClickListener positiveListener){
        AlertDialog.Builder builder = new AlertDialog.Builder(context, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
//        builder.setTitle(title);
        builder.setMessage(msg)
                .setNegativeButton(positiveButton, positiveListener);
        this.createBuilder(builder,context);
    }

    public void show(Context context,
                     String title,
                     String msg,
                     String positiveButton,
                     String negativeButton,
                     DialogInterface.OnClickListener positiveListener,
                     DialogInterface.OnClickListener negativeListener){
        AlertDialog.Builder builder = new AlertDialog.Builder(context, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
//        builder.setTitle(title);
        builder.setMessage(msg)
                .setPositiveButton(negativeButton, negativeListener)
                .setNegativeButton(positiveButton, positiveListener);
        this.createBuilder(builder,context);
    }


    public void show(Context context, String title, String msg, DialogInterface.OnClickListener positiveListener){
        AlertDialog.Builder builder = new AlertDialog.Builder(context, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
//        builder.setTitle(title);
        builder.setMessage(msg)
                .setPositiveButton("não", null)
                .setNegativeButton("sim", positiveListener);
        this.createBuilder(builder,context);
    }

    public void showOK(Context context, String title, String msg, DialogInterface.OnClickListener positiveListener){
        AlertDialog.Builder builder = new AlertDialog.Builder(context, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
//        builder.setTitle(title);
        builder.setMessage(msg)
                .setPositiveButton("OK", positiveListener);
        this.createBuilder(builder,context);
    }

    public void show(Context context, String title, String msg){

        AlertDialog.Builder builder = new AlertDialog.Builder(context, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
//        builder.setTitle(title);
        builder.setMessage(msg)
                .setPositiveButton("OK", null);

        this.createBuilder(builder,context);
    }

    public void show(Context context, String msg, DialogInterface.OnClickListener listener){

        AlertDialog.Builder builder = new AlertDialog.Builder(context, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
        builder.setCancelable(false);
//        builder.setTitle(context.getResources().getString(R.string.app_name));
        builder.setMessage(msg)
                .setPositiveButton("OK", listener);

        this.createBuilder(builder,context);
    }


    public void show(Context context, String msg){
        show(context, msg, (DialogInterface.OnClickListener) null);
    }


    /**
     * Onde ja se Encontra Titulos De Resposta do Serviço
     */


    public void showService(Context context,
                            TipoAlertaEnum tipoAlerta,
                            String msg,
                            String negativeButton,
                            String positiveButton,
                            DialogInterface.OnClickListener positiveListener){
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
//        builder.setTitle(tipoAlerta.getRepresentative());
        builder.setMessage(msg)
                .setPositiveButton(negativeButton, null)
                .setNegativeButton(positiveButton, positiveListener);
        this.createBuilder(builder,context);
    }



    public void showService(Context context,
                            TipoAlertaEnum tipoAlerta,
                            String msg,
                            String positiveButton,
                            DialogInterface.OnClickListener positiveListener){
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(tipoAlerta.getRepresentative());
        builder.setMessage(msg)
                .setNegativeButton(positiveButton, positiveListener);
        this.createBuilder(builder,context);
    }

    public void showService(Context context,
                            TipoAlertaEnum tipoAlerta,
                            String msg,
                            String positiveButton,
                            String negativeButton,
                            DialogInterface.OnClickListener positiveListener,
                            DialogInterface.OnClickListener negativeListener){
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(tipoAlerta.getRepresentative());
        builder.setMessage(msg)
                .setPositiveButton(positiveButton, positiveListener)
                .setNegativeButton(negativeButton, negativeListener);
        this.createBuilder(builder,context);
    }


    public void showService(Context context, TipoAlertaEnum tipoAlerta, String msg, DialogInterface.OnClickListener positiveListener){
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(tipoAlerta.getRepresentative());
        builder.setMessage(msg)
                .setPositiveButton("OK", positiveListener);
        this.createBuilder(builder,context);
    }


    public void showService(Context context, TipoAlertaEnum tipoAlerta, String msg){

        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(tipoAlerta.getRepresentative());
        builder.setMessage(msg)
                .setPositiveButton("OK", null);

        this.createBuilder(builder,context);
    }


    /**
     *  Metodo responsavel por criar o alerta
     *
     * @param builder
     * @param context
     */
    private void createBuilder(AlertDialog.Builder builder , Context context){
        try {
            if (!((Activity) context).isFinishing()) {
                builder.create().show();
            }
        }catch(Exception e){

        }
    }
}
