package org.ntlab.radishforandroidstudio.cactusClient.views;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import org.ntlab.radishforandroidstudio.R;
import org.ntlab.radishforandroidstudio.cactusClient.models.CactusModels;
import org.ntlab.radishforandroidstudio.cactusClient.models.MovableObjectModel;
import org.ntlab.radishforandroidstudio.cactusClient.models.Object;
import org.ntlab.radishforandroidstudio.cactusClient.models.OtherPlayerCharactersModel;
import org.ntlab.radishforandroidstudio.cactusClient.models.OwnPlayer;
import org.ntlab.radishforandroidstudio.cactusClient.models.Player;
import org.ntlab.radishforandroidstudio.framework.RWT.RWTPad;
import org.ntlab.radishforandroidstudio.framework.RWT.RWTUIFragment;
import org.ntlab.radishforandroidstudio.framework.gameMain.OvergroundActor;
import org.ntlab.radishforandroidstudio.framework.gameMain.RealTime3DFragment;
import org.ntlab.radishforandroidstudio.framework.model3D.BaseObject3D;
import org.ntlab.radishforandroidstudio.framework.physics.Ground;
import org.ntlab.radishforandroidstudio.java3d.AmbientLight;
import org.ntlab.radishforandroidstudio.java3d.Appearance;
import org.ntlab.radishforandroidstudio.java3d.Color3f;
import org.ntlab.radishforandroidstudio.java3d.DirectionalLight;
import org.ntlab.radishforandroidstudio.java3d.IndexedTriangleArray;
import org.ntlab.radishforandroidstudio.java3d.Material;
import org.ntlab.radishforandroidstudio.java3d.Point3d;
import org.ntlab.radishforandroidstudio.java3d.Vector3f;

public class PlayerFragment extends RealTime3DFragment {
    private OvergroundActor pocha;
    RWTPad pad = null;
    private OwnPlayer player;
    private Object objct;
    private OtherPlayerCharactersModel otherPlayerCharactersModel;
    private MovableObjectModel movableObjectModel;

    public PlayerFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        modelManager = new CactusModels(this, universe);

        //環境光
        AmbientLight amblight = new AmbientLight(new Color3f(1.0f, 1.0f, 1.0f));
        universe.placeLight(amblight);
        //平行光源
        DirectionalLight dirlight = new DirectionalLight(
                new Color3f(1.0f, 1.0f, 1.0f),  //光の色
                new Vector3f(0.0f, -1.0f, -0.5f) //光の方向ベクトル
        );
        universe.placeLight(dirlight);

        // 地面の作成
        IndexedTriangleArray groundGeometry = new IndexedTriangleArray(4,
                IndexedTriangleArray.COORDINATES | IndexedTriangleArray.NORMALS, 6);
        groundGeometry.setCoordinate(0, new Point3d(-20.0, -1.0, -20.0));
        groundGeometry.setCoordinate(1, new Point3d(20.0, -1.0, -20.0));
        groundGeometry.setCoordinate(2, new Point3d(20.0, -1.0, 20.0));
        groundGeometry.setCoordinate(3, new Point3d(-20.0, -1.0, 20.0));
        groundGeometry.setNormal(0, new Vector3f(0.0f, 1.0f, 0.0f));
        groundGeometry.setNormal(1, new Vector3f(0.0f, 1.0f, 0.0f));
        groundGeometry.setNormal(2, new Vector3f(0.0f, 1.0f, 0.0f));
        groundGeometry.setNormal(3, new Vector3f(0.0f, 1.0f, 0.0f));
        groundGeometry.setCoordinateIndices(0, new int[]{0, 3, 2});
        groundGeometry.setCoordinateIndices(3, new int[]{0, 2, 1});
        Appearance ap2 = new Appearance();
        Material m2 = new Material();
        m2.setDiffuseColor(0.5f, 1.0f, 0.2f);
        ap2.setMaterial(m2);
        Ground ground = new Ground(new BaseObject3D(groundGeometry, ap2));
        universe.place(ground);
        start(1000L, 50L, true);
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        Bundle bundle = getArguments();
        // プレイヤーの作成とインスタンスIDのセット
        String playerId = bundle.getString("playerId");
        player = new OwnPlayer((Player) bundle.getSerializable("player"), getResources(), universe, camera, playerId);
        modelManager.addModel(player);

        otherPlayerCharactersModel = new OtherPlayerCharactersModel(this, universe);
        otherPlayerCharactersModel.setPlayerId(playerId);
        modelManager.addModel(otherPlayerCharactersModel);
        modelManager.setInstanceId(bundle.getString("instanceId"));
        //オブジェクトの生成
        String objectId =bundle.getString("objectId");
        movableObjectModel = new MovableObjectModel(this ,universe);
        movableObjectModel.setObjectId(objectId);
        modelManager.addModel(movableObjectModel);
        modelManager.setInstanceId(bundle.getString("instanceId"));



        //画面の生成
        initGameWindowView();
        return parentView;
    }

    @Override
    protected void progress(long interval) {
        super.progress(interval);
    }

    public void initGameWindowView() {
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();

        RWTUIFragment f = new RWTUIFragment();
        f.setListener(this);
        View uiLayout = parentView.findViewById(R.id.ui_layout);
        uiLayout.setOnTouchListener(f);
        transaction.add(R.id.ui_layout, f);
        transaction.commit();
    }

    public void onCreateFragmentEvent(RWTUIFragment f) {
        pad = (RWTPad) f.findViewById(org.ntlab.radishforandroidstudio.R.id.pad);
        pad.addListener(player);
    }
}

