package com.astoev.cave.survey.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.astoev.cave.survey.Constants;
import com.astoev.cave.survey.R;
import com.astoev.cave.survey.activity.dialog.ErrorReporterDialog;
import com.astoev.cave.survey.activity.dialog.LanguageDialog;
import com.astoev.cave.survey.activity.poc.SensorTestActivity;
import com.astoev.cave.survey.fragment.InfoDialogFragment;
import com.astoev.cave.survey.service.reports.ErrorReporter;
import com.astoev.cave.survey.util.ConfigUtil;
import com.astoev.cave.survey.util.PermissionUtil;

import static android.Manifest.permission.ACCESS_NETWORK_STATE;
import static android.Manifest.permission.INTERNET;
import static android.Manifest.permission.READ_LOGS;

/**
 * Created by astoev on 10/11/15.
 */
public class SettingsActivity extends MainMenuActivity {

    private static final String LANGUAGE_DIALOG = "LANGUAGE_DIALOG";
    private static final String ERROR_REPORTER_TOOLTIP_DIALOG = "ERROR_REPORTER_TOOLTIP_DIALOG";
    private static final String ERROR_REPORTER_MESSAGE_DIALOG = "ERROR_REPORTER_MESSAGE_DIALOG";
    private static final String AUTO_BACKUP_TOOLTIP_DIALOG = "AUTO_BACKUP_TOOLTIP_DIALOG";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings);

        prepareLanguage();
        prepareSensorsTest();
        prepareAutoBackup();
        prepareErrorReporter();
    }

    private void prepareSensorsTest() {
        TextView bt = (TextView) findViewById(R.id.settingsBluetooth);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i(Constants.LOG_TAG_UI, "Azimuth Test");
                Intent intent = new Intent(SettingsActivity.this, SensorTestActivity.class);
                startActivity(intent);
            }
        });
    }

    private void prepareLanguage() {

        TextView language = (TextView) findViewById(R.id.settingsLanguage);
        language.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LanguageDialog languageDialog = new LanguageDialog();
                languageDialog.show(getSupportFragmentManager(), LANGUAGE_DIALOG);
            }
        });
    }

    private void prepareErrorReporter() {
        ToggleButton errorReporterToggle = (ToggleButton) findViewById(R.id.settingsDebugToggle);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
            // access to system logs disabled since 7.1
            errorReporterToggle.setEnabled(false);
            return;
        }

        if (!PermissionUtil.requestPermissions(new String[]{INTERNET, READ_LOGS, ACCESS_NETWORK_STATE}, this, 401)) {
            return;
        }

        errorReporterToggle.setChecked(ErrorReporter.isDebugRunning());

        errorReporterToggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    // start debug
                    ErrorReporter.startDebugSession();

                    // show the info window
                    Bundle bundle = new Bundle();
                    String message = getString(R.string.error_reporter_info);
                    bundle.putString(InfoDialogFragment.MESSAGE, message);
                    InfoDialogFragment infoDialog = new InfoDialogFragment();
                    infoDialog.setArguments(bundle);
                    infoDialog.show(getSupportFragmentManager(), ERROR_REPORTER_TOOLTIP_DIALOG);
                } else {
                    // stop session
                    String dumpFile = ErrorReporter.closeDebugSession();

                    // show message dialog that sends the report
                    ErrorReporterDialog aboutDialogFragment = new ErrorReporterDialog();
                    Bundle arguments = new Bundle();
                    arguments.putString("dumpFile", dumpFile);
                    aboutDialogFragment.setArguments(arguments);
                    aboutDialogFragment.show(getSupportFragmentManager(), ERROR_REPORTER_MESSAGE_DIALOG);
                }
            }
        });
    }

    private void prepareAutoBackup() {
        ToggleButton autoBackupToggle = (ToggleButton) findViewById(R.id.settingsBackupToggle);
        boolean enabled = ConfigUtil.getBooleanProperty(ConfigUtil.PREF_BACKUP);
        autoBackupToggle.setChecked(enabled);

        autoBackupToggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    Log.i(Constants.LOG_TAG_UI, "Auto backup on");
                    ConfigUtil.setBooleanProperty(ConfigUtil.PREF_BACKUP, true);
                } else {
                    Log.i(Constants.LOG_TAG_UI, "Auto backup off");
                    ConfigUtil.setBooleanProperty(ConfigUtil.PREF_BACKUP, false);
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        prepareErrorReporter();
    }

    public void onAutoBackupChooseInfo(View viewArg) {
        InfoDialogFragment infoDialog = new InfoDialogFragment();

        Bundle bundle = new Bundle();
        String message = getString(R.string.settings_auto_backup_info);
        bundle.putString(InfoDialogFragment.MESSAGE, message);
        infoDialog.setArguments(bundle);

        infoDialog.show(getSupportFragmentManager(), AUTO_BACKUP_TOOLTIP_DIALOG);
    }

    protected String getScreenTitle() {
        return getString(R.string.main_button_settings);
    }

    @Override
    protected boolean showBaseOptionsMenu() {
        return false;
    }
}

@@ -2891,32 +2891,50 @@
 urn;%0A        %7D%0A%0A
+        try %7B%0A    
         if (!Per
@@ -3031,24 +3031,238 @@
 is, 401)) %7B%0A
+                return;%0A            %7D%0A        %7D catch (Exception e) %7B%0A            // requesting system permissions could be dangerous%0A            Log.e(Constants.LOG_TAG_UI, %22Failed to request log permission%22, e);%0A
             

Make sure requesting system logs permission don't break, issue 140?