package org.commcare.dalvik.activities;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

import org.commcare.android.framework.CommCareActivity;
import org.commcare.android.framework.UiElement;
import org.commcare.android.tasks.UpgradeAppTask;
import org.commcare.dalvik.R;
import org.commcare.dalvik.application.CommCareApplication;
import org.commcare.dalvik.dialogs.CustomProgressDialog;
import org.javarosa.core.services.locale.Localization;

import static java.lang.Thread.sleep;

/**
 * Allow user to manage app upgrading:
 *  - Check and downloading new latest upgrade
 *  - Stop an upgrade download
 *  - Apply a downloaded upgrade
 *
 * @author Phillip Mates (pmates@dimagi.com)
 */
public class UpgradeActivity extends CommCareActivity {
    private static final String TAG = UpgradeActivity.class.getSimpleName();

    private ProgressBar mProgress;
    private int mProgressStatus = 0;
    private boolean areResourcesInitialized = false;
    private String incomingRef;

    private Handler mHandler = new Handler();

    private enum UpgradeUiState {
        idle,
        checking,
        downloading,
        unappliedInstall
    }
    private UpgradeUiState currentUiState;

    @UiElement(R.id.check_for_upgrade_button)
    Button checkUpgradeButton;

    @UiElement(R.id.stop_upgrade_download_button)
    Button stopUpgradeButton;

    @UiElement(R.id.install_upgrade_button)
    Button installUpgradeButton;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // setup UI
        setContentView(R.layout.upgrade_activity);
        mProgress = (ProgressBar)findViewById(R.id.upgrade_progress_bar);
        setupButtonListeners();

        // attempt to attach to auto-update task
        setStateFromRunningTask();

        // update UI based on current state
        setupButtonState();

        // Start lengthy operation in a background thread
        new Thread(new Runnable() {
            public void run() {
                while (mProgressStatus < 100) {
                    mProgressStatus = doWork();

                    // Update the progress bar
                    mHandler.post(new Runnable() {
                        public void run() {
                            mProgress.setProgress(mProgressStatus);
                        }
                    });
                }
            }
        }).start();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

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

    @Override
    protected void onPause() {
        super.onPause();
    }

    public int doWork() {
        try {
            sleep(1000);
        } catch (Exception e) {
        }
        return mProgressStatus + 1;
    }

    private void setupButtonListeners() {
        checkUpgradeButton = (Button)findViewById(R.id.check_for_upgrade_button);
        checkUpgradeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startUpgradeCheck();
            }
        });

        stopUpgradeButton = (Button)findViewById(R.id.stop_upgrade_download_button);
        stopUpgradeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });

        installUpgradeButton = (Button)findViewById(R.id.install_upgrade_button);
        installUpgradeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });
    }

    private void setupButtonState() {
    }

    private void startUpgradeCheck() {
        if (currentUiState == UpgradeUiState.idle) {
            UpgradeAppTask upgradeTask = new UpgradeAppTask(CommCareApplication._().getCurrentApp(), false);
            upgradeTask.connect(this);
            upgradeTask.execute(incomingRef);
        }
    }

    @Override
    public CustomProgressDialog generateProgressDialog(int taskId) {
        String title, message;
        if (areResourcesInitialized) {
            title = Localization.get("updates.title");
            message = Localization.get("updates.checking");
        } else {
            title = Localization.get("updates.resources.initialization");
            message = Localization.get("updates.resources.profile");
        }
        CustomProgressDialog dialog = CustomProgressDialog.newInstance(title, message, taskId);
        dialog.setCancelable(false);
        String checkboxText = Localization.get("updates.keep.trying");
        CustomProgressDialog lastDialog = getCurrentDialog();
        boolean isChecked = (lastDialog != null) && lastDialog.isChecked();
        dialog.addCheckbox(checkboxText, isChecked);
        dialog.addProgressBar();
        return dialog;
    }

    private void setStateFromRunningTask() {
        UpgradeAppTask.UpgradeTaskState upgradeTaskState =
            UpgradeAppTask.registerActivityWithRunningTask(this);
        switch (upgradeTaskState) {
            case checking:
                currentUiState = UpgradeUiState.checking;
                break;
            case downloading:
                currentUiState = UpgradeUiState.downloading;
                break;
            case notRunning:
                currentUiState = pendingUpgradeOrIdle();
                break;
            default:
                currentUiState = pendingUpgradeOrIdle();
        }
    }

    private UpgradeUiState pendingUpgradeOrIdle() {
        if (downloadedUpgradePresent()) {
            return UpgradeUiState.unappliedInstall;
        } else {
            return UpgradeUiState.idle;
        }
    }

    private boolean downloadedUpgradePresent() {
        return false;
    }
}

@@ -1666,28 +1666,8 @@
 );%0A%0A
-        // setup UI%0A
     
@@ -1713,16 +1713,16 @@
 ivity);%0A
+
         
@@ -1832,48 +1832,124 @@
     
-// attempt to attach to auto-update task
+UpgradeAppTask.UpgradeTaskState upgradeTaskState =%0A            UpgradeAppTask.registerActivityWithRunningTask(this);
 %0A   
@@ -1952,24 +1952,26 @@
 %0A        set
+Ui
 StateFromRun
@@ -1979,16 +1979,32 @@
 ingTask(
+upgradeTaskState
 );%0A%0A    
@@ -4017,24 +4017,921 @@
 onState() %7B%0A
+        switch (currentUiState) %7B%0A            case idle:%0A                checkUpgradeButton.setEnabled(true);%0A                stopUpgradeButton.setEnabled(false);%0A                installUpgradeButton.setEnabled(false);%0A                break;%0A            case checking:%0A            case downloading:%0A                checkUpgradeButton.setEnabled(false);%0A                stopUpgradeButton.setEnabled(true);%0A                installUpgradeButton.setEnabled(false);%0A                break;%0A            case unappliedInstall:%0A                checkUpgradeButton.setEnabled(true);%0A                stopUpgradeButton.setEnabled(false);%0A                installUpgradeButton.setEnabled(true);%0A                break;%0A            default:%0A                checkUpgradeButton.setEnabled(false);%0A                stopUpgradeButton.setEnabled(false);%0A                installUpgradeButton.setEnabled(false);%0A        %7D%0A
     %7D%0A%0A    p
@@ -6144,16 +6144,18 @@
 void set
+Ui
 StateFro
@@ -6167,28 +6167,16 @@
 ingTask(
-) %7B%0A        
 UpgradeA
@@ -6219,76 +6219,11 @@
 tate
- =%0A            UpgradeAppTask.registerActivityWithRunningTask(this);
+) %7B
 %0A   

Set button state from ui state field