package com.nulldreams.wowpaper.activity;


import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;
import android.text.format.Formatter;
import android.util.Log;
import android.view.MenuItem;

import com.nulldreams.wowpaper.BuildConfig;
import com.nulldreams.wowpaper.R;
import com.nulldreams.wowpaper.WowApp;
import com.nulldreams.wowpaper.manager.ApiManager;
import com.nulldreams.wowpaper.modules.CountResult;
import com.nulldreams.wowpaper.modules.glide.MyGlideModule;

import java.io.File;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class SettingsActivity extends AppCompatPreferenceActivity
        implements SharedPreferences.OnSharedPreferenceChangeListener, Preference.OnPreferenceChangeListener{

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

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.pref_settings);

        //setSupportActionBar();

        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayShowHomeEnabled(true);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }

    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        final PreferenceScreen screen = this.getPreferenceScreen();
        if (!BuildConfig.DEBUG) {
            screen.removePreference(findPreference(getString(R.string.pref_key_count)));
        } else {
            ApiManager.getInstance(this).getQueryCount(new Callback<CountResult>() {
                @Override
                public void onResponse(Call<CountResult> call, Response<CountResult> response) {
                    screen.findPreference(getString(R.string.pref_key_count)).setSummary(response.body().counts.toString());
                }

                @Override
                public void onFailure(Call<CountResult> call, Throwable t) {

                }
            });
        }
        refreshPaperCache(screen);
        refreshGlideCache(screen);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getPreferenceScreen().setOnPreferenceChangeListener(this);
        getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        getPreferenceScreen().setOnPreferenceChangeListener(null);
        getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            onBackPressed();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
        String key = preference.getKey();
        if (key.equals(getString(R.string.pref_key_original_pic_cache))) {
            clearPaperCache();
            refreshPaperCache(preferenceScreen);
            return true;
        } else if (key.equals(getString(R.string.pref_key_glide_pic_cache))) {
            clearGlideCache();
            refreshGlideCache(preferenceScreen);
            return true;
        }
        return super.onPreferenceTreeClick(preferenceScreen, preference);
    }

    private void refreshPaperCache(PreferenceScreen preferenceScreen) {
        File cacheDir = WowApp.getPaperCacheDir(this);
        File[] files = cacheDir.listFiles();
        long length = 0;
        for (File file : files) {
            length += file.length();
        }
        String fileSize = Formatter.formatFileSize(this, length);
        preferenceScreen.findPreference(getString(R.string.pref_key_original_pic_cache)).setSummary(
                getString(R.string.pref_summary_original_pic_cache, files.length, fileSize)
        );
    }

    private void clearPaperCache() {
        File cacheDir = WowApp.getPaperCacheDir(this);
        File[] files = cacheDir.listFiles();
        for (File file : files) {
            file.delete();
        }
    }

    private void refreshGlideCache(PreferenceScreen preferenceScreen) {
        File cacheDir = WowApp.getGlideCacheDir(this);
        File[] files = cacheDir.listFiles();
        long length = 0;
        for (File file : files) {
            length += file.length();
        }
        String fileSize = Formatter.formatFileSize(this, length);
        String totalSize = Formatter.formatFileSize(this, MyGlideModule.CACHE_SIZE);
        preferenceScreen.findPreference(getString(R.string.pref_key_glide_pic_cache)).setSummary(
                getString(R.string.pref_summary_glide_pic_cache, files.length, fileSize, totalSize)
        );
    }

    private void clearGlideCache() {
        File cacheDir = WowApp.getGlideCacheDir(this);
        File[] files = cacheDir.listFiles();
        for (File file : files) {
            file.delete();
        }
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        Log.v(TAG, "onSharedPreferenceChanged key=" + key + " value=" + sharedPreferences.getBoolean(key, false));

    }

    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        Log.v(TAG, "onPreferenceChange newValue=" + newValue);
        return false;
    }
}

@@ -3817,32 +3817,65 @@
 ong length = 0;%0A
+        if (files != null) %7B%0A    
         for (Fil
@@ -3896,32 +3896,36 @@
 ) %7B%0A            
+    
 length += file.l
@@ -3925,32 +3925,46 @@
  file.length();%0A
+            %7D%0A
         %7D%0A      
@@ -4363,32 +4363,65 @@
 ir.listFiles();%0A
+        if (files != null) %7B%0A    
         for (Fil
@@ -4442,32 +4442,36 @@
 ) %7B%0A            
+    
 file.delete();%0A 
@@ -4461,32 +4461,46 @@
  file.delete();%0A
+            %7D%0A
         %7D%0A    %7D%0A
@@ -4689,32 +4689,65 @@
 ong length = 0;%0A
+        if (files != null) %7B%0A    
         for (Fil
@@ -4768,32 +4768,36 @@
 ) %7B%0A            
+    
 length += file.l
@@ -4797,32 +4797,46 @@
  file.length();%0A
+            %7D%0A
         %7D%0A      
@@ -5325,32 +5325,65 @@
 ir.listFiles();%0A
+        if (files != null) %7B%0A    
         for (Fil
@@ -5404,32 +5404,36 @@
 ) %7B%0A            
+    
 file.delete();%0A 
@@ -5423,32 +5423,46 @@
  file.delete();%0A
+            %7D%0A
         %7D%0A    %7D%0A

fix crash