package com.helldivers.app;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private SoundManager soundManager;
    private Button btnQrInstagram;
    private Button btnMinijuego;
    private Button btnMusic;
    private ImageButton btnUp, btnDown, btnLeft, btnRight;
    private MediaPlayer mediaPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        try {
            soundManager = new SoundManager(this);
        } catch (Exception e) {
            soundManager = null;
        }

        btnQrInstagram = findViewById(R.id.btn_qr_instagram);
        btnMinijuego = findViewById(R.id.btn_minijuego);
        btnUp = findViewById(R.id.btn_up);
        btnDown = findViewById(R.id.btn_down);
        btnLeft = findViewById(R.id.btn_left);
        btnRight = findViewById(R.id.btn_right);
        btnMusic = findViewById(R.id.btn_music);

        if (btnQrInstagram != null) {
            btnQrInstagram.setOnClickListener(v -> {
                startActivity(new Intent(this, ActivityQR.class));
            });
        }

        if (btnMinijuego != null) {
            btnMinijuego.setOnClickListener(v -> {
                startActivity(new Intent(this, ActivityGame.class));
            });
        }

        if (btnMusic != null) {
            btnMusic.setOnClickListener(v -> toggleMusic());
        }

        View.OnClickListener arrowSoundListener = v -> {
            if (soundManager != null) {
                try { soundManager.playButtonClick(); } catch (Exception e) {}
            }
        };

        if (btnUp != null) btnUp.setOnClickListener(arrowSoundListener);
        if (btnDown != null) btnDown.setOnClickListener(arrowSoundListener);
        if (btnLeft != null) btnLeft.setOnClickListener(arrowSoundListener);
        if (btnRight != null) btnRight.setOnClickListener(arrowSoundListener);
    }

    private void toggleMusic() {
        if (mediaPlayer == null) {
            mediaPlayer = MediaPlayer.create(this, R.raw.ost);
            mediaPlayer.setOnCompletionListener(mp -> {
                stopMusic();
            });
        }

        if (mediaPlayer.isPlaying()) {
            stopMusic();
        } else {
            mediaPlayer.start();
            btnMusic.setAlpha(0.5f); // Efecto visual de activado
        }
    }

    private void stopMusic() {
        if (mediaPlayer != null) {
            mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer = null;
            btnMusic.setAlpha(1.0f); // Restaurar opacidad
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (soundManager != null) soundManager.resume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (soundManager != null) soundManager.pause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopMusic();
        if (soundManager != null) soundManager.release();
    }
}