package app.brest.testmin3d;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.provider.ContactsContract;
import android.support.v4.view.MotionEventCompat;
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.VelocityTracker;
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 app.brest.utils.geometry.Point;
import min3d.core.Object3d;
import min3d.core.Object3dContainer;
import min3d.core.RendererActivity;
import min3d.parser.IParser;
import min3d.parser.Parser;
import min3d.vos.CameraVo;
import min3d.vos.Light;
import min3d.vos.Number3d;
import uk.co.senab.photoview.PhotoViewAttacher;
import uk.co.senab.photoview.log.LogManager;

/**
 * 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 implements View.OnTouchListener {


    private Resource mResource;
    private FrameLayout mFrameLayout;
    private ImageView mImageView;
    private Button mButton;
    private TextView mText;
    private PhotoViewAttacher mAttacher;
    protected float mPreviousX;
    protected float mPreviousY;
    protected float mPreviousDist=0;
    protected CameraVo mCamera = new CameraVo();
    protected boolean mIsZooming = false;
    protected boolean mIsMoving = false;
    @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"))
        {
            Log.e("______", "Type="+mResource.getType());
            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);
                mAttacher.setMaximumScale(20);
            }
            else if(mResource.getType().compareTo("3D")==0) {
                mFrameLayout.addView(_glSurfaceView);
                mImageView.setVisibility(View.INVISIBLE);
                VideoView mc = (VideoView)findViewById(R.id.vv_media);
                mc.setVisibility(View.INVISIBLE);
            }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);
                _glSurfaceView.setOnTouchListener(this);
                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);
            mAttacher.setMaximumScale(20);
            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);

        Light myLight = new Light();
        myLight.position.setZ(150);
        myLight.position.setY(150);
        scene.lights().add(myLight);
        Object3d oo = mResource.get3DModel(this);
        oo.position().z=mResource.getPosition().z;
        oo.position().y=mResource.getPosition().y;
        oo.position().x=mResource.getPosition().x;
        oo.position().z-=20;
        mCamera.frustum.zFar(0.1f);
        mCamera.frustum.zFar(1000);
        scene.addChild(mResource.get3DModel(this));
    }

    @Override
    public void updateScene() {

    }

    public static String actionToString(int action) {
        switch (action) {

            case MotionEvent.ACTION_DOWN: return "Down";
            case MotionEvent.ACTION_MOVE: return "Move";
            case MotionEvent.ACTION_POINTER_DOWN: return "Pointer Down";
            case MotionEvent.ACTION_UP: return "Up";
            case MotionEvent.ACTION_POINTER_UP: return "Pointer Up";
            case MotionEvent.ACTION_OUTSIDE: return "Outside";
            case MotionEvent.ACTION_CANCEL: return "Cancel";
        }
        return "";
    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        float x = motionEvent.getX();
        float y = motionEvent.getY();
        final int action = motionEvent.getAction()%256;
        int index = MotionEventCompat.getActionIndex(motionEvent);
        if (motionEvent.getPointerCount() > 1) {
            switch (action) {
                case MotionEvent.ACTION_MOVE:
                {
                    if(!mIsZooming) break;
                    Point a = new Point(motionEvent.getX(0), motionEvent.getY(0));
                    Point b = new Point(motionEvent.getX(1), motionEvent.getY(1));
                    double d = a.getDistanceWith(b);
                    double delta = d - mPreviousDist;
                    mCamera.position.z+=delta/2;
                    mPreviousDist = (float)a.getDistanceWith(b);
                    scene.camera(mCamera);
                    break;
                }
                case MotionEvent.ACTION_POINTER_DOWN:
                case MotionEvent.ACTION_DOWN: {
                    Point a = new Point(motionEvent.getX(0), motionEvent.getY(0));
                    Point b = new Point(motionEvent.getX(1), motionEvent.getY(1));
                    mPreviousDist = (float)a.getDistanceWith(b);
                    mIsZooming=true;
                    break;
                }
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_POINTER_UP:
                case MotionEvent.ACTION_CANCEL:
                    mIsZooming=false;
                    mIsMoving=false;
                    break;
            }


        }
        else {
            mIsZooming=false;
            switch (motionEvent.getAction()) {
                case MotionEvent.ACTION_MOVE:
                {
                    if(!mIsMoving) break;
                    float dx = x - mPreviousX;
                    float dy = y - mPreviousY;
                    Object3d oo = mResource.get3DModel(this);

                    oo.rotation().y += dx / 3;
                    oo.rotation().x += dy / 3;
                    _glSurfaceView.requestRender();


                    break;
                }
                case MotionEvent.ACTION_DOWN:
                    mIsMoving=true;
                    break;
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_CANCEL:
                    mIsMoving=false;
                    mIsZooming=false;
                    break;
            }
            mPreviousX = x;
            mPreviousY = y;
        }


        return true;

    }

}
