package com.blstream.studybox.exam_view.fragment;


import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.blstream.studybox.R;
import com.blstream.studybox.activities.DecksActivity;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ResultDialogFragment extends DialogFragment implements DialogInterface.OnShowListener{

    private static final String TAG_CORRECT_ANSWERS = "correctAnswers";
    private static final String TAG_NUMBER_OF_QUESTIONS = "noOfQuestions";

    @Bind(R.id.total_score)
    public TextView totalScore;

    private int correctAnswers;
    private int noOfQuestions;

    public static ResultDialogFragment newInstance(int correctAnswers, int noOfQuestions) {
        ResultDialogFragment resultFragment = new ResultDialogFragment();
        Bundle args = new Bundle();
        args.putInt(TAG_CORRECT_ANSWERS, correctAnswers);
        args.putInt(TAG_NUMBER_OF_QUESTIONS, noOfQuestions);
        resultFragment.setArguments(args);
        return resultFragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        correctAnswers = getArguments().getInt(TAG_CORRECT_ANSWERS);
        noOfQuestions = getArguments().getInt(TAG_NUMBER_OF_QUESTIONS);
        setStyle(DialogFragment.STYLE_NO_TITLE, android.R.style.Theme_Light);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_result, container, false);
        initView(view);
        return view;
    }

    private void initView(View view){
        ButterKnife.bind(this, view);
        totalScore.setText(getString(R.string.correct_answers, correctAnswers, noOfQuestions));
    }

    @OnClick(R.id.improve_result)
    public void onClick(View view) {
        dismiss();
    }

    @OnClick(R.id.my_decks)
    public void backToMyDecks(View view) {
        startActivity(new Intent(getContext(), DecksActivity.class));
    }

    @Override
    public void onShow(DialogInterface dialog) {
        ((OnResultShow) getActivity()).onResultShow();
    }

    public interface OnResultShow {
        void onResultShow();
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        Dialog result = new Dialog(getActivity(), getTheme()) {
            @Override
            public void onBackPressed() {
                dismiss();
                getActivity().finish();
            }
        };
        result.setOnShowListener(this);
        return result;
    }
}

@@ -45,16 +45,45 @@
 ment;%0A%0A%0A
+import android.app.Activity;%0A
 import a
@@ -101,16 +101,16 @@
 Dialog;%0A
-
 import a
@@ -145,39 +145,8 @@
 ce;%0A
-import android.content.Intent;%0A
 impo
@@ -417,63 +417,8 @@
 x.R;
-%0Aimport com.blstream.studybox.activities.DecksActivity;
 %0A%0Aim
@@ -873,16 +873,47 @@
 estions;
+%0A    private Activity activity;
 %0A%0A    pu
@@ -1557,24 +1557,58 @@
 QUESTIONS);%0A
+        activity = getActivity();%0A
         setS
@@ -2192,15 +2192,21 @@
 oid 
-onClick
+improveResult
 (Vie
@@ -2324,67 +2324,24 @@
     
-startA
+a
 ctivity
-(new Intent(getContext(), DecksActivity.class)
+.finish(
 );%0A 
@@ -2433,29 +2433,24 @@
 ltShow) 
-getA
+a
 ctivity
-()
 ).onResu
@@ -2663,29 +2663,24 @@
  Dialog(
-getA
+a
 ctivity
-()
 , getThe
@@ -2759,39 +2759,47 @@
                 
-dismiss
+activity.finish
 ();%0A            
@@ -2806,28 +2806,15 @@
     
-getActivity().finish
+dismiss
 ();%0A

remove creating new DecksActivity instance from ResultDialogFragment