package com.kangacult.octogone;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.assets.loaders.ModelLoader;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g3d.Material;
import com.badlogic.gdx.graphics.g3d.Model;
import com.badlogic.gdx.graphics.g3d.ModelBatch;
import com.badlogic.gdx.graphics.g3d.ModelInstance;
import com.badlogic.gdx.graphics.g3d.attributes.BlendingAttribute;
import com.badlogic.gdx.graphics.g3d.attributes.DepthTestAttribute;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector3;
import com.kangacult.octogone.Audio.SoundType;

public class Fountains {
    public static final int NUM_FONTS = 4;
    public static final float RADIUS = 5.0f;
    public Model powerup;

    public Vector3[] position = new Vector3[NUM_FONTS];
    public float[] cooldown = new float[NUM_FONTS];
    public float[] rotation = new float[NUM_FONTS];
    public ModelInstance[] model = new ModelInstance[NUM_FONTS];
    
    public void init(ModelLoader<?> loader) {
        position = new Vector3[] {
                new Vector3(-15.13137f, 0.58567f, 1f),
                new Vector3(0f, -14.10046f, 1f),
                new Vector3(14.28209f, 0.58567f, 1f),
                new Vector3(-0.65902f, 14.27442f, 1f),
        };

	    powerup = loader.loadModel(Gdx.files.internal("Powerup.g3db"));
	    for (Material m: powerup.materials) {
            DepthTestAttribute depth = new DepthTestAttribute();
            depth.depthMask = false;
            m.set(depth);
            
            BlendingAttribute blend = new BlendingAttribute();
            blend.sourceFunction = GL20.GL_SRC_ALPHA;
            blend.destFunction = GL20.GL_ONE_MINUS_SRC_ALPHA;
            m.set(blend);
	    }
	    
        for (int i = 0; i < NUM_FONTS; i++) {
            model[i] = new ModelInstance(powerup);
            model[i].transform.setToTranslation(position[i]);
        }
    }
    
    public void reset() {
        for (int i = 0; i < NUM_FONTS; i++) {
            cooldown[i] = 0f;
            rotation[i] = MathUtils.random(360f);
        }
    }
    
    public void dispose() {
        powerup.dispose();
    }
    
    public void update(float deltaTime, Player player, Audio audio) {
        for (int i = 0; i < NUM_FONTS; i++) {
            if (cooldown[i] > 0f) {
                cooldown[i] -= deltaTime;
                rotation[i] += deltaTime * 20.0f;
                rotation[i] %= 360f;
                model[i].transform.setToTranslation(position[i]);
                model[i].transform.rotate(Vector3.Z, rotation[i]);
            } else if (position[i].dst2(player.position) < RADIUS * RADIUS) {
                player.armed += deltaTime * 4f;
                if (player.armed > 1f) {
                    player.armed = 1f;
                    cooldown[i] = 10f;
                    audio.playSound(SoundType.PLAYER_WATERREFILL, player.position, player.direction, player.position);
                }
            }
        }
    }
    
    public void render(ModelBatch batch) {
        for (int i = 0; i < NUM_FONTS; i++) {
            if (cooldown[i] <= 0f) {
                batch.render(model[i]);
            }
        }
    }
}
