package app.brest.testmin3d;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.provider.ContactsContract;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.VideoView;

import java.io.InputStream;
import java.util.ArrayList;

import app.brest.utils.app.brest.game.Resource;
import min3d.core.Object3d;
import min3d.core.Object3dContainer;
import min3d.core.RendererActivity;
import min3d.parser.IParser;
import min3d.parser.Parser;
import min3d.vos.Light;
import uk.co.senab.photoview.PhotoViewAttacher;

/**
 * An example full-screen activity that shows and hides the system UI (i.e.
 * status bar and navigation/system bar) with user interaction.
 */
public class ViewerActivity extends RendererActivity {


    private Resource mResource;
    private FrameLayout mFrameLayout;
    private ImageView mImageView;
    private Button mButton;
    private TextView mText;
    private PhotoViewAttacher mAttacher;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_viewer);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        mFrameLayout = (FrameLayout) findViewById(R.id.frame_viewer);
        mImageView = (ImageView) findViewById(R.id.imageView);
        mButton = (Button) findViewById(R.id.button_info);
        mText = (TextView) findViewById(R.id.tv_comment);

        if(getIntent().hasExtra("resource"))
        {
            mResource = (Resource) getIntent().getSerializableExtra("resource");
            if(mResource!=null) setTitle(mResource.getTitle());
        }

        VideoView m_c = (VideoView)findViewById(R.id.vv_media);
        Button title = (Button)findViewById(R.id.b_title);
        m_c.setVisibility(View.INVISIBLE);
        if(mResource != null && !getIntent().hasExtra("information") && !getIntent().hasExtra("map"))
        {
            title.setText(mResource.getTitle());
            if(mResource.getType().compareTo("Image")==0)
            {
                int id = getResources().getIdentifier(mResource.getName(),"drawable", getPackageName());

                mImageView.setImageResource(id);
                mAttacher = new PhotoViewAttacher(mImageView);
            }
            else if(mResource.getType().compareTo("3D")==0) {
                mFrameLayout.addView(_glSurfaceView);
            }else
            {
                String path="android.resource://app.brest.testmin3d/raw/"+mResource.getName();
                if(mResource.getType().compareTo("Audio")==0) path+="_audio";
                else path+="_video";
                mImageView.setVisibility(View.INVISIBLE);
                _glSurfaceView.setVisibility(View.INVISIBLE);
                VideoView mc = (VideoView)findViewById(R.id.vv_media);
                mc.setVisibility(View.VISIBLE);
                Uri video = Uri.parse(path);
                mc.setVideoURI(video);
                mc.start();
            }

        } else if(getIntent().hasExtra("map") )
        {
            int id = getResources().getIdentifier(getIntent().getStringExtra("map"),"drawable", getPackageName());
            title.setText("Carte");
            mImageView.setImageResource(id);
            mAttacher = new PhotoViewAttacher(mImageView);
            mButton.setVisibility(View.INVISIBLE);
        }
        else if(mResource != null && getIntent().hasExtra("information"))
        {
            title.setText(mResource.getTitle());
            mButton.setVisibility(View.INVISIBLE);
            mText.setVisibility(View.VISIBLE);
            mText.setText(mResource.getComment());
            //mFrameLayout.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
        }




    }

    public void onClickInfo(View v)
    {
        Intent intent = new Intent(this, ViewerActivity.class);
        intent.putExtra("resource", mResource);
        intent.putExtra("information", true);
        startActivity(intent);
    }

    public void initScene()
    {

        scene.backgroundColor().setAll(0x00000000);
        scene.lights().add(new Light());
        scene.lights().add(new Light());

        Light myLight = new Light();
        myLight.position.setZ(150);
        scene.lights().add(myLight);
        scene.addChild(mResource.get3DModel(this));
    }

    @Override
    public void updateScene() {

    }

}
