package org.stepic.droid.view.dialogs;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;

import com.yandex.metrica.YandexMetrica;

import org.jetbrains.annotations.NotNull;
import org.stepic.droid.R;
import org.stepic.droid.base.MainApplication;
import org.stepic.droid.core.IShell;
import org.stepic.droid.preferences.UserPreferences;
import org.stepic.droid.store.operations.DatabaseManager;
import org.stepic.droid.util.AppConstants;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

public class VideoQualityDialog extends DialogFragment {


    @Inject
    IShell mShell;

    @Inject
    DatabaseManager mDbManager;

    @Inject
    UserPreferences mUserPreferences;

    private static Map<String, Integer> mQualityToPositionMap = new HashMap<>();
    private static Map<Integer, String> mPositionToQualityMap = new HashMap<>();

    static {
        mQualityToPositionMap.put("270", 0);
        mQualityToPositionMap.put("360", 1);
        mQualityToPositionMap.put("720", 2);
        mQualityToPositionMap.put("1080", 3);

        mPositionToQualityMap.put(0, "270");
        mPositionToQualityMap.put(1, "360");
        mPositionToQualityMap.put(2, "720");
        mPositionToQualityMap.put(3, "1080");
    }


    @NotNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        MainApplication.component().inject(this);

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.MyAlertDialogStyle);
        builder.setTitle(R.string.video_quality)
                .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        YandexMetrica.reportEvent(AppConstants.METRICA_CANCEL_VIDEO_QUALITY);
                    }
                })
                .setSingleChoiceItems(R.array.video_quality,
                        mQualityToPositionMap.get(mUserPreferences.getQualityVideo()),
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, final int which) {
                                AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
                                    @Override
                                    protected Void doInBackground(Void... params) {
                                        mUserPreferences.storeQualityVideo(mPositionToQualityMap.get(which));
                                        return null;
                                    }
                                };
                                task.execute();
                                dialog.dismiss();
                            }
                        });

        return builder.create();
    }
}

@@ -867,31 +867,24 @@
     private 
-static 
 Map%3CString, 
@@ -917,30 +917,19 @@
 nMap = n
-ew HashMap%3C%3E()
+ull
 ;%0A    pr
@@ -934,23 +934,16 @@
 private 
-static 
 Map%3CInte
@@ -984,22 +984,11 @@
  = n
-ew HashMap%3C%3E()
+ull
 ;%0A%0A 
@@ -1007,136 +1007,148 @@
     
-    mQualityToPositionMap.put(%22270%22, 0);%0A        mQualityToPositionMap.put(%22360%22, 1);%0A        mQualityToPositionMap.put(%22720%22, 2
+%7D%0A%0A%0A    @NotNull%0A    @Override%0A    public Dialog onCreateDialog(Bundle savedInstanceState) %7B%0A        MainApplication.component().inject(this
 );%0A 
@@ -1146,32 +1146,36 @@
 (this);%0A        
+if (
 mQualityToPositi
@@ -1183,353 +1183,86 @@
 nMap
-.put(%221080%22, 3);%0A%0A        mPositionToQualityMap.put(0, %22270%22);%0A        mPositionToQualityMap.put(1, %22360%22);%0A        mPositionToQualityMap.put(2, %22720%22);%0A        mPositionToQualityMap.put(3, %221080%22);%0A    %7D%0A%0A%0A    @NotNull%0A    @Override%0A    public Dialog onCreateDialog(Bundle savedInstanceState) %7B%0A        MainApplication.component().inject(this);
+ == null %7C%7C mPositionToQualityMap == null) %7B%0A            initMaps();%0A        %7D
 %0A%0A  
@@ -2652,24 +2652,74 @@
 .dismiss();%0A
+                                dialog.dismiss();%0A
             
@@ -2803,11 +2803,660 @@
 );%0A    %7D
+%0A%0A    private void initMaps() %7B%0A%0A        if (mQualityToPositionMap == null) %7B%0A            mQualityToPositionMap = new HashMap%3C%3E();%0A            mQualityToPositionMap.put(%22270%22, 0);%0A            mQualityToPositionMap.put(%22360%22, 1);%0A            mQualityToPositionMap.put(%22720%22, 2);%0A            mQualityToPositionMap.put(%221080%22, 3);%0A        %7D%0A%0A        if (mPositionToQualityMap == null) %7B%0A            mPositionToQualityMap = new HashMap%3C%3E();%0A            mPositionToQualityMap.put(0, %22270%22);%0A            mPositionToQualityMap.put(1, %22360%22);%0A            mPositionToQualityMap.put(2, %22720%22);%0A            mPositionToQualityMap.put(3, %221080%22);%0A        %7D%0A    %7D
 %0A%7D%0A

change init maps of video quality to runtime