package com.anand.ssp;

import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.anand.firebaseapp.R;
import com.google.android.exoplayer2.ExoPlayer;
import com.google.android.exoplayer2.MediaItem;
import com.google.android.exoplayer2.ui.PlayerView;

public class SampleVast extends AppCompatActivity {

    private PlayerView playerView;
    private ExoPlayer player;
    private ImageView nonLinearAd;

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

        playerView = findViewById(R.id.player_view);
        nonLinearAd = findViewById(R.id.non_linear_ad);

        // Initialize ExoPlayer
        player = new ExoPlayer.Builder(this).build();
        playerView.setPlayer(player);

        // Load your main video URL
        String videoUrl = "https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4"; // Replace with your video URL
        MediaItem mediaItem = MediaItem.fromUri(videoUrl);
        player.setMediaItem(mediaItem);

        // Prepare and play
        player.prepare();
        player.play();

        // Simulate loading a non-linear ad (you would actually parse this from VAST)
//        showNonLinearAd("https://revphpe.djaxbidder.com:6348/SDKvast?zoneid=184");
        showNonLinearAd("https://revphpe.djaxbidder.com:6348/SDKvast?zoneid=184");
    }

    private void showNonLinearAd(String adUrl) {
        nonLinearAd.setVisibility(View.VISIBLE);
        nonLinearAd.setOnClickListener(v -> {
            // Handle ad click (e.g., open in a web browser)
            // Launch intent or similar to handle the URL
        });

        // Optionally, load the ad image or content into the ImageView
    }


    @Override
    protected void onStop() {
        super.onStop();
        player.release(); // Release the player when the activity is stopped
    }
}
