/* 
 * Date: 15.08.2016
 * Author: Etienne Frank
 * Website: www.etiennefrank.com
 * Mail: mail@etiennefrank.com
 * Info: Feel free to use it in any project. It would be pleased to mention the author name. 
 * 	     By the way the author can be hired.
 *
*/

using UnityEngine;
using System;
using System.Collections;
using UnityEngine.UI;

public class LightSensorFunctions : MonoBehaviour
{
    public float DetectingLuxIntensity = 1000;

    private AndroidJavaObject activityContext = null;
    private AndroidJavaObject jo = null;
    AndroidJavaClass activityClass = null;

    public Text Label;

    void Start()
    {
#if UNITY_ANDROID
        activityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        activityContext = activityClass.GetStatic<AndroidJavaObject>("currentActivity");

        jo = new AndroidJavaObject("com.etiennefrank.lightsensorlib.LightSensorLib");
        jo.Call("init", activityContext);
#endif
    }

    private void Update()
    {
        Label.text = "" + GetLux();

        float intensity = GetLux();
        if (intensity > DetectingLuxIntensity)
            GameManager.Singleton.ChargeBattery(intensity / DetectingLuxIntensity * Time.deltaTime);
        else
            GameManager.Singleton.ChargeBattery(0);
    }

    public float GetLux()
    {
#if UNITY_ANDROID
        return jo.Call<float>("getLux");
#endif
    }
}
