package electrosphere.client.ui.menu.debug.entity;

import java.util.Set;
import java.util.Random;

import electrosphere.engine.Globals;
import electrosphere.entity.Entity;
import electrosphere.entity.EntityUtils;
import electrosphere.entity.state.AnimationPriorities;
import electrosphere.entity.types.common.CommonEntityUtils;
import electrosphere.entity.types.tree.ProceduralTree;
import electrosphere.game.data.common.CommonEntityType;
import electrosphere.logger.LoggerInterface;
import electrosphere.renderer.actor.Actor;
import electrosphere.renderer.actor.ActorAnimationMask;
import electrosphere.renderer.actor.ActorMeshMask;
import electrosphere.renderer.anim.AnimChannel;
import electrosphere.renderer.anim.Animation;
import electrosphere.renderer.model.Bone;
import electrosphere.renderer.model.Mesh;
import electrosphere.renderer.model.Model;
import imgui.ImGui;

/**
 * The actor tab on the entity detail view
 */
public class ImGuiEntityActorTab {
    
    /**
     * Client scene entity view
     */
    protected static void drawActorView(boolean show, Entity detailViewEntity){
        if(show && ImGui.collapsingHeader("Actor Details")){
            ImGui.indent();
            if(detailViewEntity != null && EntityUtils.getActor(detailViewEntity) != null){
                Actor actor = EntityUtils.getActor(detailViewEntity);
                CommonEntityType commonEntityData = CommonEntityUtils.getCommonData(detailViewEntity);

                //procedural model
                if(commonEntityData != null && commonEntityData.getGraphicsTemplate().getProceduralModel() != null && ImGui.collapsingHeader("Procedural Model")){
                    ImGui.text("Procedural Model path: " + commonEntityData.getGraphicsTemplate().getProceduralModel());
                    if(ImGui.button("Regenerate")){
                        if(commonEntityData.getGraphicsTemplate().getProceduralModel().getTreeModel() != null){
                            ProceduralTree.setProceduralActor(
                                detailViewEntity,
                                commonEntityData.getGraphicsTemplate().getProceduralModel().getTreeModel(),
                                new Random()
                            );
                        }
                    }
                } else {
                    ImGui.text("Model path: " + actor.getModelPath());
                    Model loadedModel = Globals.assetManager.fetchModel(actor.getModelPath());
                    ImGui.text("Model is loaded: " + (loadedModel != null));
                }
    
                //mesh mask
                if(ImGui.collapsingHeader("Mesh Mask")){
                    ActorMeshMask meshMask = actor.getMeshMask();
                    ImGui.text("To Draw Meshes:");
                    for(Mesh mesh : meshMask.getToDrawMeshes()){
                        ImGui.text(mesh.getMeshName());
                    }
                    ImGui.text("Blocked Meshes:");
                    for(String blocked : meshMask.getBlockedMeshes()){
                        ImGui.text(blocked);
                    }
                }

                //animation queue
                if(ImGui.collapsingHeader("Animation Queue")){
                    Set<ActorAnimationMask> animationQueue = actor.getAnimationQueue();
                    for(ActorAnimationMask mask : animationQueue){
                        ImGui.text(mask.getAnimationName() + " -  " + mask.getPriority());
                        ImGui.text(mask.getDuration() + " " + mask.getTime());
                    }
                }

                //bone values
                if(ImGui.collapsingHeader("Bone Values")){
                    for(Bone bone : actor.getBoneValues()){
                        ImGui.text(bone.boneID);
                        ImGui.text("Position: " + actor.getBonePosition(bone.boneID));
                        ImGui.text("Rotation: " + actor.getBoneRotation(bone.boneID));
                        ImGui.text(bone.getFinalTransform() + "");
                    }
                }

                //Draws all the bones in the world
                if(ImGui.button("Draw Bones")){
                    Globals.renderingEngine.getDebugContentPipeline().getDebugBonesPipeline().setEntity(detailViewEntity);
                }
    
                //Browsable list of all animations with their data
                if(ImGui.collapsingHeader("Animation Channel Data")){
                    Model model = Globals.assetManager.fetchModel(actor.getModelPath());
                    ImGui.indent();
                    for(Animation animation : model.getAnimations()){
                        if(ImGui.collapsingHeader(animation.name)){
                            if(ImGui.button("Play")){
                                actor.playAnimation(animation.name, AnimationPriorities.getValue(AnimationPriorities.MODIFIER_MAX));
                            }
                            for(AnimChannel channel : animation.channels){
                                ImGui.pushID(channel.getNodeID());
                                if(ImGui.button("Fully describe")){
                                    channel.fullDescribeChannel();
                                }
                                ImGui.text("=" + channel.getNodeID() + "=");
                                ImGui.text("" + channel.getCurrentPosition());
                                ImGui.text("" + channel.getCurrentRotation());
                                ImGui.text("" + channel.getCurrentScale());
                                ImGui.popID();
                            }
                        }
                    }
                    ImGui.unindent();
                }

                //print data macros
                if(ImGui.collapsingHeader("Print Data")){
                    //print bone values
                    if(ImGui.button("Print current bone values")){
                        for(Bone bone : actor.getBoneValues()){
                            LoggerInterface.loggerRenderer.DEBUG(bone.boneID);
                            LoggerInterface.loggerRenderer.DEBUG("" + bone.getFinalTransform());
                        }
                    }
        
                    //print animation keys
                    if(ImGui.button("Print animation keys")){
                        Model model = Globals.assetManager.fetchModel(actor.getModelPath());
                        model.describeAllAnimations();
                    }
                }
            }
            ImGui.unindent();
        }
    }

}
