package cgeo.geocaching.activity;

import cgeo.geocaching.ui.dialog.CustomProgressDialog;
import cgeo.geocaching.utils.Log;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Message;
import android.view.WindowManager;

/**
 * progress dialog wrapper for easier management of resources
 */
public class Progress {

    private ProgressDialog dialog;
    private int progress = 0;
    private int progressDivider = 1;
    private boolean hideAbsolute = false;

    public Progress(boolean hideAbsolute) {
        this.hideAbsolute = hideAbsolute;
    }

    public Progress() {
        this(false);
    }

    public synchronized void dismiss() {
        if (dialog != null && dialog.isShowing()) {
            try {
                dialog.dismiss();
            } catch (final Exception e) {
                Log.e("Progress.dismiss", e);
            }
        }
        dialog = null;
    }

    public synchronized void show(final Context context, final String title, final String message, final boolean indeterminate, final Message cancelMessage) {
        if (!isShowing()) {
            createProgressDialog(context, title, message, cancelMessage);
            dialog.setIndeterminate(indeterminate);
            dialog.show();
        }
    }

    public synchronized void show(final Context context, final String title, final String message, final int style, final Message cancelMessage) {
        if (!isShowing()) {
            createProgressDialog(context, title, message, cancelMessage);
            dialog.setProgressStyle(style);
            dialog.show();
        }
    }

    private void createProgressDialog(Context context, String title, String message, Message cancelMessage) {
        if (hideAbsolute) {
            dialog = new CustomProgressDialog(context);
        }
        else {
            dialog = new ProgressDialog(context);
        }
        dialog.setTitle(title);
        dialog.setMessage(message);
        if (cancelMessage != null) {
            dialog.setCancelable(true);
            dialog.setCancelMessage(cancelMessage);
            dialog.setButton(DialogInterface.BUTTON_NEGATIVE, context.getResources().getString(android.R.string.cancel), cancelMessage);
        } else {
            dialog.setCancelable(false);
        }
        dialog.setProgress(0);
        dialog.setCanceledOnTouchOutside(false);
        dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        this.progress = 0;
    }

    public synchronized void setMessage(final String message) {
        if (dialog != null && dialog.isShowing()) {
            dialog.setMessage(message);
        }
    }

    public synchronized boolean isShowing() {
        return dialog != null && dialog.isShowing();
    }

    public synchronized void setMaxProgressAndReset(final int max) {
        if (dialog != null && dialog.isShowing()) {
            final int modMax = max / this.progressDivider;
            dialog.setMax(modMax);
            dialog.setProgress(0);
        }
        this.progress = 0;
    }

    public synchronized void setProgress(final int progress) {
        final int modProgress = progress / this.progressDivider;
        if (dialog != null && dialog.isShowing()) {
            dialog.setProgress(modProgress);
        }
        this.progress = modProgress;
    }

    public synchronized int getProgress() {
        if (dialog != null) {
            dialog.getProgress();
        }
        return this.progress;
    }

    public synchronized void setProgressDivider(final int progressDivider) {
        this.progressDivider = progressDivider;
    }
}

@@ -485,24 +485,30 @@
 er = 1;%0A    
+final 
 private bool
@@ -527,16 +527,8 @@
 lute
- = false
 ;%0A%0A 
@@ -728,33 +728,8 @@
 if (
-dialog != null && dialog.
 isSh
@@ -1665,16 +1665,22 @@
 sDialog(
+final 
 Context 
@@ -1687,16 +1687,22 @@
 context,
+ final
  String 
@@ -1707,16 +1707,22 @@
 g title,
+ final
  String 
@@ -1729,16 +1729,22 @@
 message,
+ final
  Message
@@ -1769,20 +1769,25 @@
         
-if (
+dialog = 
 hideAbso
@@ -1794,32 +1794,10 @@
 lute
-) %7B%0A            dialog =
+ ?
  new
@@ -1830,55 +1830,10 @@
 ext)
-;%0A        %7D%0A        else %7B%0A            dialog =
+ :
  new
@@ -1858,26 +1858,16 @@
 ntext);%0A
-        %7D%0A
         
@@ -2823,33 +2823,8 @@
 if (
-dialog != null && dialog.
 isSh
@@ -3127,32 +3127,32 @@
 rogressDivider;%0A
+
         if (dial
@@ -3151,33 +3151,8 @@
 if (
-dialog != null && dialog.
 isSh

clean progress dialog handling