/*
 * Copyright 2016 aContextLib 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 uk.ac.mdx.cs.ie.acontextlib.sample;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import java.util.Map;

import uk.ac.mdx.cs.ie.acontextlib.IContextReceiver;
import uk.ac.mdx.cs.ie.acontextlib.hardware.BatteryContext;
import uk.ac.mdx.cs.ie.acontextlib.hardware.GPSIndoorOutdoorContext;
import uk.ac.mdx.cs.ie.acontextlib.hardware.LightContext;
import uk.ac.mdx.cs.ie.acontextlib.hardware.StepCounter;

/**
 * Sample Activity to demonstrate context components.
 *
 * @author Dean Kramer <d.kramer@mdx.ac.uk>
 */
public class MainActivity extends Activity implements IContextReceiver {

    private LightContext mLightContext;
    private BatteryContext mBatteryContext;
    private GPSIndoorOutdoorContext mIndoorContext;
    private StepCounter mStepCounter;
    private Context mContext;
    private TextView mLightLevel;
    private TextView mBatteryLevel;
    private TextView mIndoorStatus;
    private TextView mStepCount;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mLightLevel = (TextView) findViewById(R.id.txtLight);
        mBatteryLevel = (TextView) findViewById(R.id.txtBattery);
        mIndoorStatus = (TextView) findViewById(R.id.txtIndoor);
        mStepCount = (TextView) findViewById(R.id.txtSteps);


        mContext = getApplicationContext();
        mLightContext = new LightContext(mContext);
        mBatteryContext = new BatteryContext(mContext);
        mIndoorContext = new GPSIndoorOutdoorContext(mContext);
        mStepCounter = new StepCounter(mContext);


        mLightContext.addContextReceiver(this);
        mBatteryContext.addContextReceiver(this);
        mIndoorContext.addContextReceiver(this);
        mStepCounter.addContextReceiver(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_start) {
            startContexts();
            return true;
        } else if (id == R.id.action_stop) {
            stopContexts();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private void startContexts() {
        mLightContext.start();
        mBatteryContext.start();
        mIndoorContext.start();
        mStepCounter.start();
    }

    private void stopContexts() {
        mLightContext.stop();
        mBatteryContext.stop();
        mIndoorContext.stop();
        mStepCounter.stop();
    }

    @Override
    public void onResume() {
        super.onResume();
        startContexts();
    }

    @Override
    public void onPause() {
        super.onPause();
        stopContexts();
    }

    //Context Receiver Implementations

    @Override
    public void newContextValue(String name, final long value) {
        if (name.equals(LightContext.RECEIVER_LIGHT)) {
            mLightLevel.setText(String.valueOf(value));
        } else if (name.equals(BatteryContext.RECEIVER_BATTERY)) {
            mBatteryLevel.setText(String.valueOf(value));
        } else if (name.equals(StepCounter.RECEIVER_STEPS)) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mStepCount.setText(String.valueOf(value));
                }
            });
        }
    }

    @Override
    public void newContextValue(String name, double value) {

    }

    @Override
    public void newContextValue(final String name, final boolean value) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (name.equals(GPSIndoorOutdoorContext.RECEIVER_INDOOR_OUTDOOR)) {
                    mIndoorStatus.setText(String.valueOf(value));
                }
            }
        });
    }

    @Override
    public void newContextValue(String name, String value) {

    }

    @Override
    public void newContextValue(String name, Object value) {

    }

    @Override
    public void newContextValues(Map<String, String> values) {

    }

}

@@ -944,23 +944,24 @@
 .Battery
-Context
+Observer
 ;%0Aimport
@@ -1014,23 +1014,24 @@
 rOutdoor
-Context
+Observer
 ;%0Aimport
@@ -1073,23 +1073,24 @@
 re.Light
-Context
+Observer
 ;%0Aimport
@@ -1138,16 +1138,24 @@
 pCounter
+Observer
 ;%0A%0A/**%0A 
@@ -1349,23 +1349,24 @@
 te Light
-Context
+Observer
  mLightC
@@ -1392,23 +1392,24 @@
  Battery
-Context
+Observer
  mBatter
@@ -1446,23 +1446,24 @@
 rOutdoor
-Context
+Observer
  mIndoor
@@ -1494,16 +1494,24 @@
 pCounter
+Observer
  mStepCo
@@ -2183,23 +2183,24 @@
 ew Light
-Context
+Observer
 (mContex
@@ -2240,23 +2240,24 @@
  Battery
-Context
+Observer
 (mContex
@@ -2305,23 +2305,24 @@
 rOutdoor
-Context
+Observer
 (mContex
@@ -2363,16 +2363,24 @@
 pCounter
+Observer
 (mContex
@@ -4117,23 +4117,24 @@
 ls(Light
-Context
+Observer
 .RECEIVE
@@ -4239,23 +4239,24 @@
 (Battery
-Context
+Observer
 .RECEIVE
@@ -4369,16 +4369,24 @@
 pCounter
+Observer
 .RECEIVE
@@ -4928,23 +4928,24 @@
 rOutdoor
-Context
+Observer
 .RECEIVE

Update sample app with new observer names