package app.mar.activities;

import android.support.v4.view.MotionEventCompat;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;

import app.mar.utils.FontChangeCrawler;
import app.mar.game.Resource;
import app.mar.utils.geometry.Point;
import min3d.core.Object3d;
import min3d.core.RendererActivity;
import min3d.vos.CameraVo;
import min3d.vos.Light;
import min3d.vos.LightType;

public class ModelViewerActivity extends RendererActivity  implements View.OnTouchListener {

    private Resource mResource;
    private Button mInfo;
    private TextView mTitle;
    private FrameLayout mFrameLayout;

    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_model_viewer);

        mInfo = (Button) findViewById(R.id.info);
        mFrameLayout = (FrameLayout) findViewById(R.id.frame_viewer);
        mTitle = (TextView) findViewById(R.id.title);

        if(getIntent().hasExtra("resource"))
        {
            mResource = (Resource) getIntent().getSerializableExtra("resource");
            if(mResource!=null) setTitle(mResource.getTitle());
            mFrameLayout.addView(_glSurfaceView);
            mTitle.setText(mResource.getTitle());
        }else
        {
            mTitle.setText("Error: Res not found");
        }
        FontChangeCrawler.setFont(this);


    }

    public void onClickInfo(View v)
    {

    }

    public void initScene()
    {

        scene.backgroundColor().setAll(0x00000000);

        Light myLight = new Light();
        myLight.position.setZ(0);
        myLight.position.setY(0);
        myLight.type(LightType.POSITIONAL);

        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() {

    }

    @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;

    }
}
