package info.nightscout.androidaps.plugins.Maintenance;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.plugins.Food.FoodPlugin;
import info.nightscout.androidaps.plugins.Treatments.TreatmentsPlugin;
import info.nightscout.utils.ImportExportPrefs;

/**
 *
 */
public class MaintenanceFragment extends Fragment {

    private Fragment f;

    @Override
    public void onResume() {
        super.onResume();

        this.f = this;
    }

    @Override
    public void onPause() {
        super.onPause();

        this.f = null;
    }

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

        final Fragment f = this;

        view.findViewById(R.id.log_send).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MaintenancePlugin.getPlugin().sendLogs();
            }
        });

        view.findViewById(R.id.log_delete).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MaintenancePlugin.getPlugin().deleteLogs();
            }
        });

        view.findViewById(R.id.nav_resetdb).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new AlertDialog.Builder(f.getContext())
                        .setTitle(R.string.nav_resetdb)
                        .setMessage(R.string.reset_db_confirm)
                        .setNegativeButton(android.R.string.cancel, null)
                        .setPositiveButton(android.R.string.ok, (dialog, which) -> {
                            MainApp.getDbHelper().resetDatabases();
                            // should be handled by Plugin-Interface and
                            // additional service interface and plugin registry
                            FoodPlugin.getPlugin().getService().resetFood();
                            TreatmentsPlugin.getPlugin().getService().resetTreatments();
                        })
                        .create()
                        .show();
            }
        });

        view.findViewById(R.id.nav_export).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // start activity for checking permissions...
                ImportExportPrefs.verifyStoragePermissions(f);
                ImportExportPrefs.exportSharedPreferences(f);
            }
        });

        view.findViewById(R.id.nav_import).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // start activity for checking permissions...
                ImportExportPrefs.verifyStoragePermissions(f);
                ImportExportPrefs.importSharedPreferences(f);
            }
        });


        return view;
    }

}

@@ -1132,119 +1132,16 @@
 ner(
-new View.OnClickListener() %7B%0A            @Override%0A            public void onClick(View view) %7B%0A               
+view1 -%3E
  Mai
@@ -1181,33 +1181,8 @@
 gs()
-;%0A            %7D%0A        %7D
 );%0A%0A
@@ -1247,119 +1247,17 @@
 ner(
-new View.OnClickListener() %7B%0A            @Override%0A            public void onClick(View view) %7B%0A               
+view12 -%3E
  Mai
@@ -1299,33 +1299,8 @@
 gs()
-;%0A            %7D%0A        %7D
 );%0A%0A
@@ -2366,108 +2366,20 @@
 ner(
-new View.OnClickListener() %7B%0A            @Override%0A            public void onClick(View view) %7B%0A    
+view13 -%3E %7B%0A
     
@@ -2436,36 +2436,32 @@
 ...%0A            
-    
 ImportExportPref
@@ -2483,36 +2483,32 @@
 Permissions(f);%0A
-    
             Impo
@@ -2545,38 +2545,24 @@
 erences(f);%0A
-            %7D%0A
         %7D);%0A
@@ -2628,108 +2628,20 @@
 ner(
-new View.OnClickListener() %7B%0A            @Override%0A            public void onClick(View view) %7B%0A    
+view14 -%3E %7B%0A
     
@@ -2698,36 +2698,32 @@
 ...%0A            
-    
 ImportExportPref
@@ -2745,36 +2745,32 @@
 Permissions(f);%0A
-    
             Impo
@@ -2811,30 +2811,16 @@
 ces(f);%0A
-            %7D%0A
         

Use lambdas