/*
 * This test program largely adapted from the android SDK camera preview
 * example which has this copyright:
 *
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package lan.my.zooty.andyscan;

import android.app.Activity;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.content.pm.ActivityInfo;
import android.widget.TextView;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.content.Intent;
import java.util.Timer;
import java.util.TimerTask;

// ----------------------------------------------------------------------

public class AndyScanActivity extends Activity {
    private Preview mPreview;
    Camera mCamera;
    Timer mTimer;
    TimerTask mTimerTask;
    int gonetime;

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

        // Hide the window title.
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

        // Let's always be landscape.
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        setContentView(R.layout.main);
    }

    // Fill in everything from scratch in onResume() and free up everything
    // in onPause() - don't bother trying to understand the nuances of the
    // life cycle, just know that onResume() is the last bit called before
    // we are really running and onPause() is the first bit called when we
    // are no longer running, and since this is a highly user interactive
    // app that pretty much has to be in the foreground to be useful, we
    // might as well not pay attention to anything else.

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

        // PUT CODE HERE - load any saved preferences
        mPreview = (Preview)findViewById(R.id.preview_view);
        mPreview.setLightText((TextView)findViewById(R.id.lightView),
                              (TextView)findViewById(R.id.evView));
        mPreview.setMotionText((TextView)findViewById(R.id.motionView));
        mPreview.setMsgText((TextView)findViewById(R.id.msgView));
        mCamera = Camera.open();
        mPreview.setCamera(mCamera);

        // This just tests the use of a timer and getting back to UI thread.
        // Obviously I should use the preference timer value and schedule things
        // differently in the real code.

        gonetime = 5;
        if (mTimer == null) {
           mTimer = new Timer();
        }
        if (mTimerTask == null) {
           mTimerTask =
              new TimerTask() {
                 public void run() {
                    runOnUiThread(new Runnable() {
                          public void run() {
                             String msg = "Seconds gone: " + gonetime;
                             gonetime += 5;
                             ((TextView)findViewById(R.id.msgView)).setText(msg);
                          }
                       });
                 }
              };
           mTimer.schedule(mTimerTask, 5000, 5000);
        }
    }

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

        // Because the Camera object is a shared resource, it's very
        // important to release it when the activity is paused.
        if (mCamera != null) {
            mPreview.setCamera(null);
            mCamera.release();
            mCamera = null;
        }
        if (mTimer != null) {
           mTimer.cancel();
           mTimer = null;
           mTimerTask = null;
        }
    }

    // Handle the options menu.

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.optionsmenu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.configure:
                // bring up preferences activity
                Intent launchPreferencesIntent =
                  new Intent().setClass(this, AndyScanPrefsActivity.class);
                startActivity(launchPreferencesIntent);
                return true;
            case R.id.motioncal:
                // PUT CODE HERE - run calibration test for motion detection
                return true;
            case R.id.lightcal:
                // PUT CODE HERE - run calibration test for light detection
                return true;
            case R.id.startscan:
                // PUT CODE HERE - display countdown and start book scanning
                return true;
            case R.id.exit:
                // Exit is easy - just tell the activity to finish().
                if (mTimer != null) {
                   mTimer.cancel();
                }
                finish();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}
