/* 
 * Copyright 2015 eje inc.
 *
 * 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 com.eje_c.meganekko.scene_objects;

import com.eje_c.meganekko.Picker;
import com.eje_c.meganekko.SceneObject;
import com.eje_c.meganekko.VrContext;
import com.eje_c.meganekko.VrFrame;
import com.eje_c.meganekko.scene_objects.CanvasSceneObject.OnDrawListener;

import android.graphics.Canvas;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import ovr.JoyButton;

/**
 * A {@linkplain SceneObject scene object} that renders Android's standard
 * {@code View}. Currently {@code ViewGroup}s are not supported. You can only
 * use {@code Button}, {@code TextView},{@code ImageView} or other {@code View}s
 * which renders its contents.
 */
public class ViewSceneObject extends CanvasSceneObject implements OnDrawListener {

    private View mView;
    private boolean mLooking;
    private boolean mSimulatePressing = true;

    public ViewSceneObject(VrContext vrContext) {
        super(vrContext);
    }

    /**
     * Set view.
     * 
     * @param view
     */
    public void setView(View view) {
        this.mView = view;

        if (mView != null) {
            getVrContext().getActivity().runOnUiThread(addContentViewAndStartRendering);
        } else {
            clearOnDrawListener();
        }
    }

    /**
     * Set view from XML resource.
     * 
     * @param xmlRes
     */
    public void setView(int xmlRes) {
        LayoutInflater layoutInflater = LayoutInflater.from(getVrContext().getContext());
        View view = layoutInflater.inflate(xmlRes, null);
        setView(view);
    }

    /**
     * Get view.
     * 
     * @return
     */
    public View getView() {
        return mView;
    }

    @Override
    protected boolean onRender() {

        if (mView != null) {
            // Update texture size
            setCanvasSize(mView.getWidth(), mView.getHeight());
            
            // call on
            return super.onRender();
        }
        
        return false;
    }

    @Override
    public void onDraw(CanvasSceneObject object, Canvas canvas, VrFrame vrFrame) {

        if (mView != null) {

            // Simulate pressing
            if (mSimulatePressing && getEyePointeeHolder() != null) {
                float distance = Picker.pickSceneObject(this, getVrContext().getActivity().getScene().getMainCamera());
                if (distance < Float.POSITIVE_INFINITY) {
                    if (!mLooking) {
                        mLooking = true;
                        getVrContext().getActivity().runOnUiThread(updateViewState);
                    }
                } else {
                    if (mLooking) {
                        mLooking = false;
                        getVrContext().getActivity().runOnUiThread(updateViewState);
                    }
                }
            }

            // Simulate click
            if (mLooking) {
                if (JoyButton.contains(vrFrame.getButtonPressed(), JoyButton.BUTTON_TOUCH_SINGLE)) {
                    mView.callOnClick();
                } else if (JoyButton.contains(vrFrame.getButtonPressed(), JoyButton.BUTTON_TOUCH_LONGPRESS)) {
                    mView.performLongClick();
                }
            }
            mView.draw(canvas);
        }
    }

    private final Runnable addContentViewAndStartRendering = new Runnable() {
        @Override
        public void run() {

            if (mView == null)
                return;

            getVrContext().getActivity().addContentView(mView,
                    new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            mView.setVisibility(View.INVISIBLE);

            setOnDrawListener(ViewSceneObject.this);
        }
    };

    private final Runnable updateViewState = new Runnable() {
        @Override
        public void run() {

            if (mView == null)
                return;

            mView.setPressed(mLooking);
        }
    };
}

@@ -954,52 +954,8 @@
 ew;%0A
-import android.view.ViewGroup.LayoutParams;%0A
 impo
@@ -972,16 +972,16 @@
 Button;%0A
+
 %0A/**%0A * 
@@ -1660,32 +1660,33 @@
 View != null) %7B%0A
+%0A
             getV
@@ -1685,82 +1685,498 @@
     
-getVrContext().getActivity().runOnUiThread(addContentViewAndStartR
+mView.measure(0, 0);%0A            mView.layout(0, 0, mView.getMeasuredWidth(), mView.getMeasuredHeight());%0A%0A            // if mesh is missing, assign quad mesh%0A            if (getRenderData().getMesh() == null) %7B%0A                getRenderData()%0A                        .setMesh(getVrContext().createQuad(mView.getWidth() * 0.002f, mView.getHeight() * 0.002f));%0A            %7D%0A%0A            setCanvasSize(mView.getWidth(), mView.getHeight());%0A%0A            setOnDrawList
 en
-d
 er
-ing
+(ViewSceneObject.this
 );%0A 
@@ -2656,307 +2656,8 @@
  %7D%0A%0A
-    @Override%0A    protected boolean onRender() %7B%0A%0A        if (mView != null) %7B%0A            // Update texture size%0A            setCanvasSize(mView.getWidth(), mView.getHeight());%0A            %0A            // call on%0A            return super.onRender();%0A        %7D%0A        %0A        return false;%0A    %7D%0A%0A
     
@@ -3924,473 +3924,15 @@
   %7D%0A
+
     %7D%0A%0A
-    private final Runnable addContentViewAndStartRendering = new Runnable() %7B%0A        @Override%0A        public void run() %7B%0A%0A            if (mView == null)%0A                return;%0A%0A            getVrContext().getActivity().addContentView(mView,%0A                    new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));%0A            mView.setVisibility(View.INVISIBLE);%0A%0A            setOnDrawListener(ViewSceneObject.this);%0A        %7D%0A    %7D;%0A%0A
     

Improve ViewSceneObject performance