package com.offline.music.playermp3.dialog;

import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.OptIn;
import androidx.media3.common.MediaItem;
import androidx.media3.common.util.UnstableApi;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.offline.music.playermp3.MusicApplication;
import com.offline.music.playermp3.R;
import com.offline.music.playermp3.adapter.AdapterPlayList;
import com.offline.music.playermp3.api.onImageColorListener;
import com.offline.music.playermp3.databinding.DialogPlayListBinding;
import com.offline.music.playermp3.helper.CommonUtils;
import com.offline.music.playermp3.javabean.response.ResponsePlayListInfo;
import com.offline.music.playermp3.media3.MyMediaControllerManager;

import java.util.List;

public class DialogPlayList extends BaseDialog<DialogPlayListBinding> {
    private MyMediaControllerManager instance;

    @Override
    protected DialogPlayListBinding getViewBinding(LayoutInflater inflater, ViewGroup container) {
        return DialogPlayListBinding.inflate(inflater, container, false);
    }

    @Override
    protected void initView() {

        instance = MyMediaControllerManager.getInstance();
        initPlayListUi();

        MediaItem currentMediaItem = instance.getCurMediaItem();
        if (currentMediaItem != null) {
            Uri artworkUri = currentMediaItem.mediaMetadata.artworkUri;
            vb.topSongName.setText(currentMediaItem.mediaMetadata.title);
            vb.topSingerName.setText(currentMediaItem.mediaMetadata.artist);

            Glide.with(MusicApplication.myApplication)
                    .asDrawable()
                    .apply(RequestOptions.bitmapTransform(new RoundedCorners(CommonUtils.dpToPx(10))))
                    .load(artworkUri)
                    .placeholder(R.mipmap.im_placeholder)
                    .listener(new RequestListener<Drawable>() {
                        @Override
                        public boolean onLoadFailed(@Nullable GlideException e, @Nullable Object model, @NonNull Target<Drawable> target, boolean isFirstResource) {
                            CommonUtils.LogMsg(e.getMessage());
                            return false;
                        }

                        @OptIn(markerClass = UnstableApi.class)
                        @Override
                        public boolean onResourceReady(@NonNull Drawable resource, @NonNull Object model, Target<Drawable> target, @NonNull DataSource dataSource, boolean isFirstResource) {
                            CommonUtils.getDominantDarkColor1(resource, new onImageColorListener() {
                                @Override
                                public void onImageColor(int color) {
                                    if (color == -1) {
                                        return;
                                    }
                                    int lighterColor = CommonUtils.adjustBrightness(color, 1.2f);  // 比原始颜色亮 20%
                                    int darkerColor = CommonUtils.adjustBrightness(color, 0.8f);  // 比原始颜色暗 20%
//                                    GradientDrawable gradientDrawable = new GradientDrawable(
//                                            GradientDrawable.Orientation.TOP_BOTTOM,
//                                            new int[]{lighterColor, darkerColor}  // 浅到深渐变
//                                    );
                                    vb.topLayout.setBackgroundColor(darkerColor);
                                    Drawable newDrawable = CommonUtils.getNewDrawable(lighterColor, 24f, 24f, 0, 0);
                                    vb.listLayout.setBackground(newDrawable);


                                }
                            });

                            return false;
                        }
                    })
                    .into(vb.topIm);
        }
        vb.imPlay.setSelected(instance.getIsPlaying());

        vb.imPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                vb.imPlay.setSelected(!vb.imPlay.isSelected());
                if (vb.imPlay.isSelected()) {
                    instance.play();
                } else {
                    instance.pause();
                }
            }
        });


    }

    @Override
    public boolean isFullScreen() {
        return true;
    }

    private void initPlayListUi() {
        List<ResponsePlayListInfo> playList = instance.getPlayList();
        AdapterPlayList adapterPlayList = new AdapterPlayList();
        vb.recyclerList.setLayoutManager(new LinearLayoutManager(MusicApplication.myApplication));
        adapterPlayList.addData(playList);
        vb.recyclerList.setAdapter(adapterPlayList);
    }


}
