package com.avg.zaap.crashlibrary.show.error.dialog;

import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.avg.zaap.crashlibrary.CrashLib;

/**
 * Dialog Activity is showed when error occurs and showErrorType is set to EShowErrorType.DIALOG in CrashLib config.
 * <p/>
 * Accepts 2 configuration parameters:
 * mainText - error message showed on dialog
 * errorDetails - text showed as a title of an alert dialog
 */
@SuppressLint("Registered")
public class DialogActivity extends AppCompatActivity {

    private IDialogActivityFactory mFactory;

    /**
     * Default constructor. Uses default dependency factory.
     */
    @SuppressWarnings("unused")
    public DialogActivity() {
        this(new DialogActivityFactory());
    }

    /**
     * Parametric constructor. Sets dependency factory to be used.
     *
     * @param factory dependency factory to be used
     */
    public DialogActivity(IDialogActivityFactory factory) {
        mFactory = factory;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);

        AlertDialog alertDialog = mFactory.getAlertDialog(DialogActivity.this)
                .setTitle(getResources().getString(CrashLib.getConfig().getErrorMessage()))
                .setMessage(getResources().getString(CrashLib.getConfig().getErrorDetails()))
                .setPositiveButton(android.R.string.ok, null)
                .setOnDismissListener(new DialogInterface.OnDismissListener() {

                    @Override
                    public void onDismiss(DialogInterface dialog) {
                        finishWithoutAnimation();
                    }

                })
                .create();

        alertDialog.show();
    }

    private void finishWithoutAnimation() {
        finish();
        overridePendingTransition(0, 0);
    }

}
