package org.ntlab.radishforandroidstudio.cactusClient.models;

import android.content.res.Resources;
import android.view.MotionEvent;

import net.arnx.jsonic.JSON;

import org.ntlab.radishforandroidstudio.cactusClient.connections.CharacterConnection;
import org.ntlab.radishforandroidstudio.framework.animation.Animation3D;
import org.ntlab.radishforandroidstudio.framework.event.PadEvent;
import org.ntlab.radishforandroidstudio.framework.gameMain.GameBaseModel;
import org.ntlab.radishforandroidstudio.framework.gameMain.OvergroundActor;
import org.ntlab.radishforandroidstudio.framework.listener.PadListener;
import org.ntlab.radishforandroidstudio.framework.model3D.ModelFactory;
import org.ntlab.radishforandroidstudio.framework.model3D.Object3D;
import org.ntlab.radishforandroidstudio.framework.model3D.Position3D;
import org.ntlab.radishforandroidstudio.framework.model3D.Quaternion3D;
import org.ntlab.radishforandroidstudio.framework.model3D.Universe;
import org.ntlab.radishforandroidstudio.framework.network.CallBack;
import org.ntlab.radishforandroidstudio.framework.physics.Solid3D;
import org.ntlab.radishforandroidstudio.framework.physics.Velocity3D;
import org.ntlab.radishforandroidstudio.framework.view3D.Camera3D;
import org.ntlab.radishforandroidstudio.java3d.Appearance;
import org.ntlab.radishforandroidstudio.java3d.Material;
import org.ntlab.radishforandroidstudio.java3d.Vector3d;

public class OwnPlayer implements PadListener, GameBaseModel, CallBack {
    private boolean isTouched = false;
    private float touchX = 0.0f;
    private float touchY = 0.0f;
    private OvergroundActor actor;
    private Camera3D camera;
    private Player player;
    private CharacterConnection con;
    private int nextConnectRenaimdTime = 0;
    private int connectInterval = 300;
    private String playerId;

    public OwnPlayer(Player player, Resources resources, Universe universe, Camera3D camera, String playerId) {
        this.player = player;
        setPlayerId(playerId);

        // キャラクタの作成
        Appearance ap1 = new Appearance();
        Material m = new Material();
        m.setDiffuseColor(0.0f, 0.3f, 1.0f);
        m.setAmbientColor(0.0f, 0.0f, 0.0f);
        m.setEmissiveColor(0.0f, 0.0f, 0.0f);
        m.setSpecularColor(0.0f, 0.0f, 0.0f);
        m.setShininess(5.0f);
        ap1.setMaterial(m);
        Object3D pochaBody = null;
        try {
            pochaBody = ModelFactory.loadModel(resources, "pocha.stl", ap1).createObject();
            Animation3D pochaAnimation = null;    //AnimationFactory.loadAnimation("data\\pocha\\walk.wrl");
            actor = new OvergroundActor(pochaBody, pochaAnimation);
            actor.setPosition(new Position3D(0.0, 1.0, 0.0));
            universe.place(actor);
        } catch (Exception e) {
            e.printStackTrace();
        }

        this.camera = camera;
        camera.setViewPoint(actor.getPosition().add(0.0, 1.5, 0.0));
        camera.setViewLine(actor.getDirection());
        camera.setFieldOfView(1.5);
        camera.setBackClipDistance(10000.0);
        updateCamera();
    }

    public void setPlayerId(String id) {
        playerId = id;
    }

    public void updateCamera() {
        Vector3d charaVector3d = actor.getDirection();
        charaVector3d.normalize();//キャラの向きを単位ベクトルに
        camera.setViewPoint(actor.getPosition()
                .add(-5.0 * charaVector3d.getX(), charaVector3d.getY() + 5.0, -5.0 * charaVector3d.getZ()));//視点
        camera.setViewLine(new Vector3d(5.0 * charaVector3d.getX(), charaVector3d.getY() - 2.5, 5.0 * charaVector3d.getZ() + touchX));//視線
    }

    @Override
    public boolean onEvent(PadEvent event) {
        Vector3d charaVector3d = actor.getDirection();
        charaVector3d.normalize();//キャラの向きを単位ベクトルに
        MotionEvent motionEvent = event.getMotionEvent();

        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN || motionEvent.getAction() == MotionEvent.ACTION_MOVE) {
            isTouched = true;
            touchX = (float) (Math.cos(event.getAngle()) * event.getLength());
            touchY = (float) (Math.sin(event.getAngle()) * event.getLength());


        } else if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
            Velocity3D vel = actor.getVelocity();
            vel.setX(0);
            vel.setY(0);
            actor.setVelocity(vel);
            isTouched = false;
        }
        return false;
    }

    @Override
    public void update(double interval) {
        updateCamera();

        if (isTouched) {
            Velocity3D vel = actor.getVelocity();
            vel.setX(touchX);
            vel.setY(touchY);
            actor.setVelocity(vel);
        }

        nextConnectRenaimdTime -= interval;
        if (nextConnectRenaimdTime <= 0) {
            nextConnectRenaimdTime = connectInterval;
            con = new CharacterConnection(playerId);
            con.setCallBack(this);

            Quaternion3D q = ((Solid3D)(actor.getBody())).getQuaternion();
            player.setPosition(actor.getPosition());
            player.setAngle(q);

            JSON json = new JSON();
            con.addFormParam("characterID", json.encode(player.getCharacterID()));
            con.addFormParam("cameraState", json.encode(player.getCameraState()));
            con.addFormParam("position", json.encode(player.getPosition()));
            con.addFormParam("animationClassToStart", json.encode(player.getEmoteState()));
            con.addFormParam("angle", "{ \"x\":" + q.getX() + ", \"y\":" + q.getY() + ", \"z\":" + q.getZ() + ", \"w\":" + q.getW() + "}");
            con.doPut();
        }
    }

    @Override
    public void setInstanceId(String id) {
        player.setInstanceID(id);
    }

    @Override
    public void onResponse(String response) {

    }
}