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

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.LayoutRes;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

import com.avg.zaap.crashlibrary.BuildConfig;
import com.avg.zaap.crashlibrary.CrashLib;
import com.avg.zaap.crashlibrary.R;
import com.avg.zaap.crashlibrary.ReportData;
import com.avg.zaap.crashlibrary.core.logger.CrashLogger;

/**
 * Activity id showed when error occurs and showErrorType is set to EShowErrorType.ERROR_SCREEN in CrashLib config.
 * <p/>
 * Accepts 5 configuration parameters:
 * layout - id of layout
 * errorMessageViewId - id of view where to show error message
 * errorDetails - error message to show
 * errorStackTraceViewId - id of view where to show stack trace
 * errorStackTrace - stack trace to show *
 */
@SuppressLint("Registered")
public class ErrorActivity extends AppCompatActivity {

    private static final int UNDEFINED = 0;

    @LayoutRes
    private static final int RESOURCE_ACTIVITY_LAYOUT = R.layout.activity_error;
    @IdRes
    private static final int RESOURCE_CRASH_CODE = R.id.crashlib_crashCode;
    @IdRes
    private static final int RESOURCE_MESSAGE_ID = R.id.crashlib_errorMessage;
    @IdRes
    private static final int RESOURCE_CAP_EXCEPTION_CODE_VIEW = R.id.crashlib_capExceptionCode;
    @IdRes
    private static final int RESOURCE_STACKTRACE_ID = R.id.crashlib_errorDetails;

    public static final String INTENT_KEY_REPORT_DATA = "reportData";

    private static final String TAG = ErrorActivity.class.getSimpleName();

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

        setContentView(RESOURCE_ACTIVITY_LAYOUT);
        final Bundle extras = getIntent().getExtras();

        if (extras != null && extras.containsKey(INTENT_KEY_REPORT_DATA)) {
            final ReportData reportData = extras.getParcelable(INTENT_KEY_REPORT_DATA);

            if (reportData == null) {
                CrashLogger.e(TAG, "Malformed ErrorActivity intent, reportData is null.");
                return;
            }

            TextView crashCodeView = (TextView) findViewById(RESOURCE_CRASH_CODE);
            if (crashCodeView != null) {
                String crashCode = reportData.getCrashCode();

                if (BuildConfig.DEBUG) {
                    crashCodeView.setText(crashCode);
                }
            } else {
                CrashLogger.e(TAG, "Crash code TextView is null.");
            }

            TextView errorMessageView = (TextView) findViewById(RESOURCE_MESSAGE_ID);
            if (errorMessageView != null) {
                int errorMessage = CrashLib.getConfig().getErrorMessage();

                //noinspection PointlessBooleanExpression,ConstantConditions
                final String reportDataErrorName = reportData.getErrorName();
                final String reportDataErrorMessage = reportData.getErrorMessage();

                if (BuildConfig.DEBUG && reportDataErrorName != null && reportDataErrorMessage != null) {
                    errorMessageView.setText(String.format("%s: %s", reportDataErrorName, reportDataErrorMessage));
                } else {
                    CrashLogger.w(TAG, "A part of error message is null, error name: %s, error message: %s.", reportDataErrorName, reportDataErrorMessage);

                    if (errorMessage != UNDEFINED) {
                        errorMessageView.setText(errorMessage);
                    } else {
                        CrashLogger.w(TAG, "Error message text is undefined.");
                    }
                }
            } else {
                CrashLogger.e(TAG, "Error message TextView is null.");
            }

            TextView capExceptionCodeView = (TextView) findViewById(RESOURCE_CAP_EXCEPTION_CODE_VIEW);
            if (capExceptionCodeView != null) {
                if (BuildConfig.DEBUG) {
                    capExceptionCodeView.setText(reportData.getCapGUID());
                }
            } else {
                CrashLogger.e(TAG, "CAP message TextView is null.");
            }

            TextView errorStackTraceView = (TextView) findViewById(RESOURCE_STACKTRACE_ID);
            if (errorStackTraceView != null) {
                int errorStackTrace = CrashLib.getConfig().getErrorDetails();
                final String reportDataErrorDetails = reportData.getErrorDetails();

                if (BuildConfig.DEBUG && reportDataErrorDetails != null) {
                    errorStackTraceView.setText(reportDataErrorDetails);
                } else {
                    if (errorStackTrace != UNDEFINED) {
                        errorStackTraceView.setText(errorStackTrace);
                    } else {
                        CrashLogger.w(TAG, "Error stacktrace text is undefined.");
                    }
                }
            } else {
                CrashLogger.e(TAG, "Error stacktrace TextView is null.");
            }
        } else {
            CrashLogger.e(TAG, "Malformed ErrorActivity intent, missing reportData.");
        }
    }

}
