package com.ociweb.demo;

import java.util.concurrent.TimeUnit;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class HomeActivity extends Activity implements OnClickListener, HomeModelListener {

  private Button mStartButton;
  private TextView mStatusLabel;
  
  private static final String ENABLED_KEY = "com.ociweb.buttonEnabled";
  private static final String HOME_MODEL_KEY = "com.ociweb.homeModel";

  // background threads use this Handler to post messages to
  // the main application thread
  private final Handler mHandler = new Handler();
  
  // this data model knows when a thread is fetching data
  private HomeModel mHomeModel;

  // post this to the Handler when the background thread completes
  private final Runnable mCompleteRunnable = new Runnable() {
    public void run() {
      onThreadCompleted();
    }
  };

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

    mStartButton = (Button) findViewById(R.id.start_background_thread_btn);
    mStatusLabel = (TextView) findViewById(R.id.thread_status_label);

    mStartButton.setOnClickListener(this);
    
    if (savedInstanceState != null) {
      if (savedInstanceState.containsKey(ENABLED_KEY)) {
        mStartButton.setEnabled(savedInstanceState.getBoolean(ENABLED_KEY));
      }
      if (savedInstanceState.containsKey(HOME_MODEL_KEY)) {
        mHomeModel = (HomeModel) savedInstanceState.getSerializable(HOME_MODEL_KEY);
      }
    }
    if (mHomeModel == null) {
      mHomeModel = new HomeModel();
    }
    mHomeModel.setHomeModelListener(this);
    updateDisplay();
  }

  public void homeModelChanged(HomeModel hm) {
    updateDisplay();
  }

  @Override
  protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putBoolean(ENABLED_KEY, mStartButton.isEnabled());
    outState.putSerializable(HOME_MODEL_KEY, mHomeModel);
  }

  public void onClick(View v) {
    if (v == mStartButton) {
      
      

      mStartButton.setEnabled(false);
      mStatusLabel.setText(R.string.thread_running);
      Toast.makeText(this, R.string.thread_running, Toast.LENGTH_SHORT).show();

      Thread t = new Thread() {
        public void run() {
          // perform expensive tasks in a background thread
          expensiveOperation();

          // let the UI know the task is complete
          mHandler.post(mCompleteRunnable);
        }
      };
      t.start();
    }
  }

  /**
   * Call this method on the main application thread once the background thread
   * completes.
   */
  private void onThreadCompleted() {
    mStartButton.setEnabled(true);
    mStatusLabel.setText(R.string.thread_finished);
    Toast.makeText(this, R.string.thread_finished, Toast.LENGTH_SHORT).show();
  }
  
  private void updateDisplay() {
    if (mHomeModel == null) {
      return;
    }
    
    HomeModel.State state = mHomeModel.getState();
    
    
    mStartButton.setEnabled(state != HomeModel.State.RUNNING);
    
    switch (state) {
    case RUNNING:
      mStatusLabel.setText(R.string.thread_running);
      break;
    case NOT_STARTED:
      mStatusLabel.setText(R.string.thread_not_started);
      break;
    case FINISHED:
      mStatusLabel.setText(R.string.thread_finished);
      break;
    }
  }
}