package com.hi.music.player.media3;

import android.content.ComponentName;

import androidx.media3.common.MediaItem;
import androidx.media3.common.MediaMetadata;
import androidx.media3.session.MediaController;
import androidx.media3.session.SessionToken;

import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.MoreExecutors;
import com.hi.music.player.MusicApplication;
import com.hi.music.player.helper.CommonUtils;
import com.hi.music.player.javabean.response.ResponsePlay;
import com.hi.music.player.network.RetrofitManager;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

public class MyMediaControllerManager {

    private static volatile MyMediaControllerManager myMediaControllerManagerInstance;

    private MediaController mediaController;
    public static MyMediaControllerManager getInstance() {
        if (myMediaControllerManagerInstance == null) {
            synchronized (RetrofitManager.class) {
                if (myMediaControllerManagerInstance == null) {
                    myMediaControllerManagerInstance = new MyMediaControllerManager();
                }
            }
        }
        return myMediaControllerManagerInstance;
    }

    public void init(){
        SessionToken sessionToken =
                new SessionToken(MusicApplication.myApplication, new ComponentName(MusicApplication.myApplication, PlaybackService.class));
        ListenableFuture<MediaController> controllerFuture =
                new MediaController.Builder(MusicApplication.myApplication, sessionToken).buildAsync();
        controllerFuture.addListener(() -> {
            // Call controllerFuture.get() to retrieve the MediaController.
            // MediaController implements the Player interface, so it can be
            // attached to the PlayerView UI component.
//            playerView.setPlayer(controllerFuture.get());
            try {
                mediaController = controllerFuture.get();
            } catch (ExecutionException | InterruptedException e) {
                CommonUtils.LogErrorMsg(e.getMessage());
            }
        }, MoreExecutors.directExecutor());



    }

    public void addMusicPlay(ResponsePlay responsePlay){
        List<MediaItem> mediaItems = new ArrayList<>();
        MediaItem.Builder builder = new MediaItem.Builder();
        builder.setUri(responsePlay.getVideoId());
        MediaMetadata.Builder MediaMetadata_builder = new MediaMetadata.Builder();
//        MediaMetadata_builder.setMediaType();
//        MediaMetadata_builder.setAlbumTitle();
//        MediaMetadata_builder.setArtist();
//        MediaMetadata_builder.setDurationMs();
//        MediaMetadata_builder.setTitle();
//        MediaMetadata_builder.setRecordingYear();
        builder.setMediaMetadata(MediaMetadata_builder.build());

        mediaItems.add(builder.build());
        mediaController.addMediaItems(mediaItems);
        mediaController.prepare();
    }

    public void play(){
        mediaController.play();
    }
}
