package net.pietru.omni_power.api;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Quaternion;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import de.pottgames.tuningfork.SoundBuffer;
import finalforeach.cosmicreach.GameAssetLoader;
import finalforeach.cosmicreach.GameSingletons;
import finalforeach.cosmicreach.TickRunner;
import finalforeach.cosmicreach.entities.Entity;
import finalforeach.cosmicreach.items.ItemStack;
import finalforeach.cosmicreach.savelib.crbin.CRBSerialized;
import finalforeach.cosmicreach.savelib.crbin.CRBinSerializer;
import finalforeach.cosmicreach.world.Zone;

public class HoloBlock extends Entity {
    public static final String ENTITY_TYPE_ID = "base:entity_holo_block";
    public static SoundBuffer flickSound;
    @CRBSerialized
    ItemStack itemStack;
    @CRBSerialized
    Vector3 renderSize;
    @CRBSerialized
    public Quaternion renderRotation;
    @CRBSerialized
    public boolean visible = true;


    public HoloBlock(Vector3 position, Vector3 size, Quaternion rotation) {
        super(ENTITY_TYPE_ID);
        setPosition(position);
        this.setSize(size);
        this.renderRotation=rotation;
        this.shouldSaveInChunks=false;
        this.noClip=true;
        this.canDespawn=false;
        this.gravityModifier=0F;
        this.hasGravity=false;
    }

    public HoloBlock(ItemStack itemStack, Vector3 position, Vector3 size, Quaternion rotation) {
        this(position, size, rotation);
        if (itemStack == null) {
            throw new IllegalArgumentException("itemStack cannot be null!");
        } else {
            this.itemStack = itemStack;
        }
    }

    public HoloBlock(ItemStack itemStack, Vector3 position, Vector3 size){
        this(itemStack,position, size, new Quaternion());
    }

    public HoloBlock(ItemStack itemStack, Vector3 position, float s){
        this(itemStack,position, new Vector3(s,s,s), new Quaternion());
    }

    public HoloBlock(ItemStack itemStack, Vector3 position){
        this(itemStack,position, new Vector3(1F,1F,1F), new Quaternion());
    }



    public void setSize(float s) {
        setSize(new Vector3(s,s,s));
    }

    public void setSize(Vector3 s) {
        this.renderSize = s;
        this.localBoundingBox.min.set(-s.x / 2.0F, -s.y / 2.0F, -s.z / 2.0F);
        this.localBoundingBox.max.set(s.x / 2.0F, s.y / 2.0F, s.z / 2.0F);
        this.localBoundingBox.update();
    }


    
    public void onAttackInteraction(Entity sourceEntity) {
        float dx = this.position.x - sourceEntity.position.x;
        float dz = this.position.z - sourceEntity.position.z;
        float l = Vector2.len(dx, dz);
        if (l != 0.0F) {
            if (GameSingletons.soundManager != null) {
                GameSingletons.soundManager.playSound3D(flickSound, this.position, 1.0F, MathUtils.random(1.0F, 1.2F));
            }
        }
    }

    @Override
    public void write(CRBinSerializer serial) {
        //super.write(serial);
        //Probably disable for Hologram it can be recreated next time game starts
    }

    public void die(Zone zone) {
        this.hitpoints = 0.0F;
        this.onHitpointChange();
    }

    public void render(Camera worldCamera) {
        if (!GameSingletons.isHost) {
            this.age += Gdx.graphics.getDeltaTime();
        }

        if (visible && worldCamera.frustum.boundsInFrustum(this.globalBoundingBox)) {
            if (this.modelInstance == null && this.itemStack != null) {
                this.modelInstance = GameSingletons.itemEntityModelLoader.load(this.itemStack);
            }

            if (this.modelInstance != null) {
                tmpModelMatrix.idt();
                tmpRenderPos.set(this.lastRenderPosition);
                TickRunner.INSTANCE.partTickSlerp(tmpRenderPos, this.position);
                this.lastRenderPosition.set(tmpRenderPos);
                tmpModelMatrix.translate(tmpRenderPos);
                tmpModelMatrix.scl(this.renderSize);
                tmpModelMatrix.rotate(renderRotation);
                //tmpModelMatrix.translate(-0.5F, -0.5F, -0.5F);
                this.renderModelAfterMatrixSet(worldCamera);
            }

        }
    }

    static {
        if (GameSingletons.isClient) {
            flickSound = GameAssetLoader.getSound("sounds/entities/item/item-flick.ogg");
        }
    }
}
